Question re Java number precision

Discussion in 'Java' started by inspired, Nov 26, 2010.

  1. inspired

    inspired Guest

    When I run the following code

    final double MAX_ANGLE = 360.00;
    // Do for every angle from max to zero
    for (double angle = MAX_ANGLE; angle >= 0; angle = angle - 0.01)
    {
    System.out.print("Input Angle = " + angle + " : ");
    }

    I get inconsistent output like this:

    Input Angle = 360.0 :
    Input Angle = 359.99 :
    Input Angle = 359.98 :
    Input Angle = 359.97 :
    Input Angle = 359.96000000000004 : <-- why all the extra digits?
    Input Angle = 359.95000000000005 : <-- why all the extra digits?
    Input Angle = 359.94000000000005 : <-- why all the extra digits?
    Input Angle = 359.93000000000006 : <-- why all the extra digits?
    Input Angle = 359.9200000000001 : <-- why all the extra digits?
    Input Angle = 359.9100000000001 : <-- why all the extra digits?
    Input Angle = 359.9000000000001 : <-- why all the extra digits?
    Input Angle = 359.8900000000001 : <-- why all the extra digits?

    while I expected:

    Input Angle = 360.0 :
    Input Angle = 359.99 :
    Input Angle = 359.98 :
    Input Angle = 359.97 :
    Input Angle = 359.96 :
    Input Angle = 359.95 :
    Input Angle = 359.94 :
    Input Angle = 359.93 :
    Input Angle = 359.92 :
    Input Angle = 359.91 :
    Input Angle = 359.90 :
    Input Angle = 359.89 :

    Why do I get all the extra digits? How can I avoid this?

    Thanks, Alan
    inspired, Nov 26, 2010
    #1
    1. Advertising

  2. inspired

    Stefan Ram Guest

    inspired <> writes:
    >Why do I get all the extra digits?


    You used »double« instead of »java.lang.BigDecimal«.
    Double values are binary floating point values and therefore
    cannot represent the values of your decimal numerals precisely.

    > How can I avoid this?


    Either use »java.lang.BigDecimal« or a format specifier
    like »%.02f« in the standard method »printf«.

    (In a Java newsgroup, you do not need to mention »Java« in
    the subject of your posts.)
    Stefan Ram, Nov 26, 2010
    #2
    1. Advertising

  3. inspired

    inspired Guest

    Thank you.

    On Nov 26, 6:52 pm, -berlin.de (Stefan Ram) wrote:
    > inspired <> writes:
    > >Why do I get all the extra digits?

    >
    >   You used »double« instead of »java.lang.BigDecimal«.
    >   Double values are binary floating point values and therefore
    >   cannot represent the values of your decimal numerals precisely.
    >
    > >                                        How can I avoid this?

    >
    >   Either use »java.lang.BigDecimal« or a format specifier
    >   like »%.02f« in the standard method »printf«.
    >
    >   (In a Java newsgroup, you do not need to mention »Java« in
    >   the subject of your posts.)
    inspired, Nov 27, 2010
    #3
  4. inspired

    Arne Vajhøj Guest

    On 26-11-2010 18:47, inspired wrote:
    > When I run the following code
    >
    > final double MAX_ANGLE = 360.00;
    > // Do for every angle from max to zero
    > for (double angle = MAX_ANGLE; angle>= 0; angle = angle - 0.01)
    > {
    > System.out.print("Input Angle = " + angle + " : ");
    > }
    >
    > I get inconsistent output like this:
    >
    > Input Angle = 360.0 :
    > Input Angle = 359.99 :
    > Input Angle = 359.98 :
    > Input Angle = 359.97 :
    > Input Angle = 359.96000000000004 :<-- why all the extra digits?
    > Input Angle = 359.95000000000005 :<-- why all the extra digits?
    > Input Angle = 359.94000000000005 :<-- why all the extra digits?
    > Input Angle = 359.93000000000006 :<-- why all the extra digits?
    > Input Angle = 359.9200000000001 :<-- why all the extra digits?
    > Input Angle = 359.9100000000001 :<-- why all the extra digits?
    > Input Angle = 359.9000000000001 :<-- why all the extra digits?
    > Input Angle = 359.8900000000001 :<-- why all the extra digits?
    >
    > while I expected:
    >
    > Input Angle = 360.0 :
    > Input Angle = 359.99 :
    > Input Angle = 359.98 :
    > Input Angle = 359.97 :
    > Input Angle = 359.96 :
    > Input Angle = 359.95 :
    > Input Angle = 359.94 :
    > Input Angle = 359.93 :
    > Input Angle = 359.92 :
    > Input Angle = 359.91 :
    > Input Angle = 359.90 :
    > Input Angle = 359.89 :
    >
    > Why do I get all the extra digits? How can I avoid this?


    That is how floating point work.

    Does the difference between 359.96000000000004 and 359.96
    really matters for your computations?

    If the answer is yes, then you should not use FP.

    Arne
    Arne Vajhøj, Nov 27, 2010
    #4
  5. inspired

    Roedy Green Guest

    On Fri, 26 Nov 2010 15:47:54 -0800 (PST), inspired
    <> wrote, quoted or indirectly quoted someone
    who said :

    ><-- why all the extra digits


    see http://mindprod.com/jgloss/floatingpoint.html

    The short answer is 0.1 is a repeater in binary.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
    Roedy Green, Nov 28, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Pop
    Replies:
    0
    Views:
    1,168
    Dan Pop
    Jun 24, 2003
  2. cyberdude
    Replies:
    2
    Views:
    5,104
    Keith Thompson
    Jun 25, 2003
  3. Replies:
    8
    Views:
    799
    Andrew Thompson
    Oct 7, 2006
  4. Ronny Mandal

    Obtaining double precision random number?

    Ronny Mandal, Apr 14, 2005, in forum: C Programming
    Replies:
    2
    Views:
    538
    Eric Sosman
    Apr 14, 2005
  5. Protoman
    Replies:
    37
    Views:
    1,009
Loading...

Share This Page