-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
f76f05b
commit 1705875
Showing
5 changed files
with
92 additions
and
79 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
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
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
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
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 |
---|---|---|
@@ -1,15 +1,42 @@ | ||
export const epsilon = 3.4028235 * 10 ** 38; | ||
// export const epsilon = Number.MAX_VALUE; | ||
|
||
export function absDiffEq( | ||
x: number, | ||
y: number, | ||
epsilon = Number.MIN_VALUE, | ||
): boolean { | ||
return (x > y ? x - y : x - y) <= epsilon; | ||
} | ||
|
||
export function absDiffNe( | ||
x: number, | ||
y: number, | ||
epsilon = Number.MIN_VALUE, | ||
): boolean { | ||
return !absDiffNe(x, y, epsilon); | ||
return !absDiffNe(x, y); | ||
} | ||
|
||
export function relativeDiff(x: number, y: number): number { | ||
return Math.abs((x - y) / Math.min(x, y)); | ||
} | ||
|
||
export function epsilonDiff(x: number, y: number): number { | ||
return relativeDiff(x, y) / epsilon; | ||
} | ||
|
||
export function f32ToI32(f32: number): number { | ||
return new Int32Array(new Float32Array([f32]).buffer)[0]; | ||
} | ||
|
||
export function ulpsDist(x: number, y: number): number { | ||
if (x === y) return 0; | ||
|
||
if (isNaN(x) || isNaN(y)) return epsilon; | ||
if (!isFinite(x) || !isFinite(y)) return epsilon; | ||
|
||
const ix = f32ToI32(x); | ||
const iy = f32ToI32(y); | ||
|
||
if ((ix < 0) !== (iy < 0)) return epsilon; | ||
|
||
return ix > iy ? ix - iy : iy - ix; | ||
} |