1. ## 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,  Reply With Quote

2. ## 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 03:35 PM.  Reply With Quote

3. ## 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  Reply With Quote

4. ## 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 03:55 PM.  Reply With Quote

5. ## 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 04:24 PM.  Reply With Quote

6. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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.  Reply With Quote

double, precision 