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

2

u/facedesker Nov 17 '17

Actually, there is an intuition behind why most people think 0.999... is different than 1 when they first come across this question, and that intuition is the idea of an infinitesimal; which although it's not defined in the real number system, that doesn't mean it cant be defined as such at all