Results 1 to 6 of 6
Thread: Double precision issue
- 02-26-2013, 03:27 PM #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:
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 :)Java Code:double test1 =1.00000000000000010; double test2 =1.00000000000000001; if(test1==test2) System.out.println("Considered equal");
Its probably something silly, but I dont get it,
Thanks in advance!--Otacon
Somebody set up us the bomb.
-
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-1985Last edited by Fubarable; 02-26-2013 at 03:35 PM.
- 02-26-2013, 03:49 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-26-2013, 03:53 PM #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.
--Otacon
Somebody set up us the bomb.
- 02-26-2013, 04:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosLast edited by JosAH; 02-26-2013 at 04:24 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-26-2013, 04:23 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Border Layout issue with GUI - Single or double JTextFields are tiny and won't spread
By cknapick in forum AWT / SwingReplies: 2Last Post: 04-27-2012, 07:05 AM -
not getting precision as expected
By ellipse in forum New To JavaReplies: 4Last Post: 01-29-2012, 01:14 AM -
Double to Int Issue
By phosphide in forum Advanced JavaReplies: 8Last Post: 07-16-2011, 08:16 AM -
Precision
By c_walker in forum New To JavaReplies: 1Last Post: 10-18-2009, 11:36 AM -
How do I convert a decimal value to hexadecimal with double precision (64 bit)
By SKaur in forum New To JavaReplies: 7Last Post: 01-12-2008, 09:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks