Registry / testing / almost-equal

almost-equal

JSON →
library1.1.0jsnpmunverified

The `almost-equal` package provides a robust utility for comparing two floating-point numbers (`a` and `b`) with configurable absolute and relative tolerances. It addresses the inherent precision issues in floating-point arithmetic by not relying on direct equality (`a === b`). The current stable version is 1.1.0, released in 2013, indicating a very mature and stable codebase with a minimal release cadence, primarily focusing on its original purpose without frequent updates. It differentiates itself by offering both `FLT_EPSILON` (32-bit) and `DBL_EPSILON` (64-bit) constants and a clear formula `|a - b| < max(absoluteTolerance, min(|a|, |b|) * relativeTolerance)` for precise control over the comparison logic, making it suitable for scientific and financial applications where exact float comparison is critical but direct equality checks are unreliable.

npm install almost-equal
INSTALL
IMPORT
SIG · ALMOST-EQUAL
A
almost-equal
testingjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

almostEqual
import almostEqual from 'almost-equal';
import { almostEqual } from 'almost-equal';
The package primarily exports `almostEqual` as a CommonJS default export. For ESM, use a default import. Named imports like `{ almostEqual }` are incorrect as it's not a named export.
almostEqual.FLT_EPSILON
import almostEqual from 'almost-equal'; const epsilon = almostEqual.FLT_EPSILON;
import { FLT_EPSILON } from 'almost-equal';
Constants like `FLT_EPSILON` are properties of the default `almostEqual` export, not directly named exports from the package root.
almostEqual.DBL_EPSILON
import almostEqual from 'almost-equal'; const epsilon = almostEqual.DBL_EPSILON;
import { DBL_EPSILON } from 'almost-equal';
Similar to `FLT_EPSILON`, `DBL_EPSILON` is accessed as a property of the default `almostEqual` export.

This quickstart demonstrates comparing two floating-point numbers using `almostEqual` with both `FLT_EPSILON` and `DBL_EPSILON` for explicit tolerance settings, and also shows the default `DBL_EPSILON` tolerance for common floating-point inaccuracies.

import almostEqual from 'almost-equal'; // Define the numbers to compare. 'b' is slightly different from 'a', // representing a common floating-point precision scenario. const a = 100; const b = 100 + 1e-12; console.log('--- Comparing with explicit FLT_EPSILON ---'); console.log(`a = ${a}, b = ${b}`); // Check if 'a' and 'b' are almost equal using single-precision (32-bit) float epsilon. // This uses a relatively small tolerance. console.log(`Are a and b almost equal (FLT_EPSILON)? ${almostEqual(a, b, almostEqual.FLT_EPSILON, almostEqual.FLT_EPSILON)}`); console.log('\n--- Comparing with explicit DBL_EPSILON ---'); console.log(`a = ${a}, b = ${b}`); // Check if 'a' and 'b' are almost equal using double-precision (64-bit) float epsilon. // This typically provides a larger, more forgiving tolerance. console.log(`Are a and b almost equal (DBL_EPSILON)? ${almostEqual(a, b, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)}`); console.log('\n--- Demonstrating with default tolerance (DBL_EPSILON) ---'); // A classic floating-point addition error: 0.1 + 0.2 is not exactly 0.3 const c = 0.1 + 0.2; // Result is typically 0.30000000000000004 const d = 0.3; console.log(`c = ${c}, d = ${d}`); // Using 'almostEqual' with its default tolerance (DBL_EPSILON) should correctly identify them as 'almost equal'. console.log(`Are c and d almost equal (default DBL_EPSILON)? ${almostEqual(c, d)}`);
Debug
Known issues
gotchaIncorrectly choosing `absoluteTolerance` and `relativeTolerance` values can lead to unexpected comparison results, especially when numbers are extremely small (near zero) or very large. Solely relying on `absoluteTolerance` for large numbers or `relativeTolerance` for numbers near zero may produce false positives or negatives.
fix
Carefully review the `almostEqual` formula (`|a - b| < max(absoluteTolerance, min(|a|, |b|) * relativeTolerance)`) and select appropriate values based on the expected magnitude and scale of numbers being compared. For general cases, `almostEqual.DBL_EPSILON` for both tolerances is a common starting point, but domain-specific requirements may necessitate fine-tuning.
affects: >=1.0.0
gotchaAssuming `almostEqual.FLT_EPSILON` and `almostEqual.DBL_EPSILON` are universal, fixed 'small numbers' for all scenarios. These constants are specific to the IEEE 754 standard's machine epsilon for 32-bit and 64-bit floating points respectively, and are best used as a baseline for relative error, not necessarily as absolute thresholds for all domain-specific tolerances.
fix
Understand that while these epsilon constants are useful defaults for relative tolerance, specific applications (e.g., high-precision scientific computing, financial calculations) often require custom absolute or relative tolerances tailored to the problem domain. Do not substitute these constants for explicit, problem-specific error bounds.
affects: >=1.0.0
gotchaAs an older CommonJS package, direct usage of `almost-equal` in a pure ESM Node.js environment or certain modern bundler configurations without proper interoperability setup might result in import errors or require specific workarounds.
fix
Ensure your build tooling (e.g., Webpack, Rollup, esbuild, Babel) is configured to handle CommonJS modules within an ESM project. In Node.js ESM, you might need to use `import pkg from 'almost-equal'; const almostEqual = pkg.default || pkg;` or adjust your `tsconfig.json` (for TypeScript) or `package.json` (`type: module`) settings for CJS interoperability.
affects: <=1.1.0
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
24
Amazon
1
Resources