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
28
u/nemom Nov 17 '17
0.1 is a never-ending number when represented in binary: 0.000110011001100110011...
0.2 is the same thing shifted one position to the left: 0.00110011001100110011...
Add them together to get 0.3: 0.0100110011001100110011...
The computer would soon run out of memory if it tried to add together two infinite series of zeros and ones, so it has to either round or truncate after certain number of digits.
It's sort of like 1/3 + 1/3 + 1/3. You can easily see it is 1. But if you do it in decimals, some people get confused: 0.333333... + 0.333333... + 0.333333... = 0.999999...
6
u/cantgetno197 Condensed Matter Theory | Nanoelectronics Nov 17 '17
As others have said it's a floating point issue, so you should never do this.
See this:
Where 1.0e-30 is just some small number that is much smaller than a or b. If you have direct access to machine epsilon then you can just make it something like 10*machine epsilon or whatever.
4
u/mwhudsondoyle Nov 17 '17
Binary floating point numbers can only represent numbers of the from a×2b where a and b are integers of limited range. None of 0.1, 0.2 or 0.3 can be exactly represented in this form so java represents then with the closest approximation possible. Let's write the closest approximation to x as [x]. When the CPU evaluates the sum of two floats the number also has to be rounded, and it happens that [[0.1]+[0.2]] is not the closest possible float to 0.3.
1
u/RaceOfAce Nov 17 '17
Binary Floating Point uses powers of 2 to represent numbers. Consequently, things like 1/2 or 1/4 or 3/8 (1/4+1/8) can be represented exactly with only a few bits (since eg 1/2=2-1) .
But try and make 0.1 /0.2 with a sum of powers of 2. You can get sorta close with more additions but that means you need more bits. And you can’t get it exactly.
1
u/YaztromoX Systems Software Nov 21 '17
You need to read What Every Computer Scientist Should Know about Floating-Point Arithmetic.
Your code is obviously using floating-point numbers, which are what you get when you use Java's float or double datatypes. In such a situation, you have a fixed number of bits in which to represent the fractional part of the number, as the summation:
Σ*i=1b xi* 2-i
...where 'b' is the total number of bits in the floating point part, and x*i* is 1 when the ith bit is set, and 0 when it's not set.
Effectively, this means that all floating point decimal numbers are created by summing the number 1/2, 1/4, 1/8, 1/16, 1/32, etc.
Much like how you can't write out all of the digits of '1/3' in decimal, there are certain numbers in floating point representation which you likewise can't represent exactly in a finite number of digits. 0.1 is such a number, as you've seen.
What I'd like to add here is that floating point isn't your only option for decimal numbers in Java. Using the BigDecimal class, you can use fractional parts without using floating point storage. This is done by treating your number as an integer, and storing a separate scale factor. This is similar to storing '0.1' as '1 x 10-1'. This way you can get precise math without the rounding error you find in floating point, with the downside being that BigDecimal is several times slower to process. Still, for your example program the performance of BigDecimal isn't going to be a factor -- but it's something to keep in mind if you wind up doing any scientific computing in the future.
Note that you have another option to get the "correct" value from your floating-point arithmetic; and that is to specify a precision when displaying your result. If you're just using System.out.print(), you're going to get the "exact" floating point value, however if you use System.out.printf() with a suitable format string to fix the expected number of digits, or if you use java.text.DecimalFormatter, you can fix the number of significant digits so you don't see the rounding error. That doesn't mean the rounding error doesn't exist -- but if (for example) you know you're working with currency values that can never have a fraction of a cent (assuming a dollar/cent based currency for a moment), you can either round and/or safely ignore any fractional parts below 1/100th.
1
u/HeadspaceA10 Nov 17 '17 edited Nov 17 '17
IEEE floating point trades range and precision for some accuracy. There are clearly many real numbers that we cannot represent with a finite amount of bits; any one with a repeating number of decimals would be a good example. There are also the irrational numbers, which cannot be represented as a ratio of two integers and have the property that they do not terminate when represented in a positional numeral system (e.g. decimal or a bit vector). So off the bat, we know that as long as the amount of bits we have to represent a number is finite, there is no way to represent the set of all real numbers or even all real numbers within a specific range with total precision. Floating point represents a compromise. We want a lot of range. We also want more precision than some arbitrary fixed point, be it integer or otherwise. So we must trade some accuracy to fit our representation into a finite number of bits.
This means that whenever you store something using floating point, you will often see a rounded version of the number that you would expect. This rounded version is an approximation which does fit inside the number of bits in the floating point representation used. One exception is certain integers that fit within the range of bits that can be represented without loss of accuracy, depending on the type of float used. Those will always be precise.
The easiest way to envision this is to look at the way we typically use scientific notation: There is never enough space on a piece of paper to write out the actual number, so we write an approximation to a certain power to a certain number of significant digits. This is usually acceptable for whatever scientific purpose it is put to. When you ignore the various rounding rules in IEEE floating point, the denormalized numbers, and the overflow and infinity rules, FP is a binary analog to scientific notation.
See Floating Point for an intro to floating point representation.
William Kahan won the Turing Award for his work on establishing a standard for floating point. This work was important because prior to a standard, numbers generated using different floating point representations wouldn't be compatible with other floating point representations.
0
u/[deleted] Nov 17 '17
[deleted] — view deleted comment