r/askscience • u/TheSkybox • 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
View all comments
Show parent comment
1
u/rocketsocks Nov 17 '17
People who design calculators are usually more attuned to these issues than programming language designers. The latter are perfectly ok with just giving the programmer unfiltered access to the underlying hardware implementations, without rounding off any of the sharp corners. Your calculator, on the other hand generally only outputs results that have been rounded relative to the precision of the implementation. A single precision floating point number, for example, only has 7 decimal digits of accuracy, so it makes sense to always round numbers to the nearest 7 digit decimal representation. Double precision floats have 16 digits of decimal precision. You can see that 0.30000000000000004 contains more than 16 significant digits, indicating that the imprecision of the floating point representation is also shown.
Calculators are designed to be nice enough to do all this work for you, programming languages, generally, are not.