DEV Community

Sneha Makwana
Sneha Makwana

Posted on

Your code is correct. But!! Your computer is lying..🤯

Why! Why!

Have you ever seen something in your code that made your brain go, "Wait.....WHAT? while a hundred possible explanations started running through your head? Is my computer a GHOST? 🤯

I recently got this feeling. I wrote something that look completely reasonable. Yeah! I am a coder, having a very good experience. But, once in a blue moon, I got something weird things that blown my mind and just hate the computer system with why! why!

I wrote this on my computer when I was just playing with numbers in java.

</> Java
System.out.println(0.1 + 0.2);
Enter fullscreen mode Exit fullscreen mode

My brain expected

0.3

My computer had other plans..

0.30000000000000004

For a second, I thought...

Okay, What did I break? (baffled programmer in skibidi) Hahaha!

Nothing.
My code was correct.
The computer wasn't broken either.

The problem was my assumption about what 0.1 actually means to a computer.

Computers don't really understand 0.1

Humans use decimal numbers:

0.1
0.2
0.5

Computers work with binary:

0s and 1s

And here's the annoying part:

Some decimal fractions simply cannot be represented exactly in binary.
It's a bit like trying to represent 1/3 using only two decimal places.

0.33

It's close.
But it's not actually 1/3.

Floating-point numbers have a simmilar problem.
So, when you write

</> Java
0.1
Enter fullscreen mode Exit fullscreen mode

the computer stores the closest representable binary value, not necessarily the exact mathematical value that you had in your head and Java print that value quite different than expectations.

So... is the computer or programming language lying?

Not really.

It's being painfully honest.

The lie happened earlier.
We assumed...

0.1 in our program = exactly 0.1 in the computer.

That's the trap.

And this isn't just a funny Java trick
This matters in real software.

Imagine you are building:

  • a banking system
  • a payment application
  • an inventory system
  • a billing platform
  • a trading system

Would you want this?

</> Java

double price = 0.1;
double quantity = 3;

System.out.println(price * quantity);
Enter fullscreen mode Exit fullscreen mode

Maybe not.

For financial calculations, using floating-point numbers without understanding their limitations can cause very real problems.

That's why developers often use decimal-oriented representations such as Java's:

</> Java
BigDecimal

When exact decimal arithmatic matters.

The bigger lession

This isn't really story about 01 + 0.2.
It's about something much bigger:

Your program can be completely correct and still produce a result you didn't expect. (Usually this happens in a critical situations like pick time of project delivery... Haha!)

Because software doesn't operate on our assumptions.

It operates on:

representations -> rules -> limitations -> hardware.

And sometimes the weirdest bugs aren't caused by bad code. Is it so?** (Comment with 'Your situation' If I hit a pitch here.)**

They're caused by a perfectly reasonable human assumtopn about how computers work.

One last question please!

What's a computer behavior that looked completely wrong to you the first time you encountered it?

Floating point? Time zones? null? Threads? Memory? stuck at production release? Something else?

Drop it in the comments.
Curious and I'm collecting examples for the next "Wait... why does the computer do THAT?" Am I special? 😄

Top comments (0)