feat: Implement subtypes

This should eventually be moved into typed-function itself, but for
  now it can be implemented on top of the existing typed-function.

  Uses subtypes to define (and error-check) gcd and lcm, which are only
  defined for integer arguments.

  Resolves #36.
This commit is contained in:
Glen Whitney 2022-07-30 04:59:04 -07:00
parent 4d38f4161c
commit c429c19dfe
35 changed files with 294 additions and 43 deletions

View file

@ -52,4 +52,16 @@ describe('bigint', () => {
assert.deepStrictEqual(bo.sqrt(-3249n), bo.complex(0n, 57n))
})
it('computes gcd', () => {
assert.strictEqual(math.gcd(105n, 70n), 35n)
})
it('computes lcm', () => {
assert.strictEqual(math.lcm(105n, 70n), 210n)
assert.strictEqual(math.lcm(15n, 60n), 60n)
assert.strictEqual(math.lcm(0n, 17n), 0n)
assert.strictEqual(math.lcm(20n, 0n), 0n)
assert.strictEqual(math.lcm(0n, 0n), 0n)
})
})

View file

@ -29,4 +29,10 @@ describe('complex', () => {
math.complex(ms.negate(ms.sqrt(0.5)), ms.sqrt(0.5)))
})
it('computes gcd', () => {
assert.deepStrictEqual(
math.gcd(math.complex(53n, 56n), math.complex(47n, -13n)),
math.complex(4n, 5n))
})
})

View file

@ -39,6 +39,7 @@ describe('A custom instance', () => {
assert.strictEqual(pm.subtract(5, 10), -5)
pm.install(complexAdd)
pm.install(complexNegate)
pm.install(complexComplex)
// Should be enough to allow complex subtraction, as subtract is generic:
assert.deepStrictEqual(
pm.subtract({re:5, im:0}, {re:10, im:1}), {re:-5, im: -1})

View file

@ -27,4 +27,7 @@ describe('number', () => {
assert.deepStrictEqual(no.sqrt(-16), no.complex(0,4))
})
it('computes gcd', () => {
assert.strictEqual(math.gcd(15, 35), 5)
})
})