Number Constants: EPSILON
What you'll learn: You'll discover how Number.EPSILON represents the smallest meaningful difference between floating-point numbers in JavaScript.
The Problem with Floating-Point Math
Remember how JavaScript uses floating-point numbers? This means some decimal arithmetic doesn't work perfectly:
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
This happens because computers store numbers in binary, and some decimals can't be represented exactly. The tiny errors accumulate.
What is Number.EPSILON?
Number.EPSILON is the smallest positive number you can add to 1 and get a result different from 1. It represents the machine precision limit — the gap between representable numbers.
console.log(Number.EPSILON); // 2.220446049250313e-16
Think of it like the finest measuring tick on a ruler. You can't measure differences smaller than one tick mark.
Using EPSILON for Comparisons
Instead of checking if two floating-point results are exactly equal (which often fails), check if they're "close enough":
const a = 0.1 + 0.2;
const b = 0.3;
// Bad approach:
console.log(a === b); // false
// Good approach:
const difference = Math.abs(a - b);
console.log(difference < Number.EPSILON); // true
If the absolute difference between two numbers is smaller than EPSILON, they're effectively equal for practical purposes.
Why This Matters
Number.EPSILON gives you a scientific way to handle floating-point comparison errors. Instead of guessing at tolerances like 0.0001, you use the actual machine precision limit.
Key Takeaway: Number.EPSILON (approximately 2.22e-16) represents the smallest distinguishable difference between floating-point numbers, making it essential for reliable floating-point comparisons.