pocomath/test/complex/_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

33 lines
1.1 KiB
JavaScript

import assert from 'assert'
import math from '../../src/pocomath.mjs'
import PocomathInstance from '../../src/core/PocomathInstance.mjs'
import * as complexSqrt from '../../src/complex/sqrt.mjs'
describe('complex', () => {
it('supports sqrt', () => {
assert.deepStrictEqual(math.sqrt(math.complex(1,0)), 1)
assert.deepStrictEqual(
math.sqrt(math.complex(0,1)),
math.complex(math.sqrt(0.5), math.sqrt(0.5)))
assert.deepStrictEqual(
math.sqrt(math.complex(5, 12)),
math.complex(3, 2))
math.config.predictable = true
assert.deepStrictEqual(math.sqrt(math.complex(1,0)), math.complex(1,0))
assert.deepStrictEqual(
math.sqrt(math.complex(0,1)),
math.complex(math.sqrt(0.5), math.sqrt(0.5)))
math.config.predictable = false
})
it('can bundle sqrt', async function () {
const ms = new PocomathInstance('Minimal Sqrt')
ms.install(complexSqrt)
await ms.importDependencies(['number', 'complex'])
assert.deepStrictEqual(
ms.sqrt(math.complex(0, -1)),
math.complex(ms.negate(ms.sqrt(0.5)), ms.sqrt(0.5)))
})
})