Floating-Point Precision Error
Quick summary
Your decimal calculation gives a result like 0.30000000000000004 instead of 0.3 — a fundamental limitation of floating-point math.
Why this happens
Some decimal fractions cannot be represented exactly in binary
Accumulated rounding errors in repeated calculations
Comparing floating-point values with ===
Minimal example
console.log(0.1 + 0.2 === 0.3); // false!
console.log(0.1 + 0.2); // 0.30000000000000004const result = 0.1 + 0.2;
console.log(Math.abs(result - 0.3) < 1e-10); // true
console.log(Math.round(result * 100) / 100); // 0.30.1 and 0.2 cannot be represented exactly in binary, so their sum has a tiny error. Using a tolerance comparison or rounding fixes the issue.
How to diagnose
Print the result with full precision to see the tiny error
Check if comparisons fail due to tiny differences
Use Number.EPSILON to understand the precision limit
How to fix
Round results to a fixed number of decimal places: Math.round(x * 100) / 100
Use a tolerance for comparisons: Math.abs(a - b) < 0.0001
Use integer arithmetic or a decimal library for financial calculations
How to prevent
Never compare floats with === — use a tolerance check
Round results before displaying them to users
Related Resources
Related Glossary
Related Lessons
- Numbers and Math
Learn about floating-point limitations.