@lvlte/ulp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Eric Lavault (https://github.com/lvlte)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The smallest positive normal number representable by IEEE-754 float64.
3
+ */
4
+ export declare const FLOAT64_MIN: number;
5
+ /**
6
+ * Return the unit in the last place or unit of least precision (ulp) of x, that
7
+ * is, the distance between two consecutive representable floating-point numbers
8
+ * at x.
9
+ *
10
+ * @param x The input number (default: 1)
11
+ * @returns The ulp of x, or `NaN` if `x` is not a finite number.
12
+ */
13
+ export declare function eps(x?: number): number;
14
+ /**
15
+ * Exponent of a normalized floating-point number x (`Math.log2()` not precise
16
+ * enough for large numbers).
17
+ *
18
+ * NB. This function assumes `x` is a finite, strictly positive number.
19
+ *
20
+ * @param x The input number
21
+ * @returns The largest integer `y` such that `2^y ≤ x`.
22
+ */
23
+ export declare function exponent(x: number): number;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FLOAT64_MIN = void 0;
4
+ exports.eps = eps;
5
+ exports.exponent = exponent;
6
+ const modf_1 = require("@lvlte/modf");
7
+ exports.FLOAT64_MIN = Math.pow(2, -1022);
8
+ function eps(x = 1) {
9
+ if (Number.isFinite(x)) {
10
+ x = Math.abs(x);
11
+ if (x <= exports.FLOAT64_MIN) {
12
+ return Number.MIN_VALUE;
13
+ }
14
+ return Math.pow(2, (exponent(x) - 52));
15
+ }
16
+ return NaN;
17
+ }
18
+ function exponent(x) {
19
+ const [ipart, fpart] = (0, modf_1.modf)(x);
20
+ if (ipart > 0) {
21
+ return ipart.toString(2).split('.', 1)[0].length - 1;
22
+ }
23
+ return -(fpart.toString(2).split('1', 1)[0].length - 1);
24
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The smallest positive normal number representable by IEEE-754 float64.
3
+ */
4
+ export declare const FLOAT64_MIN: number;
5
+ /**
6
+ * Return the unit in the last place or unit of least precision (ulp) of x, that
7
+ * is, the distance between two consecutive representable floating-point numbers
8
+ * at x.
9
+ *
10
+ * @param x The input number (default: 1)
11
+ * @returns The ulp of x, or `NaN` if `x` is not a finite number.
12
+ */
13
+ export declare function eps(x?: number): number;
14
+ /**
15
+ * Exponent of a normalized floating-point number x (`Math.log2()` not precise
16
+ * enough for large numbers).
17
+ *
18
+ * NB. This function assumes `x` is a finite, strictly positive number.
19
+ *
20
+ * @param x The input number
21
+ * @returns The largest integer `y` such that `2^y ≤ x`.
22
+ */
23
+ export declare function exponent(x: number): number;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ import { modf } from '@lvlte/modf';
2
+ export const FLOAT64_MIN = Math.pow(2, -1022);
3
+ export function eps(x = 1) {
4
+ if (Number.isFinite(x)) {
5
+ x = Math.abs(x);
6
+ if (x <= FLOAT64_MIN) {
7
+ return Number.MIN_VALUE;
8
+ }
9
+ return Math.pow(2, (exponent(x) - 52));
10
+ }
11
+ return NaN;
12
+ }
13
+ export function exponent(x) {
14
+ const [ipart, fpart] = modf(x);
15
+ if (ipart > 0) {
16
+ return ipart.toString(2).split('.', 1)[0].length - 1;
17
+ }
18
+ return -(fpart.toString(2).split('1', 1)[0].length - 1);
19
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@lvlte/ulp",
3
+ "version": "1.0.0",
4
+ "description": "Compute the unit in last place of a given IEEE-754 64-bit number",
5
+ "license": "MIT",
6
+ "author": "Eric Lavault <lvlte.code@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/lvlte/ulp"
10
+ },
11
+ "type": "module",
12
+ "main": "./dist/cjs/index.js",
13
+ "exports": {
14
+ ".": {
15
+ "import": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "require": {
20
+ "types": "./dist/cjs/index.d.ts",
21
+ "default": "./dist/cjs/index.js"
22
+ }
23
+ }
24
+ },
25
+ "engines": {
26
+ "node": ">=18.20"
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "test": "jest",
33
+ "prebuild": "rimraf dist && make-dir dist",
34
+ "build": "tsc && tsc -p tsconfig-cjs.json",
35
+ "postbuild": "(echo { && echo \"type\": \"commonjs\" && echo }) > ./dist/cjs/package.json",
36
+ "prepublishOnly": "npm test && npm run build && tsc --emitDeclarationOnly true --removeComments false && tsc -p tsconfig-cjs.json --emitDeclarationOnly true --removeComments false"
37
+ },
38
+ "keywords": [
39
+ "number",
40
+ "epsilon",
41
+ "function",
42
+ "precision",
43
+ "eps",
44
+ "ulp",
45
+ "float",
46
+ "float64",
47
+ "IEEE-754",
48
+ "rounding",
49
+ "tolerance",
50
+ "compare",
51
+ "equal",
52
+ "ULP"
53
+ ],
54
+ "devDependencies": {
55
+ "@lvlte/tsconfig": "^1.0.1",
56
+ "@types/jest": "^29.5.12",
57
+ "jest": "^29.7.0",
58
+ "make-dir-cli": "^4.0.0",
59
+ "rimraf": "^6.0.1",
60
+ "ts-jest": "29.2.5",
61
+ "typescript": "^5.9.2"
62
+ },
63
+ "dependencies": {
64
+ "@lvlte/modf": "^1.1.0"
65
+ }
66
+ }
package/readme.md ADDED
@@ -0,0 +1,40 @@
1
+ # ulp (epsilon function)
2
+
3
+ > Compute the [unit of least precision](https://en.wikipedia.org/wiki/Unit_in_the_last_place)
4
+ of a given IEEE-754 64-bit number.
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ npm install @lvlte/ulp
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```js
15
+ // ESM
16
+ import { eps } from '@lvlte/ulp';
17
+ ```
18
+ ```js
19
+ // CJS
20
+ const { eps } = require('@lvlte/ulp');
21
+ ```
22
+ ```js
23
+ console.log(eps()); // 2.220446049250313e-16
24
+ console.log(eps() === eps(1)); // true
25
+ console.log(eps() === Number.EPSILON); // true
26
+
27
+ console.log(eps(Number.MAX_SAFE_INTEGER)); // 1
28
+ console.log(eps(Number.MAX_SAFE_INTEGER + 1)); // 2
29
+
30
+ console.log(eps(0)); // 5e-324
31
+ console.log(eps(0) === Number.MIN_VALUE); // true
32
+
33
+ console.log(eps(Infinity)); // NaN
34
+ ```
35
+
36
+ ## Use case
37
+
38
+ - Check whether two floating point numbers can be considered equal.
39
+ - Approximate a floating point number x as a rational number.
40
+ - Get the next/previous float on the float64 number line.