diff --git a/package.json5 b/package.json5 index a0cd6c3..b6575a1 100644 --- a/package.json5 +++ b/package.json5 @@ -7,7 +7,7 @@ test: 'echo "Error: no test specified" && exit 1', build: 'mkdirp build && cpy etc/package.json build --flat && tsc', start: 'node build', - go: 'pnpm --sequential "/build|start/"', + go: 'pnpm --sequential "/clean|build|start/"', clean: 'del-cli build', }, packageManager: 'pnpm', diff --git a/src/index.ts b/src/index.ts index 8b4ecfd..dc389e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,17 +8,30 @@ import {absquare as absquare_complex} from './Complex/arithmetic.js' const mockRealAdd = (a: number, b: number) => a+b const mockComplexAbsquare = (z: Complex) => z.re*z.re + z.im*z.im +const mockComplex = (re: number, im: number) => ({ re, im }) + +const config = { + predictable: false, + epsilon: 1e-14 +} const quatAbsquare = absquare_complex({ - add: mockRealAdd, - absquare: mockComplexAbsquare + add: mockRealAdd, + absquare: mockComplexAbsquare }) const myabs = quatAbsquare({re: {re: 0, im: 1}, im: {re:2, im: 3}}) const typeTest: typeof myabs = 7 // check myabs is just a number +console.log('Result is myabs=', myabs) -console.log('Result is', myabs) +const sqrt = Specifications.numbers.sqrt({ + config, + complex: mockComplex +}) +console.log('Result of sqrt(16)=', sqrt(16)) +console.log('Result of sqrt(-4)=', sqrt(-4)) // Check type of the generic square implementation +console.log('Type of sqrt (number) is', Specifications.numbers.sqrt.reflectedType) console.log('Type of square is', Specifications.generic.square.reflectedType) console.log('Type of complex square root is', Specifications.complex.sqrt.reflectedType) diff --git a/src/numbers/arithmetic.ts b/src/numbers/arithmetic.ts index 11da5c2..d2d3414 100644 --- a/src/numbers/arithmetic.ts +++ b/src/numbers/arithmetic.ts @@ -1,5 +1,6 @@ import type {configDependency} from '../core/Config.js' import type {Dependencies, Signature} from '../interfaces/type.js' +import { $implement } from '../interfaces/type.js' export const add: Signature<'add', number> = (a, b) => a + b export const unaryMinus: Signature<'unaryMinus', number> = a => -a @@ -13,13 +14,14 @@ export const divide: Signature<'divide', number> = (a, b) => a / b const basicSqrt = a => isNaN(a) ? NaN : Math.sqrt(a) export const conservativeSqrt: Signature<'conservativeSqrt', number> = basicSqrt -export const sqrt = - (dep: configDependency & Dependencies<'complex', number>): - Signature<'sqrt', number> => { - if (dep.config.predictable || !dep.complex) return basicSqrt - return a => { - if (isNaN(a)) return NaN - if (a >= 0) return Math.sqrt(a) - return dep.complex(0, Math.sqrt(unaryMinus(a))) - } - } +$implement!('sqrt', + (dep: configDependency & Dependencies<'complex', number>): Signature<'sqrt', number> => { + if (dep.config.predictable || !dep.complex) { + return basicSqrt + } + return a => { + if (isNaN(a)) return NaN + if (a >= 0) return Math.sqrt(a) + return dep.complex(0, Math.sqrt(unaryMinus(a))) + } + })