{"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,"search_vec":"'1.1.0':52 '2013':55 '32':85 '64':90 'absolut':24 'absolutetoler':100 'address':29 'almost':1,5,138 'almost-equ':4 'applic':119 'arithmet':38 'b':21,46,98,103 'bit':86,91 'cadenc':67 'check':129 'clear':95 'codebas':62 'compar':13,134 'comparison':110,123 'configur':23 'constant':92 'control':107 'critic':125 'current':48 'dbl':88 'differenti':78 'direct':43,127 'doubl':135 'epsilon':84,89 'equal':2,6,44,128,137 'exact':121 'financi':118 'float':16,36,122,133 'floating-point':15,35 'flt':83 'focus':69 'formula':96 'frequent':75 'indic':56 'inher':31 'issu':33 'javascript':132 'logic':111 'make':112 'matur':59 'max':99 'min':101 'minim':65 'near':139 'number':18 'offer':81 'origin':72 'packag':7 'point':17,37 'precis':32,106 'primarili':68 'provid':8 'purpos':73 'relat':26 'relativetoler':104 'releas':53,66 'reli':41 'robust':10 'round':136 'scientif':116 'stabl':49,61 'suitabl':114 'toler':27,140 'two':14 'unreli':131 'updat':76 'util':11 'version':50 'without':74","created_at":"2026-04-20T01:51:08.605575+00:00","updated_at":"2026-04-20T01:51:08.605575+00:00","problems":[],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/mikolalysenko/almost-equal","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/almost-equal","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}