IEEE 754 is an approximate representation of floating-point numbers; therefore, floating-point numbers should not be directly compared for equality.
Programmers are probably familiar with variations of this head-scratcher:
let f1 = 0.1 + 0.2; let f2 = 0.3; console.log(f1 === f2); // 'false'
Comparisons of IEEE 754 floating-point numbers must account for ε (epsilon), the maximum relative error in floating point numbers (defined as "the difference between 1 and the smallest floating-point number greater than 1").
Number object (
To account for ε and correctly compare two floating-point numbers for equality, subtract one number from the other and ensure the difference is smaller than ε.
let f1 = 0.1 + 0.2; let f2 = 0.3; console.log(Math.abs(f1 - f2) < Number.EPSILON); // 'true'
(The comparison should be strict less-than, not lte, by the definition of ε:
1.0 + ε ≠ 1.0 in IEEE 754 representation.)