JavaScript Float Precision Issue
This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Floating Point Precision and improve your technical interview readiness.
console.log(0.1 + 0.2 === 0.3)
Detailed Explanation
Why This Question Matters
If you've ever spent three hours debugging a financial calculation or a game engine's collision detection only to find out that 0.1 + 0.2 doesn't actually equal 0.3 in JavaScript, you aren't alone. This is a rite of passage for every JS developer.
At first glance, the question seems like a trick. We spent years in grade school learning basic arithmetic, so our brains tell us the answer should be true. But JavaScript (and almost every other modern language) doesn't handle decimals the way humans do. Understanding why this happens is the difference between writing code that "mostly works" and writing code that is actually reliable.
Understanding the Code
Let's look at the snippet:
On the surface, it's a simple comparison. But under the hood, JavaScript is using the IEEE 754 standard for floating-point arithmetic.
Here is the problem: computers don't speak "decimal." They speak binary. While an integer like 5 is easy to represent in binary, fractions are a different story. Some decimals, like 0.1 or 0.2, cannot be represented perfectly in binary. They become repeating fractions—similar to how 1/3 becomes 0.3333... in base 10.
Because the computer has a finite amount of memory (64 bits for a number in JS), it eventually has to cut that repeating sequence off. This creates a tiny rounding error.
When you add 0.1 and 0.2, you aren't adding the exact numbers you see on the screen. You're adding two slightly imprecise approximations. The result is:
Since 0.30000000000000004 is not exactly 0.3, the strict equality operator (===) returns false.
Finding the Correct Answer
The correct answer is Option B: false.
Why not true? Because the CPU isn't "thinking" in base 10. It's performing binary addition on the approximations of these numbers.
If you want to see this in action yourself, try running this in your browser console:
The === operator checks for absolute equality. Even a difference of 0.00000000000000004 is enough to make the expression fail.
Common Mistakes Developers Make
The biggest mistake is assuming that Number in JavaScript is a "decimal" type. It isn't. Every number in JS is a 64-bit float.
Beginners often try to fix this by using toFixed(), like this:
The problem here is that toFixed() returns a string, not a number. Comparing strings is slower and can lead to other bugs if you forget to cast the type back to a number later.
Another common trap is using Math.round() without considering the precision needed. If you're dealing with very small numbers, rounding to the nearest integer is useless.
Real-World Usage
You might think, "Who cares about a tiny fraction?" In a simple Todo app, you don't. But in production-grade engineering, this is a nightmare.
1. E-commerce and Payments
Never, ever use floating-point numbers for currency. If you're calculating taxes or totals for a shopping cart, those tiny rounding errors accumulate. Over thousands of transactions, you'll end up with missing cents or "phantom" money.
*The fix:* Work in cents (integers). Instead of $10.99, store 1099. Only convert back to decimals when displaying the value to the user.
2. Game Development
In a physics engine, if you check if a character's position is *exactly* 0.3 to trigger an event, the event might never fire because the character is actually at 0.30000000000000004.
3. Data Visualization
When plotting points on a graph, floating point errors can cause "gaps" or "overlaps" in your rendering logic if you rely on strict equality.
Key Takeaways
To handle this in the real world, stop using === for floating-point numbers. Instead, use a small value called epsilon (the smallest interval between two numbers).
If the difference between your result and your target is smaller than epsilon, you can consider them "equal enough."
The cheat sheet for your next project:
- Avoid === with decimals.
- Use integers for money.
- Use Number.EPSILON for precision comparisons.
- Remember that binary isn't perfect, and neither is JavaScript's math.
Why this matters
Understanding Floating Point Precision is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.