feat: add arithmetic functions for number
This commit is contained in:
parent
fea0d3ac91
commit
183a894868
2 changed files with 27 additions and 0 deletions
21
src/number/arithmetic.js
Normal file
21
src/number/arithmetic.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
import {number, plain} from './type'
|
||||
|
||||
export const abs = plain(Math.abs)
|
||||
export const absquare = plain(a => a*a)
|
||||
export const add = plain((a, b) => a + b)
|
||||
export const divide = plain((a, b) => a / b)
|
||||
export const cbrt = plain(a => {
|
||||
if (a === 0) return a
|
||||
const negate = a < 0
|
||||
if (negate) a = -a
|
||||
let result = a
|
||||
if (isFinite(a)) {
|
||||
result = Math.exp(Math.log(x) / 3)
|
||||
result = (a / (result * result) + (2 * result)) / 3
|
||||
}
|
||||
return negate ? -result : result
|
||||
})
|
||||
export const invert = plain(a => 1/a)
|
||||
export const multiply = plain((a, b) => a * b)
|
||||
export const negate = plain(a => -a)
|
||||
export const quotient = plain((a,b) => Math.floor(a/b))
|
6
src/number/type.js
Normal file
6
src/number/type.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
export const number = {
|
||||
test: n => typeof n === 'number'
|
||||
}
|
||||
|
||||
export plain = f =>
|
||||
[Array(f.length).fill(number), {returns: number, behavior: f}]
|
Loading…
Add table
Reference in a new issue