import {Returns} from '#core/Type.js' import {match, Optional} from '#core/TypePatterns.js' import {boolnum} from './helpers.js' import {NumberT} from './NumberT.js' // In nanomath, we take the point of view that two comparators are primitive: // indistinguishable(a, b, relTol, absTol), and exceeds(a, b). All others // are defined generically in terms of these. They typically return BooleanT, // but in a numbers-only bundle, they return 1 or 0. // Notice a feature of TypedDispatcher: if you specify one tolerance, you must // specify both. export const indistinguishable = match( [NumberT, NumberT, Optional([NumberT, NumberT])], boolnum((a, b, [tolerances = [0, 0]]) => { const [relTol, absTol] = tolerances if (relTol < 0 || absTol < 0) { throw new RangeError( `Tolerances (relative: ${relTol}, absolute: ${absTol}) ` + 'must be nonnegative') } if (isNaN(a) || isNaN(b)) return false if (a === b) return true if (!isFinite(a) || !isFinite(b)) return false // |a-b| <= absTol or |a-b| <= relTol*max(|a|, |b|) const diff = Math.abs(a-b) if (diff <= absTol) return true const magnitude = Math.max(Math.abs(a), Math.abs(b)) return diff <= relTol * magnitude }) ) // Returns truthy if a (interpreted as completely precise) represents a // greater value than b (interpreted as completely precise). Note that even if // so, a and b might be indistinguishable() to some tolerances. export const exceeds = match([NumberT, NumberT], boolnum((a, b) => a > b))