25 lines
768 B
TypeScript
25 lines
768 B
TypeScript
import {configDependency} from '../core/Config.js'
|
|
import {Dependency} from '../core/Dispatcher.js'
|
|
|
|
const unaryMinus = (a: number) => -a
|
|
|
|
export const arithmeticImps = <T>() => ({
|
|
add: (a: number, b: number) => a + b,
|
|
unaryMinus,
|
|
subtract: (a: number, b: number) => a - b,
|
|
multiply: (a: number, b: number) => a * b,
|
|
divide: (a: number, b: number) => a / b,
|
|
sqrt:
|
|
(dep: configDependency
|
|
& Dependency<'complex', [number, number]>) => {
|
|
if (dep.config.predictable || !dep.complex) {
|
|
return (a: number) => isNaN(a) ? NaN : Math.sqrt(a)
|
|
}
|
|
return (a: number) => {
|
|
if (isNaN(a)) return NaN
|
|
if (a >= 0) return Math.sqrt(a)
|
|
return dep.complex(0, Math.sqrt(unaryMinus(a)))
|
|
}
|
|
}
|
|
})
|