Results 1 to 6 of 6
  1. #1
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Double precision issue

    Hi guys,

    Here is something messing up my code...and which I'm a little confused about.

    To by knowledge, a double can represent a smallest positive value of: 2.225E-307 ,

    so why is this happening:

    Java Code:
    double test1 =1.00000000000000010;
    double test2 =1.00000000000000001;
    if(test1==test2) System.out.println("Considered equal");
    In the example test1 and test2 are in fact considered equal, despite the double type being able to represent much smaller numbers, and its really screwing up my program :)

    Its probably something silly, but I dont get it,

    Thanks in advance!
    --Otacon
    Somebody set up us the bomb.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Double precision issue

    You need to understand that digital computers cannot represent floating point numbers to exact precision. This has nothing to do with Java and all to do with a basic engineering fact. If you need greater precision than is afforded by double values, consider using BigDecimal numbers, but realize that by doing so your program will take a memory and performance hit.

    You can find out more on double's precision from the JLS here: 4.2.3. Floating-Point Types, Formats, and Values

    and from the IEEE specification which Java follows: IEEE 754-1985
    Last edited by Fubarable; 02-26-2013 at 04:35 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Double precision issue

    A double value can store +- 15 significant digits (53 bits) an a 10 bit binary exponent. Your numbers are considered equal within an accuracy of 15 digits.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: Double precision issue

    Hmm, thanks,

    I do realize the issue of representing real values with binary precision, still, 1+10e-16 and 1+10e-17 are subjectively very different. There is no way to realize that difference with double precision type? That is a 64 bit precision, which I think should be able to cover much smaller differences. I'll look into BigDecimal I suppose.

    EDIT: just saw the response above. Thanks! I take it there is no datatype that handles bigger precision then 15 sd's , other then BigDecimal?
    Last edited by otacon; 02-26-2013 at 04:55 PM.
    --Otacon
    Somebody set up us the bomb.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Double precision issue

    The numbers are 1+1e-16 and 1+1e-17; they don't differ in 15 significant decimals: 1 is 1e16 times as big as the first number and 1e17 times as big as the second number. A double does take up 64 bits but 10 bits are for the (binary, biased) exponent and 1 bit is the sign bit ...

    kind regards,

    Jos
    Last edited by JosAH; 02-26-2013 at 05:24 PM.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Double precision issue

    Nope
    That's one of the things BigDecimal is for.
    The other being that it's accurate, which is why it's used for money things.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2012, 08:05 AM
  2. not getting precision as expected
    By ellipse in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 02:14 AM
  3. Double to Int Issue
    By phosphide in forum Advanced Java
    Replies: 8
    Last Post: 07-16-2011, 09:16 AM
  4. Precision
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 12:36 PM
  5. Replies: 7
    Last Post: 01-12-2008, 10:02 PM

Tags for this Thread

Posting Permissions

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