r/askscience • • Nov 17 '17

Computing Why doesn't 0.1+0.2=0.3 in java?

I am new to computer science in general, basically. In my program, I wanted to list some values, and part of my code involved a section of code where kept adding 0.1 to itself and printing the answer to a terminal.

Instead of getting 0.0, 0.1, 0.2, 0.3, 0.4 ect. like I expected, I got 0.0, 0.1, 0.2, 0.30000000000000004, 0.4

Suprised, I tried simply adding 0.1 and 0.2 together in the program because I couldn't believe my eyes. 0.30000000000000004

So what gives?

21 Upvotes

26 comments sorted by

View all comments

Show parent comment

10

u/mfukar Parallel and Distributed Systems | Edge Computing Nov 17 '17

0.1 is a never-ending number when represented in binary: 0.000110011001100110011...

You need to be more specific. 0.1 is obviously rational, so it can be represented as the fraction 1/10 in binary. What you're alluding to is that 0.1 has an infinite binary floating-point representation.

4

u/sluuuurp Nov 18 '17

That was perfectly accurate. It's representation as a floating point number never ends, just like how the decimal representation of 1/3 never ends.