JS Built-ins
NumberEPSILONintermediate

Number.EPSILON β€” epsilon-based equality check

πŸ“– Usage Example
// Use EPSILON to compare floats safely
0.1 + 0.2 === 0.3   // β†’ false (floating-point issue)
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON   // β†’ true

Use Number.EPSILON as a tolerance threshold when comparing floating-point numbers that may differ by tiny rounding errors.

Epsilon Equality

Use Number.EPSILON to compare floats within a tolerance.

function almostEqual(a, b) {
  return Math.abs(a - b) < Number.EPSILON * Math.max(Math.abs(a), Math.abs(b), 1)
}

Challenge: Implement almostEqual(a, b).

πŸ’‘ Hints (1)
  • β€’ Multiply `EPSILON` by the magnitude of the numbers being compared for a relative tolerance.