feat: add number sqrt with all three return strategies
This commit is contained in:
parent
793dd361a6
commit
bc8c2ff7fb
4 changed files with 48 additions and 1 deletions
|
@ -10,4 +10,9 @@ describe('the numbers-only bundle', () => {
|
||||||
assert.strictEqual(math.isnan(-16.5), 0)
|
assert.strictEqual(math.isnan(-16.5), 0)
|
||||||
assert.strictEqual(math.isnan(NaN), 1)
|
assert.strictEqual(math.isnan(NaN), 1)
|
||||||
})
|
})
|
||||||
|
it('takes sqrt with NaN for negative', () => {
|
||||||
|
assert.strictEqual(math.sqrt(25), 5)
|
||||||
|
assert(math.isnan(math.sqrt(-25)))
|
||||||
|
assert(math.isnan(math.sqrt(NaN)))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
import math from '#nanomath'
|
import math from '#nanomath'
|
||||||
|
import {ReturnTyping} from '#core/Type.js'
|
||||||
|
|
||||||
describe('number arithmetic', () => {
|
describe('number arithmetic', () => {
|
||||||
it('supports basic operations', () => {
|
it('supports basic operations', () => {
|
||||||
|
@ -14,4 +15,18 @@ describe('number arithmetic', () => {
|
||||||
assert.strictEqual(math.subtract(4, 2), 2)
|
assert.strictEqual(math.subtract(4, 2), 2)
|
||||||
assert.strictEqual(math.quotient(7, 3), 2)
|
assert.strictEqual(math.quotient(7, 3), 2)
|
||||||
})
|
})
|
||||||
|
it('takes square root of numbers appropriately', () => {
|
||||||
|
assert(isNaN(math.sqrt(NaN)))
|
||||||
|
assert.strictEqual(math.sqrt(4), 2)
|
||||||
|
assert.deepStrictEqual(math.sqrt(-4), math.complex(0, 2))
|
||||||
|
math.config.returnTyping = ReturnTyping.conservative
|
||||||
|
assert(isNaN(math.sqrt(NaN)))
|
||||||
|
assert.strictEqual(math.sqrt(4), 2)
|
||||||
|
assert(isNaN(math.sqrt(-4)))
|
||||||
|
math.config.returnTyping = ReturnTyping.full
|
||||||
|
assert(isNaN(math.sqrt(NaN)))
|
||||||
|
assert.deepStrictEqual(math.sqrt(4), math.complex(2, 0))
|
||||||
|
assert.deepStrictEqual(math.sqrt(-4), math.complex(0, 2))
|
||||||
|
math.config.returnTyping = ReturnTyping.free
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
import {plain} from './helpers.js'
|
import {plain} from './helpers.js'
|
||||||
|
import {NumberT} from './NumberT.js'
|
||||||
|
import {OneOf, Returns, ReturnTyping} from '#core/Type.js'
|
||||||
|
import {match} from '#core/TypePatterns.js'
|
||||||
|
import {Complex} from '#complex/Complex.js'
|
||||||
|
|
||||||
|
const {conservative, full} = ReturnTyping
|
||||||
|
|
||||||
export const abs = plain(Math.abs)
|
export const abs = plain(Math.abs)
|
||||||
export const absquare = plain(a => a*a)
|
export const absquare = plain(a => a*a)
|
||||||
|
@ -18,5 +24,25 @@ export const cbrt = plain(a => {
|
||||||
export const invert = plain(a => 1/a)
|
export const invert = plain(a => 1/a)
|
||||||
export const multiply = plain((a, b) => a * b)
|
export const multiply = plain((a, b) => a * b)
|
||||||
export const negate = plain(a => -a)
|
export const negate = plain(a => -a)
|
||||||
|
|
||||||
|
export const sqrt = match(NumberT, (math, _N, strategy) => {
|
||||||
|
if (!math.types.Complex || strategy === conservative) {
|
||||||
|
return Returns(NumberT, Math.sqrt)
|
||||||
|
}
|
||||||
|
const cplx = math.complex.resolve([NumberT, NumberT], full)
|
||||||
|
if (strategy === full) {
|
||||||
|
const cnan = math.nan(Complex(NumberT))
|
||||||
|
return Returns(Complex(NumberT), a => {
|
||||||
|
if (isNaN(a)) return cnan
|
||||||
|
return a >= 0 ? cplx(Math.sqrt(a), 0) : cplx(0, Math.sqrt(-a))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// strategy === free, return "best" type
|
||||||
|
return Returns(OneOf(NumberT, Complex(NumberT)), a => {
|
||||||
|
if (isNaN(a)) return NaN
|
||||||
|
return a >= 0 ? Math.sqrt(a) : cplx(0, Math.sqrt(-a))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
export const subtract = plain((a, b) => a - b)
|
export const subtract = plain((a, b) => a - b)
|
||||||
export const quotient = plain((a,b) => Math.floor(a/b))
|
export const quotient = plain((a,b) => Math.floor(a/b))
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
{
|
{
|
||||||
"imports" : {
|
"imports" : {
|
||||||
"#nanomath": "./nanomath.js",
|
"#nanomath": "./nanomath.js",
|
||||||
"#boolean/*.js": "./boolean/*.js",
|
|
||||||
"#core/*.js": "./core/*.js",
|
"#core/*.js": "./core/*.js",
|
||||||
|
"#boolean/*.js": "./boolean/*.js",
|
||||||
|
"#complex/*.js": "./complex/*.js",
|
||||||
"#generic/*.js": "./generic/*.js",
|
"#generic/*.js": "./generic/*.js",
|
||||||
"#number/*.js": "./number/*.js"
|
"#number/*.js": "./number/*.js"
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue