Leaderboard
Javascript Jul 4, 2026
Javascript Floating Point 01 Plus 07 Precision

JavaScript Floating Point — What does this print?

This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Floating Point 01 Plus 07 Precision and improve your technical interview readiness.

console.log(0.1 + 0.7)
A 0.8
B 0.7999999999999999
C 0.8000000001
D NaN

Detailed Explanation

Why This Question Matters

If you’ve been coding in JavaScript for a while, you’ve probably hit a moment where the math just... didn't add up. You write a simple addition, you expect a clean decimal, and instead, JavaScript hands you something like 0.7999999999999999.

It feels like a bug. It feels like the language is broken. But it's actually not a JS bug—it's a fundamental way computers handle numbers.

This specific challenge (0.1 + 0.7) is a rite of passage for junior developers. Understanding why this happens is the difference between a dev who just copies snippets from StackOverflow and one who actually understands how the engine under the hood works. If you don't get this, you'll eventually ship a bug to production where a payment calculation is off by a penny, and your users will be very unhappy.

Understanding the Code

The code is as simple as it gets:

console.log(0.1 + 0.7);

In a perfect world, the output is 0.8. In the real world, the output is 0.7999999999999999.

To understand why, we have to talk about IEEE 754. That's the technical standard for floating-point arithmetic that JavaScript (and Python, Java, C++, and most other languages) uses.

Computers don't see numbers the way we do. We use base-10 (decimal). Computers use base-2 (binary). While integers (like 1, 2, 100) translate perfectly into binary, fractions are a different story.

Think about it like this: in base-10, if you try to write $1/3$ as a decimal, you get $0.3333...$ forever. You can never write it perfectly because the base-10 system can't represent that fraction exactly.

The same thing happens in binary with numbers like 0.1 and 0.7. To a computer, these numbers are repeating decimals. Since the computer has a finite amount of memory (64 bits for a number in JS), it eventually has to "cut off" the number and round it.

When you add two numbers that have already been slightly rounded, those tiny errors compound. That's how you end up with 0.7999999999999999 instead of a clean 0.8.

Finding the Correct Answer

In the challenge, the correct answer is Option B (which represents the floating-point result).

Here is why the other options are wrong:
- Option A (0.8): This is the "human" answer. It's what we expect mathematically, but it ignores how hardware actually processes decimals.
- Option C (NaN or Error): Adding two numbers is a valid operation. There's no reason for the engine to throw an error or return "Not a Number."

The correct answer is the one that reflects the precision error. When you run this in a Chrome console or Node.js, you'll see that the result isn't exactly 0.8 because the binary approximation of 0.1 plus the binary approximation of 0.7 doesn't land exactly on the binary approximation of 0.8.

Common Mistakes Developers Make

The biggest mistake juniors make is trusting the == operator with floating-point numbers.

Imagine you're building a game or a shopping cart:

let total = 0.1 + 0.7;

if (total === 0.8) {
console.log("Payment successful!");
} else {
console.log("Error: Amount mismatch.");
}


This code will hit the else block every single time. The total is 0.7999999999999999, and 0.8 is 0.8. They aren't equal.

Another common mistake is trying to "fix" this by using toFixed(2). While toFixed helps with displaying the number to a user, it returns a string, not a number. If you try to do more math with that result, you'll end up with weird string concatenation bugs.

Real-World Usage

How do we actually handle this in production? You can't just hope the math works out.

1. Use Integers (The Cent Pattern)
This is the industry standard for financial apps. Never store money as decimals. Store everything in the smallest unit (cents).
Instead of $0.10 + $0.70, do 10 + 70 = 80. Then, only divide by 100 when you need to display the value to the user.

2. Use a Tolerance (Epsilon)
If you're doing scientific calculations or game physics, you don't check if two numbers are equal. You check if the difference between them is "small enough."

const result = 0.1 + 0.7;
const expected = 0.8;

if (Math.abs(result - expected) < Number.EPSILON) {
console.log("Close enough to be equal!");
}


Number.EPSILON is a built-in JS constant that represents the smallest possible difference between two numbers.

3. Specialized Libraries
For heavy-duty math, use libraries like Big.js or Decimal.js. These libraries handle the math in a way that avoids binary floating-point issues entirely.

Key Takeaways

- JavaScript uses the IEEE 754 standard, meaning it handles numbers as 64-bit floats.
- Many decimals cannot be represented exactly in binary, leading to rounding errors.
- 0.1 + 0.7 results in 0.7999999999999999 because of these precision gaps.
- Never compare floating-point numbers using ===.
- For money, work with integers (cents) or use a dedicated math library.

Why this matters

Understanding Floating Point 01 Plus 07 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.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →