48 lines
2.2 KiB
TypeScript
48 lines
2.2 KiB
TypeScript
import {configDependency} from '../core/Config.js'
|
|
import {Signature, Dependency, ImpType} from '../core/Dispatcher.js'
|
|
import type {Complex} from '../Complex/type.js'
|
|
|
|
declare module "./type" {
|
|
interface NumbersReturn<Params> {
|
|
// This description loses information: some subtypes like NumInt or
|
|
// Positive are closed under addition, but this says that the result
|
|
// of add is just a number, not still of the reduced type
|
|
add: Signature<Params, [number, number], number>
|
|
// Whereas this one would preserve information, but would lie
|
|
// because it claims all subtypes of number are closed under addition,
|
|
// which is not true for `1 | 2 | 3`, for example.
|
|
// add: Params extends BBinary<infer B>
|
|
// ? B extends number ? B : never
|
|
// : never
|
|
//
|
|
// Not sure how this will need to go when we introduce NumInt.
|
|
unaryMinus: Signature<Params, [number], number>
|
|
subtract: Signature<Params, [number, number], number>
|
|
multiply: Signature<Params, [number, number], number>
|
|
divide: Signature<Params, [number, number], number>
|
|
// Best we can do for sqrt at compile time, since actual return
|
|
// type depends on config. Not sure how this will play out
|
|
// when we make a number-only bundle, but at least the import type
|
|
// above for Complex<> does not lead to any emitted JavaScript.
|
|
sqrt: Signature<Params, [number], number | Complex<number>>
|
|
}
|
|
}
|
|
|
|
export const add: ImpType<'add', [number, number]> = (a, b) => a + b
|
|
export const unaryMinus: ImpType<'unaryMinus', [number]> = a => -a
|
|
export const subtract: ImpType<'subtract', [number, number]> = (a, b) => a - b
|
|
export const multiply: ImpType<'multiply', [number, number]> = (a, b) => a * b
|
|
export const divide: ImpType<'divide', [number, number]> = (a, b) => a / b
|
|
export const sqrt =
|
|
(dep: configDependency
|
|
& Dependency<'complex', [number, number]>): ImpType<'sqrt', [number]> => {
|
|
if (dep.config.predictable || !dep.complex) {
|
|
return a => isNaN(a) ? NaN : Math.sqrt(a)
|
|
}
|
|
return a => {
|
|
if (isNaN(a)) return NaN
|
|
if (a >= 0) return Math.sqrt(a)
|
|
return dep.complex(0, Math.sqrt(unaryMinus(a)))
|
|
}
|
|
}
|