{"id":12796,"library":"almost-equal","title":"Almost Equal","description":"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.","status":"maintenance","version":"1.1.0","language":"javascript","source_language":"en","source_url":"git://github.com/mikolalysenko/almost-equal","tags":["javascript","float","compare","double","round","equal","almost","near","tolerance"],"install":[{"cmd":"npm install almost-equal","lang":"bash","label":"npm"},{"cmd":"yarn add almost-equal","lang":"bash","label":"yarn"},{"cmd":"pnpm add almost-equal","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { almostEqual } from 'almost-equal';","symbol":"almostEqual","correct":"import almostEqual from 'almost-equal';"},{"note":"Constants like `FLT_EPSILON` are properties of the default `almostEqual` export, not directly named exports from the package root.","wrong":"import { FLT_EPSILON } from 'almost-equal';","symbol":"almostEqual.FLT_EPSILON","correct":"import almostEqual from 'almost-equal'; const epsilon = almostEqual.FLT_EPSILON;"},{"note":"Similar to `FLT_EPSILON`, `DBL_EPSILON` is accessed as a property of the default `almostEqual` export.","wrong":"import { DBL_EPSILON } from 'almost-equal';","symbol":"almostEqual.DBL_EPSILON","correct":"import almostEqual from 'almost-equal'; const epsilon = almostEqual.DBL_EPSILON;"}],"quickstart":{"code":"import almostEqual from 'almost-equal';\n\n// Define the numbers to compare. 'b' is slightly different from 'a',\n// representing a common floating-point precision scenario.\nconst a = 100;\nconst b = 100 + 1e-12;\n\nconsole.log('--- Comparing with explicit FLT_EPSILON ---');\nconsole.log(`a = ${a}, b = ${b}`);\n// Check if 'a' and 'b' are almost equal using single-precision (32-bit) float epsilon.\n// This uses a relatively small tolerance.\nconsole.log(`Are a and b almost equal (FLT_EPSILON)? ${almostEqual(a, b, almostEqual.FLT_EPSILON, almostEqual.FLT_EPSILON)}`);\n\nconsole.log('\\n--- Comparing with explicit DBL_EPSILON ---');\nconsole.log(`a = ${a}, b = ${b}`);\n// Check if 'a' and 'b' are almost equal using double-precision (64-bit) float epsilon.\n// This typically provides a larger, more forgiving tolerance.\nconsole.log(`Are a and b almost equal (DBL_EPSILON)? ${almostEqual(a, b, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON)}`);\n\nconsole.log('\\n--- Demonstrating with default tolerance (DBL_EPSILON) ---');\n// A classic floating-point addition error: 0.1 + 0.2 is not exactly 0.3\nconst c = 0.1 + 0.2; // Result is typically 0.30000000000000004\nconst d = 0.3;\nconsole.log(`c = ${c}, d = ${d}`);\n// Using 'almostEqual' with its default tolerance (DBL_EPSILON) should correctly identify them as 'almost equal'.\nconsole.log(`Are c and d almost equal (default DBL_EPSILON)? ${almostEqual(c, d)}`);\n","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Incorrectly 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Assuming `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"As 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.","severity":"gotcha","affected_versions":"<=1.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}