Results 1 to 7 of 7
Thread: Floating point inaccuracy
- 02-03-2011, 02:46 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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.
- 02-03-2011, 02:56 PM #2
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!
- 02-03-2011, 02:58 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Read this: http://www.cse.msu.edu/~cse320/Docum...atingPoint.pdf
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-03-2011, 03:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 02-03-2011, 04:53 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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??
- 02-03-2011, 05:06 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-04-2011, 05:07 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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
-
if it is floating point number
By Stephen Douglas in forum New To JavaReplies: 26Last Post: 07-06-2010, 09:17 AM -
Floating point operations is slower when small values are used?
By Cesaro in forum Advanced JavaReplies: 1Last Post: 07-14-2009, 07:04 PM -
java floating point comparison
By sardare in forum Advanced JavaReplies: 6Last Post: 03-03-2009, 04:11 PM -
number of floating point
By mohammad8065 in forum Advanced JavaReplies: 5Last Post: 12-28-2008, 09:41 AM -
Floating point values in SWT Spinner
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks