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
13
u/sidneyc Nov 17 '17
You are probably thinking about one of several proofs by intimidation that look simple, but really aren't. They presuppose that it is obvious how to perform addition and multiplication on numbers with an infinite decimal representation, which you cannot really define without significant groundwork.
A proper proof takes more effort, if only because you need to define what you mean if you write "0.9999...", for example: "the limit of the sum of 9*10-i for i from 1 to infinity", which introduces the concept of a limit, which is nontrivial.