feat: Add return typing strategies and implement sqrt with them (#26)
All checks were successful
/ test (push) Successful in 17s

Resolves #25

Reviewed-on: #26
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2025-04-28 16:29:33 +00:00 committed by Glen Whitney
parent aad62df8ac
commit 0765ba7202
35 changed files with 1125 additions and 152 deletions

View file

@ -1,5 +1,6 @@
import assert from 'assert'
import math from '#nanomath'
import {ReturnTyping} from '#core/Type.js'
describe('number arithmetic', () => {
it('supports basic operations', () => {
@ -14,4 +15,18 @@ describe('number arithmetic', () => {
assert.strictEqual(math.subtract(4, 2), 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
})
})

View file

@ -10,4 +10,11 @@ describe('number utilities', () => {
assert.strictEqual(math.isnan(Infinity), false)
assert.strictEqual(math.isnan(43), false)
})
it('tests if a number is an integer', () => {
assert(math.isInteger(7))
assert(math.isInteger(7+5e-16))
assert(!math.isInteger(7.000001))
assert(!math.isInteger(-Infinity))
assert(!math.isInteger(NaN))
})
})

View file

@ -1,4 +1,10 @@
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 absquare = plain(a => a*a)
@ -18,5 +24,25 @@ export const cbrt = plain(a => {
export const invert = plain(a => 1/a)
export const multiply = plain((a, b) => a * b)
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 quotient = plain((a,b) => Math.floor(a/b))

View file

@ -1,4 +1,3 @@
import {Returns} from '#core/Type.js'
import {match, Optional} from '#core/TypePatterns.js'
import {boolnum} from './helpers.js'
import {NumberT} from './NumberT.js'

View file

@ -1,8 +1,13 @@
import {plain, boolnum} from './helpers.js'
import {NumberT} from './NumberT.js'
import {Returns} from '#core/Type.js'
import {match} from '#core/TypePatterns.js'
export const clone = plain(a => a)
export const isfinite = match(NumberT, boolnum(isFinite))
export const isInteger = match(NumberT, math => {
const finite = math.isfinite.resolve(NumberT)
const eq = math.equal.resolve([NumberT, NumberT])
return boolnum(a => finite(a) && eq(a, Math.round(a)))(math)
})
export const isnan = match(NumberT, boolnum(isNaN))