2025-04-16 04:23:48 +00:00
|
|
|
import {Returns} from '#core/Type.js'
|
2025-04-24 13:09:15 -07:00
|
|
|
import {match, Optional} from '#core/TypePatterns.js'
|
2025-04-16 04:23:48 +00:00
|
|
|
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.
|
refactor: change onType to match and take only one pattern and result (#22)
Pursuant to #12. Besides changing the name of onType to match, and only allowing one pattern and result in `match()`,
this PR also arranges that in place of an onType with lots of alternating PATTERN, VALUE, PATTERN, VALUE arguments, one now exports an _array_ of `match(PATTERN, VALUE)` items.
Doesn't quite fully resolve #12, because there is still the question of whether `match(...)` can be left out for a behavior that literally matches anything (current behavior), or whether `match(Passthru, behavior)` should be required for such cases.
Reviewed-on: https://code.studioinfinity.org/StudioInfinity/nanomath/pulls/22
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-04-22 05:01:21 +00:00
|
|
|
export const indistinguishable = match(
|
2025-04-16 04:23:48 +00:00
|
|
|
[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.
|
refactor: change onType to match and take only one pattern and result (#22)
Pursuant to #12. Besides changing the name of onType to match, and only allowing one pattern and result in `match()`,
this PR also arranges that in place of an onType with lots of alternating PATTERN, VALUE, PATTERN, VALUE arguments, one now exports an _array_ of `match(PATTERN, VALUE)` items.
Doesn't quite fully resolve #12, because there is still the question of whether `match(...)` can be left out for a behavior that literally matches anything (current behavior), or whether `match(Passthru, behavior)` should be required for such cases.
Reviewed-on: https://code.studioinfinity.org/StudioInfinity/nanomath/pulls/22
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
2025-04-22 05:01:21 +00:00
|
|
|
export const exceeds = match([NumberT, NumberT], boolnum((a, b) => a > b))
|