9 Replies Latest reply: Nov 8, 2007 2:59 PM by 807603 RSS

    Double Precision Problems...

    807603
      Can anyone explain the output of the following code to me? I don't understand why there is a precision issue here. The desired result is what you would get if you used a calculator to increment 1.0 by 0.1 over and over again (i.e. 1.1, 1.2, 1.3, 1.4, 1.5...).

      Code:

      double a = 1.0;
      double b = 2.0;
      double i = 0.1;

      for (double n = a; a <= b; a += i) {
      System.out.println("a: " + a);
      }

      Output:

      a: 1.0
      a: 1.1
      a: 1.2000000000000002
      a: 1.3000000000000003
      a: 1.4000000000000004
      a: 1.5000000000000004
      a: 1.6000000000000005
      a: 1.7000000000000006
      a: 1.8000000000000007
      a: 1.9000000000000008

      I have pretty much the same problem if I use all floats also.
        • 1. Re: Double Precision Problems...
          807603
          yawn

          welcome to the world of floating point numbers
          • 2. Re: Double Precision Problems...
            807603
            It's just how the IEEE floating point standard works - many relatively simple numbers do not have an exact representation in the IEEE standard. You could display the numbers in a different format which would remove the annoying extra digits...
            import java.text.DecimalFormat;
            /...
            static DecimalFormat df = DecimalFormat("0.000");
            //loop
             System.out.println("a: " + df.format(a));
            Output should be:
            1.000
            1.100
            1.200
            etc.

            Edited by: practissum on Nov 8, 2007 12:08 PM
            • 3. Re: Double Precision Problems...
              807603
              http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf
              • 4. Re: Double Precision Problems...
                807603
                It's because 0.1 is periodic when you write it as a binary number. Try 0.5, 0.25, 0.125 and so on.
                • 5. Re: Double Precision Problems...
                  807603
                  You probably want to use the BigDecimal class rather than Double.
                  • 6. Re: Double Precision Problems...
                    807603
                    Thanks for the replies. I had suspected the problem was what practissum mentioned above: That there isn't an exact representation for those numbers (1.2, 1.3, etc) in the IEEE standard. But if that is the case, how do you explain the following:
                        double d0 = 1.1;
                        double d1 = 1.2;
                        double d2 = 1.3;
                        System.out.println("d0: " + d0);
                        System.out.println("d1: " + d1);
                        System.out.println("d2: " + d2);
                    Output:
                    d0: 1.1
                    d1: 1.2
                    d2: 1.3

                    So it's something that has to do with the arithmetic operations being done on the numbers. I expect this kind of thing for very small numbers, not things like 1.1.

                    It should also be noted that I'm not concerned with how the numbers look when I print them out. I'm concerned with the actual internal representation.

                    P.S. jToohey, thanks for the link. Working my way through that document.
                    • 7. Re: Double Precision Problems...
                      807603
                      It's not the 1.1, 1.2 that's the problem. It's the .1 that you summed repeatedly in your first post
                      • 8. Re: Double Precision Problems...
                        807603
                        mjs483 wrote:
                        I expect this kind of thing for very small numbers, not things like 1.1.
                        They don't have to be small. 0.1 doesn't give you exact results in your first example, while for example 0.03125 would be ok even after many additions. That's because 0.03125 has a non-periodic binary representation (0.00001) so its repeated use is safe.

                        Numbers like 1.2 or 1.3 are ok if you don't repeatedly add them to something.
                        • 9. Re: Double Precision Problems...
                          807603
                          Ah, Ok this makes things much clearer. Thanks for the help everyone.