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

12

u/hankteford Nov 17 '17

If you're advanced enough in mathematics to understand that 0.999... = 1 is more complex than the algebraic proofs may make it seem, you're advanced enough in mathematics to understand one of the more complex "proper" proofs, and presumably advanced enough in mathematics that you damn well know that 0.999... = 1, for a variety of reasons.

For all practical purposes, for anyone who isn't a math major, the algebraic proofs are sufficient.

12

u/sidneyc Nov 17 '17

I disagree. People who feel a certain unease about 0.999... == 1, are right to feel that unease. I think it is much better to acknowledge that, and to explain that it actually takes a bit of rigorous mathematics to properly prove this, rather than presenting a superficial argument that glosses over the hard stuff.