@lvlte/ulp 1.0.0 → 1.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Eric Lavault (https://github.com/lvlte)
3
+ Copyright (c) 2025 Eric Lavault <lvlte.code@gmail.com> (https://github.com/lvlte)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -12,12 +12,14 @@ export declare const FLOAT64_MIN: number;
12
12
  */
13
13
  export declare function eps(x?: number): number;
14
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.
15
+ * @borrows eps as ulp
16
+ */
17
+ export declare const ulp: typeof eps;
18
+ /**
19
+ * Exponent of a normalized floating-point number x.
19
20
  *
20
21
  * @param x The input number
21
- * @returns The largest integer `y` such that `2^y ≤ x`.
22
+ * @returns The largest integer `y` such that `2^y ≤ |x|`, or `NaN` if x is not
23
+ * a finite number / if x is ±0.
22
24
  */
23
25
  export declare function exponent(x: number): number;
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FLOAT64_MIN = void 0;
3
+ exports.ulp = exports.FLOAT64_MIN = void 0;
4
4
  exports.eps = eps;
5
5
  exports.exponent = exponent;
6
6
  const modf_1 = require("@lvlte/modf");
@@ -11,11 +11,18 @@ function eps(x = 1) {
11
11
  if (x <= exports.FLOAT64_MIN) {
12
12
  return Number.MIN_VALUE;
13
13
  }
14
- return Math.pow(2, (exponent(x) - 52));
14
+ return Math.pow(2, (_exponent(x) - 52));
15
15
  }
16
16
  return NaN;
17
17
  }
18
+ exports.ulp = eps;
18
19
  function exponent(x) {
20
+ if (Number.isFinite(x) && x !== 0) {
21
+ return _exponent(Math.abs(x));
22
+ }
23
+ return NaN;
24
+ }
25
+ function _exponent(x) {
19
26
  const [ipart, fpart] = (0, modf_1.modf)(x);
20
27
  if (ipart > 0) {
21
28
  return ipart.toString(2).split('.', 1)[0].length - 1;
package/dist/index.d.ts CHANGED
@@ -12,12 +12,14 @@ export declare const FLOAT64_MIN: number;
12
12
  */
13
13
  export declare function eps(x?: number): number;
14
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.
15
+ * @borrows eps as ulp
16
+ */
17
+ export declare const ulp: typeof eps;
18
+ /**
19
+ * Exponent of a normalized floating-point number x.
19
20
  *
20
21
  * @param x The input number
21
- * @returns The largest integer `y` such that `2^y ≤ x`.
22
+ * @returns The largest integer `y` such that `2^y ≤ |x|`, or `NaN` if x is not
23
+ * a finite number / if x is ±0.
22
24
  */
23
25
  export declare function exponent(x: number): number;
package/dist/index.js CHANGED
@@ -6,11 +6,18 @@ export function eps(x = 1) {
6
6
  if (x <= FLOAT64_MIN) {
7
7
  return Number.MIN_VALUE;
8
8
  }
9
- return Math.pow(2, (exponent(x) - 52));
9
+ return Math.pow(2, (_exponent(x) - 52));
10
10
  }
11
11
  return NaN;
12
12
  }
13
+ export const ulp = eps;
13
14
  export function exponent(x) {
15
+ if (Number.isFinite(x) && x !== 0) {
16
+ return _exponent(Math.abs(x));
17
+ }
18
+ return NaN;
19
+ }
20
+ function _exponent(x) {
14
21
  const [ipart, fpart] = modf(x);
15
22
  if (ipart > 0) {
16
23
  return ipart.toString(2).split('.', 1)[0].length - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvlte/ulp",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Compute the unit in last place of a given IEEE-754 64-bit number",
5
5
  "license": "MIT",
6
6
  "author": "Eric Lavault <lvlte.code@gmail.com>",