feat: Add complex numbers

No negation (and therefore no subtraction) since that needs self-reference.
This commit is contained in:
Glen Whitney 2022-07-19 09:52:16 -07:00
parent b59a8c2ca9
commit b82336e590
7 changed files with 71 additions and 0 deletions

View file

@ -16,4 +16,18 @@ describe('The default full pocomath instance "math"', () => {
assert.strictEqual(math.negate(-1), 1)
assert.strictEqual(math.add(10, math.negate(3)), 7)
})
it('can be extended', () => {
math.install({'add': {
'...string': [[], addends => addends.reduce((x,y) => x+y, '')]
}})
assert.strictEqual(math.add('Kilroy',' is here'), 'Kilroy is here')
})
it('handles complex numbers', () => {
const norm13 = {re: 2, im: 3}
assert.deepStrictEqual(math.complex(2,3), norm13)
assert.deepStrictEqual(math.complex(2), math.complex(2,0))
assert.deepStrictEqual(math.add(2, math.complex(0,3)), norm13)
})
})

21
test/custom.mjs Normal file
View file

@ -0,0 +1,21 @@
import assert from 'assert'
import typed from 'typed-function'
import PocomathInstance from '../PocomathInstance.mjs'
import * as numbers from '../number/all.mjs'
import * as complex from '../complex/all.mjs'
const bw = new PocomathInstance('backwards')
describe('A custom instance', () => {
it("works when partially assembled", () => {
bw.install(complex)
assert.deepStrictEqual(bw.add(2, bw.complex(0, 3)), {re: 2, im: 3})
})
it("can be assembled in any order", () => {
bw.install(numbers)
typed.addConversion({from: 'string', to: 'number', convert: x => +x})
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})
})
})