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.
This commit is contained in:
parent
b21d2b59fa
commit
f68c7bd1fb
44 changed files with 287 additions and 109 deletions
|
@ -22,11 +22,9 @@ describe('The default full pocomath instance "math"', () => {
|
|||
add: {
|
||||
'...stringK': () => addends => addends.reduce((x,y) => x+y, '')
|
||||
},
|
||||
Types: {
|
||||
stringK: {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
}
|
||||
Type_stringK: {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
}
|
||||
})
|
||||
assert.strictEqual(math.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
|
|
55
test/bigint/_all.mjs
Normal file
55
test/bigint/_all.mjs
Normal file
|
@ -0,0 +1,55 @@
|
|||
import assert from 'assert'
|
||||
import math from '../../src/pocomath.mjs'
|
||||
import PocomathInstance from '../../src/core/PocomathInstance.mjs'
|
||||
import * as bigintSqrt from '../../src/bigint/sqrt.mjs'
|
||||
import * as complex from '../../src/complex/all.mjs'
|
||||
import * as bigints from '../../src/bigint/all.mjs'
|
||||
|
||||
describe('bigint', () => {
|
||||
it('can divide', () => {
|
||||
assert.strictEqual(math.divide(15n, 5n), 3n)
|
||||
assert.strictEqual(math.divide(14n, 5n), undefined)
|
||||
math.config.predictable = true
|
||||
assert.strictEqual(math.divide(14n, 5n), 2n)
|
||||
assert.strictEqual(math.divide(-14n, 5n), -3n)
|
||||
assert.strictEqual(math.divide(14n, -5n), -3n)
|
||||
assert.strictEqual(math.divide(-14n, -5n), 2n)
|
||||
math.config.predictable = false
|
||||
})
|
||||
|
||||
it('supports sqrt', () => {
|
||||
assert.strictEqual(math.sqrt(0n), 0n)
|
||||
assert.strictEqual(math.sqrt(4n), 2n)
|
||||
assert.strictEqual(math.sqrt(5n), undefined)
|
||||
assert.strictEqual(
|
||||
math.sqrt(82120471531550314555681345949499512621827274120673745141541602816614526075010755373654280259022317599142038423759320355177481886719814621305828811322920076213800348341464996337890625n),
|
||||
9062034624274524065844376014975805577107171799890766992670739972241112960081909332275390625n)
|
||||
assert.deepStrictEqual(math.sqrt(-9n), math.complex(0n, 3n))
|
||||
assert.deepStrictEqual(
|
||||
math.sqrt(math.complex(5n, 12n)),
|
||||
math.complex(3n, 2n))
|
||||
assert.deepStrictEqual(math.sqrt(math.complex(1n, 0n)), 1n)
|
||||
assert.deepStrictEqual(math.sqrt(math.complex(0n, 1n)), undefined)
|
||||
math.config.predictable = true
|
||||
assert.strictEqual(math.sqrt(-9n), -9n)
|
||||
assert.deepStrictEqual(
|
||||
math.sqrt(math.complex(1n, 0n)), math.complex(1n, 0n))
|
||||
assert.deepStrictEqual(
|
||||
math.sqrt(math.complex(0n, 1n)), math.complex(0n, 0n))
|
||||
assert.deepStrictEqual(
|
||||
math.sqrt(math.complex(0n, 2n)), math.complex(1n, 1n))
|
||||
math.config.predictable = false
|
||||
assert.deepStrictEqual(math.sqrt(-1024n), math.complex(0n, 32n))
|
||||
})
|
||||
|
||||
it('supports sqrt by itself', () => {
|
||||
const bo = new PocomathInstance('BigInts Only')
|
||||
bo.install(bigintSqrt)
|
||||
assert.strictEqual(bo.sqrt(256n), 16n)
|
||||
assert.strictEqual(bo.sqrt(-17n), undefined)
|
||||
bo.install(complex)
|
||||
bo.install(bigints)
|
||||
assert.deepStrictEqual(bo.sqrt(-3249n), bo.complex(0n, 57n))
|
||||
})
|
||||
|
||||
})
|
|
@ -9,6 +9,9 @@ describe('complex', () => {
|
|||
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(
|
||||
|
|
|
@ -23,7 +23,7 @@ describe('A custom instance', () => {
|
|||
|
||||
it("can be assembled in any order", () => {
|
||||
bw.install(numbers)
|
||||
bw.install({Types: {string: {test: s => typeof s === 'string'}}})
|
||||
bw.install({Type_string: {test: s => typeof s === 'string'}})
|
||||
assert.strictEqual(bw.subtract(16, bw.add(3,4,2)), 7)
|
||||
assert.strictEqual(bw.negate('8'), -8)
|
||||
assert.deepStrictEqual(bw.add(bw.complex(1,3), 1), {re: 2, im: 3})
|
||||
|
|
|
@ -4,6 +4,7 @@ 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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue