feat: Template operations (#41)
Relational functions are added using templates, and existing generic functions are made more strict with them. Also a new built-in typeOf function is added, that automatically updates itself. Resolves #34. Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #41
This commit is contained in:
parent
2609310b8e
commit
fe54bc6004
29 changed files with 480 additions and 119 deletions
|
@ -1,4 +1,5 @@
|
|||
import assert from 'assert'
|
||||
import PocomathInstance from '../src/core/PocomathInstance.mjs'
|
||||
import math from '../src/pocomath.mjs'
|
||||
|
||||
describe('The default full pocomath instance "math"', () => {
|
||||
|
@ -10,14 +11,25 @@ describe('The default full pocomath instance "math"', () => {
|
|||
assert.strictEqual(undef.length, 0)
|
||||
})
|
||||
|
||||
it('has a built-in typeOf operator', () => {
|
||||
assert.strictEqual(math.typeOf(42), 'NumInt')
|
||||
assert.strictEqual(math.typeOf(-1.5), 'number')
|
||||
assert.strictEqual(math.typeOf(-42n), 'bigint')
|
||||
assert.strictEqual(math.typeOf(undefined), 'undefined')
|
||||
assert.strictEqual(math.typeOf({re: 15n, im: -2n}), 'GaussianInteger')
|
||||
assert.strictEqual(math.typeOf({re: 6.28, im: 2.72}), 'Complex')
|
||||
})
|
||||
|
||||
it('can subtract numbers', () => {
|
||||
assert.strictEqual(math.subtract(12, 5), 7)
|
||||
//assert.strictEqual(math.subtract(3n, 1.5), 1.5)
|
||||
})
|
||||
|
||||
it('can add numbers', () => {
|
||||
assert.strictEqual(math.add(3, 4), 7)
|
||||
assert.strictEqual(math.add(1.5, 2.5, 3.5), 7.5)
|
||||
assert.strictEqual(math.add(Infinity), Infinity)
|
||||
assert.throws(() => math.add(3n, -1.5), TypeError)
|
||||
})
|
||||
|
||||
it('can negate numbers', () => {
|
||||
|
@ -26,16 +38,17 @@ describe('The default full pocomath instance "math"', () => {
|
|||
})
|
||||
|
||||
it('can be extended', () => {
|
||||
math.installType('stringK', {
|
||||
const stretch = PocomathInstance.merge(math) // clone to not pollute math
|
||||
stretch.installType('stringK', {
|
||||
test: s => typeof s === 'string' && s.charAt(0) === 'K',
|
||||
before: ['string']
|
||||
})
|
||||
math.install({
|
||||
stretch.install({
|
||||
add: {
|
||||
'...stringK': () => addends => addends.reduce((x,y) => x+y, '')
|
||||
},
|
||||
})
|
||||
assert.strictEqual(math.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
assert.strictEqual(stretch.add('Kilroy','K is here'), 'KilroyK is here')
|
||||
})
|
||||
|
||||
it('handles complex numbers', () => {
|
||||
|
@ -46,10 +59,10 @@ describe('The default full pocomath instance "math"', () => {
|
|||
assert.deepStrictEqual(
|
||||
math.subtract(math.complex(1,1), math.complex(2,-2)),
|
||||
math.complex(-1,3))
|
||||
assert.strictEqual(math.negate(math.complex(3, 8)).im, -8)
|
||||
assert.deepStrictEqual(
|
||||
math.subtract(16, math.add(3, math.complex(0,4), 2)),
|
||||
math.complex(11, -4))
|
||||
assert.strictEqual(math.negate(math.complex(3, 8)).im, -8)
|
||||
})
|
||||
|
||||
it('handles bigints', () => {
|
||||
|
|
|
@ -38,6 +38,13 @@ describe('complex', () => {
|
|||
math.complex(ms.negate(ms.sqrt(0.5)), ms.sqrt(0.5)))
|
||||
})
|
||||
|
||||
it('checks for equality', () => {
|
||||
assert.ok(math.equal(math.complex(3,0), 3))
|
||||
assert.ok(math.equal(math.complex(3,2), math.complex(3, 2)))
|
||||
assert.ok(!(math.equal(math.complex(45n, 3n), math.complex(45n, -3n))))
|
||||
assert.ok(!(math.equal(math.complex(45n, 3n), 45n)))
|
||||
})
|
||||
|
||||
it('computes gcd', () => {
|
||||
assert.deepStrictEqual(
|
||||
math.gcd(math.complex(53n, 56n), math.complex(47n, -13n)),
|
||||
|
|
|
@ -3,6 +3,7 @@ import math from '../src/pocomath.mjs'
|
|||
import PocomathInstance from '../src/core/PocomathInstance.mjs'
|
||||
import * as numbers from '../src/number/all.mjs'
|
||||
import * as numberAdd from '../src/number/add.mjs'
|
||||
import {add as genericAdd} from '../src/generic/arithmetic.mjs'
|
||||
import * as complex from '../src/complex/all.mjs'
|
||||
import * as complexAdd from '../src/complex/add.mjs'
|
||||
import * as complexNegate from '../src/complex/negate.mjs'
|
||||
|
@ -66,6 +67,7 @@ describe('A custom instance', () => {
|
|||
const cherry = new PocomathInstance('cherry')
|
||||
cherry.install(numberAdd)
|
||||
await extendToComplex(cherry)
|
||||
cherry.install({add: genericAdd})
|
||||
/* Now we have an instance that supports addition for number and complex
|
||||
and little else:
|
||||
*/
|
||||
|
|
|
@ -30,4 +30,11 @@ describe('number', () => {
|
|||
it('computes gcd', () => {
|
||||
assert.strictEqual(math.gcd(15, 35), 5)
|
||||
})
|
||||
|
||||
it('compares numbers', () => {
|
||||
assert.ok(math.smaller(12,13.5))
|
||||
assert.ok(math.equal(Infinity, Infinity))
|
||||
assert.ok(math.largerEq(12.5, math.divide(25,2)))
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue