feat: add number sqrt with all three return strategies

This commit is contained in:
Glen Whitney 2025-04-27 06:54:29 -07:00
parent 793dd361a6
commit bc8c2ff7fb
4 changed files with 48 additions and 1 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
})
})