math5/src/numbers/arithmetic.ts
Glen Whitney 239215c234 chore: Remove use of ts-macros (#3)
Plan is to use a custom build step instead.

Reviewed-on: #3
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2024-10-15 06:53:08 +00:00

27 lines
879 B
TypeScript

import {implementations} from '@/core/Dispatcher'
import type {CommonSignature} from '@/interfaces/type'
const conservativeSqrt = (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
export const common = implementations<CommonSignature<number>>()
.independent({
add: (a, b) => a + b,
unaryMinus: a => -a,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
absquare: a => a * a,
reciprocal: a => 1 / a,
divide: (a, b) => a / b,
conj: a => a,
conservativeSqrt })
.dependent({config: {}, complex: {}}, {
sqrt: dep => {
if (dep.config().predictable || !dep.complex) return conservativeSqrt
return a => {
if (isNaN(a)) return NaN
if (a >= 0) return Math.sqrt(a)
return dep.complex(0, Math.sqrt(-a))
}
}})
.ship()