-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab51f41
commit 244aff9
Showing
3 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** Computes the IEEE 754 floating-point remainder of x / y. */ | ||
export const remainder = (x: number, y: number): number => { | ||
if (isNaN(x) || isNaN(y) || !isFinite(x) || y === 0) return NaN | ||
|
||
const quotient = x / y | ||
let n = Math.round(quotient) | ||
|
||
// When quotient is exactly halfway between two integers, round to the nearest even integer | ||
if (Math.abs(quotient - n) === 0.5) n = 2 * Math.round(quotient / 2) | ||
|
||
const rem = x - n * y | ||
return !rem ? Math.sign(x) * 0 : rem | ||
} | ||
|
||
/** Returns the next representable floating-point value after x towards y. */ | ||
export const nextAfter = (x: number, y: number): number => { | ||
if (isNaN(x) || isNaN(y)) return NaN | ||
if (x === y) return y | ||
if (x === 0) return y > 0 ? Number.MIN_VALUE : -Number.MIN_VALUE | ||
|
||
const buffer = new ArrayBuffer(8) | ||
const view = new DataView(buffer) | ||
|
||
view.setFloat64(0, x, true) | ||
let bits = view.getBigUint64(0, true) | ||
|
||
if (x > 0 === y > x) bits += 1n | ||
else bits -= 1n | ||
|
||
view.setBigUint64(0, bits, true) | ||
return view.getFloat64(0, true) | ||
} | ||
|
||
/** Returns true IFF a is within epsilon distance of b. */ | ||
export const float64Near = (a: number, b: number, epsilon: number) => Math.abs(a - b) <= epsilon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import test from 'node:test' | ||
import { equal, ok } from 'node:assert/strict' | ||
import { remainder, nextAfter, float64Near } from './math' | ||
|
||
test('remainder', t => { | ||
equal(remainder(5.1, 2), -0.9000000000000004) | ||
equal(remainder(5.5, 2), -0.5) | ||
equal(remainder(-5.5, 2), 0.5) | ||
equal(remainder(5, 2.5), 0) | ||
equal(remainder(5.1, 0), NaN) | ||
equal(remainder(Infinity, 2), NaN) | ||
equal(remainder(5, NaN), NaN) | ||
equal(remainder(0, 2), 0) | ||
equal(remainder(5, 2), 1) | ||
equal(remainder(-5, 2), -1) | ||
}) | ||
|
||
test('nextAfter', t => { | ||
equal(nextAfter(0, 1), 5e-324) | ||
equal(nextAfter(0, -1), -5e-324) | ||
equal(nextAfter(1, 2), 1.0000000000000002) | ||
equal(nextAfter(1, 0), 0.9999999999999999) | ||
equal(nextAfter(1, 1), 1) | ||
equal(nextAfter(Number.MAX_VALUE, Infinity), Infinity) | ||
equal(nextAfter(-Number.MAX_VALUE, -Infinity), -Infinity) | ||
equal(nextAfter(Number.NaN, 1), NaN) | ||
equal(nextAfter(1, Number.NaN), NaN) | ||
}) | ||
|
||
test('float64Near', t => { | ||
ok(float64Near(0, 0, 0)) | ||
ok(float64Near(1e-10, 1e-10*2, 1e-10)) | ||
ok(!float64Near(1e-10, 1e-9, 1e-10)) | ||
ok(!float64Near(1e-5, 1e-4, 1e-5/10)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters