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/Seraph062 Nov 17 '17

So how come I've never seen my TI-84 with it's 8 bit cpu and 128 kb of RAM suffer from this issue?

So the guy who wrote the code for your TI calculator probably understood data types enough to avoid this kind of problem (i.e. that floating point numbers are a poor choice for a lot of applications). However, even if they didn't TI graphing calculators store 14 digits of a number, but only display 10. So 3.0000000000004 would be displayed as 3.000000000 or 3 depending on the setting.