pocomath/test/number/_all.mjs
Glen Whitney f68c7bd1fb fix(Types): Move distinct types into distinct identifiers
This allows types to be collected; prior to this commit they
   were conflicting from different modules.

   Uses this fix to extend sqrt to bigint, with the convention
   that it is undefined for non-perfect squares when 'predictable'
   is false and is the "best" approximation to the square root when
   'predictable' is true. Furthermore, for negative bigints, you might
   get a Gaussian integer when predictable is false; or you will just get
   your argument back when 'predictable' is true because what other
   bigint could you give back for a negative bigint?

   Also had to modify tests on the sign in sqrt(Complex) and add functions
   'zero' and 'one' to get types to match, as expected in #27.

   Adds numerous tests.

   Resolves #26.
   Resolves #27.
2022-07-25 11:56:12 -07:00

31 lines
1.1 KiB
JavaScript

import assert from 'assert'
import math from '../../src/pocomath.mjs'
import PocomathInstance from '../../src/core/PocomathInstance.mjs'
import * as numberSqrt from '../../src/number/sqrt.mjs'
import * as complex from '../../src/complex/all.mjs'
import * as numbers from '../../src/number/all.mjs'
describe('number', () => {
it('supports sqrt', () => {
assert.strictEqual(math.sqrt(4), 2)
assert.strictEqual(math.sqrt(NaN), NaN)
assert.strictEqual(math.sqrt(2.25), 1.5)
assert.deepStrictEqual(math.sqrt(-9), math.complex(0, 3))
math.config.predictable = true
assert.strictEqual(math.sqrt(-9), NaN)
math.config.predictable = false
assert.deepStrictEqual(math.sqrt(-0.25), math.complex(0, 0.5))
})
it('supports sqrt by itself', () => {
const no = new PocomathInstance('Numbers Only')
no.install(numberSqrt)
assert.strictEqual(no.sqrt(2.56), 1.6)
assert.strictEqual(no.sqrt(-17), NaN)
no.install(complex)
no.install(numbers)
assert.deepStrictEqual(no.sqrt(-16), no.complex(0,4))
})
})