0

I tried to loop from 0.1 to 2.0 and then print the output to the console.. But I got strange output like these:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004

Source code:

public class test {
    public static void main(String[] a) {
        double i = 0.1;
        while (i < 2.1)
            System.out.println(i);
            i+=0.1;
        }
    }
}

Why it doesn't this print the exact numbers instead of having point like 0.79999999999? Also is ther any difference using for instead of while, since I dont know how to make 0.1 increment?

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
justmyfreak
  • 1,260
  • 3
  • 16
  • 30
  • 1
    Google digital representation of floating point numbers to learn why there is nothing strange going on here. This has nothing to do with Java and all to do with the inherent limitations of trying to represent a floating point number with discrete bits. – Hovercraft Full Of Eels Mar 23 '11 at 03:28
  • 1
    Never, ever use a float / double as an iterating variable. The exec time would be awfull. Replace it with an int (So the iteration would go fro 1 to 21 in your case, and in the loop you can get a double variable equal to (i/10.0) . As a side note for Hovercraft comment, read about the floating point mantissa, you'll understand why you loose accuracy as the number evolves. – Doodloo Mar 23 '11 at 03:28
  • 1
    obligatory link to [What every programmer should know about floating point](http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf). – Mark Elliot Mar 23 '11 at 03:30
  • Thank you for your answer.. I`ll read about floating point and mantisa..is this occur in java only or other programminglanguage? – justmyfreak Mar 23 '11 at 03:32
  • this is a common problem across all languages. In java there are classes like BigDecimal that were made to try to get a better representation of floating point numbers than floats, but the some numbers just can't be represented accurately. – MeBigFatGuy Mar 23 '11 at 03:44
  • possible duplicate of [Retain precision with Doubles in java.](http://stackoverflow.com/questions/322749/retain-precision-with-doubles-in-java) – Nishant Mar 23 '11 at 03:45
  • wonders how many times he's going to ask "does this occur only in Java" before he reads the links. – Hovercraft Full Of Eels Mar 23 '11 at 05:19

2 Answers2

4

This is normal. It's inherent in floating point; numbers like 0.3 can't be stored as exact values in binary, so you get slowly accumulating errors. References: Python manual, Wikipedia, Technical explanation from Princeton CS.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • I heard that this only occur in Java language. Is that true? – justmyfreak Mar 23 '11 at 03:29
  • @Tyo Oblivion - it's inherent to the representation of floating point numbers, not to any one language. – zellio Mar 23 '11 at 03:31
  • @Tyo: Hardly. Please read the links before making any more replies. You won't regret reading them. – Hovercraft Full Of Eels Mar 23 '11 at 03:31
  • @Tyo: Java defaults to showing the full precision, is all. The default format for printing floating point in C / C++ rounds off a little short of full precision, to avoid the *appearance* of issues; this has its own problems, because they're still there and can eventually accumulate to the point that they're visible even in the reduced precision. – geekosaur Mar 23 '11 at 03:34
0

That's a bug (on your side).

Do not use floating point numbers until you learn what they are and how they work.

Glen P
  • 719
  • 5
  • 10