Results 1 to 7 of 7
  1. #1
    Aga
    Aga is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Question Floating point inaccuracy

    Hi,

    I have the following code:

    System.out.println(" fac5 = ln(fac2 / fac3) = " + fac5);
    System.out.println(" fac4 / fac2 = " + fac4 / fac2);
    System.out.println(" fac4 / fac3 = " + fac4 / fac3);
    double FAC1 = (fac4 / fac2) - (fac4 / fac3);
    System.out.println(" FAC1 = fac4 / fac2 - fac4 / fac3 = " + FAC1 );
    double FAC2_1 = fac5 + FAC1;
    double FAC2_2 = fac5 + (fac4 / fac2) - (fac4 / fac3);
    System.out.println(" FAC2_1 = fac5 + fac4 / fac2 - fac4 / fac3 using FAC1= " + FAC2_1 );
    System.out.println(" FAC2_2 = fac5 + fac4 / fac2 - fac4 / fac3 computed= " + FAC2_2 );

    whose output is:

    fac5 = ln(fac2 / fac3) = -7.294165271787545E-14
    fac4 / fac2 = 1.000000000000219
    fac4 / fac3 = 1.0000000000001459
    FAC1 = fac4 / fac2 - fac4 / fac3 = 7.30526750203353E-14
    FAC2_1 = fac5 + FAC1= 1.1102230245985246E-16
    FAC2_2 = fac5 + fac4 / fac2 - fac4 / fac3 = 0.0 !!!!!:confused:

    Incredibly, FAC2_1 and FAC2_2 give different results when they are the same calculation made in two different ways.

    I'd appreciate any help on clarifying why this happens. Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    That's expected. For a full article, google "what every computer scientist should know about floating-point arithmetic".

    But the gist of it is, many numbers can not be represented in binary, so they must be rounded to something that can. This leads to inaccuracies when dealing with small numbers.

    PS- When posting code, it should be in the form of an SSCCE, and you should use the code tags. What you posted really doesn't give us any idea what you're doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    But the gist of it is, many numbers can not be represented in binary, so they must be rounded to something that can. This leads to inaccuracies when dealing with small numbers.
    Not only with small numbers; for very large numbers the absolute accuracy gets worse; see that mysterious ulp( ... ) method in the Math class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Aga
    Aga is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Floating point inaccuracy

    Thank you all so much for your answers and the reference.

    I would not have expected a 1.0E-16 result using DOUBLE primitives to be rounded off to zero, thus my surprise. I wonder whether C or FORTRAN would have the same problem. Any guesses??

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Aga View Post
    Thank you all so much for your answers and the reference.

    I would not have expected a 1.0E-16 result using DOUBLE primitives to be rounded off to zero, thus my surprise. I wonder whether C or FORTRAN would have the same problem. Any guesses??
    Not a guess, but a fact: Java uses the same IEEE/754 floating format rules as C and Fortran do; don't be mislead by the textual representation of those numbers (i.e. when you print them).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by Aga View Post
    Thank you all so much for your answers and the reference.

    I would not have expected a 1.0E-16 result using DOUBLE primitives to be rounded off to zero, thus my surprise. I wonder whether C or FORTRAN would have the same problem. Any guesses??
    One thing java does have though , is a class arbitrary precision floating point - if you need more precision than a double will give. Take a look at java.math.BigDecimal.

    Many 64 bit linux machines also have a 'long double' type, that's 16 bytes. There is no direct representation for that (yet) in java.

Similar Threads

  1. if it is floating point number
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 07-06-2010, 09:17 AM
  2. Replies: 1
    Last Post: 07-14-2009, 07:04 PM
  3. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 04:11 PM
  4. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 09:41 AM
  5. Floating point values in SWT Spinner
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •