This content has been marked as final.
Show 12 replies

1. Re: Subtraction with Doubles
807599 Dec 31, 2006 5:42 PM (in response to 807599)http://docs.sun.com/source/8063568/ncg_goldberg.html 
2. Re: Subtraction with Doubles
807599 Dec 31, 2006 5:53 PM (in response to 807599)Does that apply to doubles?
e.g.double a; double b; double y = a  b;

3. Re: Subtraction with Doubles
807599 Dec 31, 2006 5:58 PM (in response to 807599)Does that apply to doubles?
Why do you think I posted the reference? 
4. Re: Subtraction with Doubles
807599 Dec 31, 2006 6:04 PM (in response to 807599)I can't see how that applies with a quick read. The sum doesn't need to round? 
5. Re: Subtraction with Doubles
807599 Dec 31, 2006 9:11 PM (in response to 807599)observe that the variable double has 64 size bits 
6. Re: Subtraction with Doubles
796254 Jan 1, 2007 1:28 AM (in response to 807599)64 bits yes, but only 11 bits for the exponent and 53 for the mantissa. This translates into 1517 digit precision for doubles:
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Tech/Chapter02/floatingPt.html
% 
7. Re: Subtraction with Doubles
807599 Jan 1, 2007 5:55 AM (in response to 807599)It seems that you only want it to show three places. A very simple answer, check into java.text.DecimalFormat for decimal formatting. 
8. Re: Subtraction with Doubles
807599 Jan 1, 2007 10:51 PM (in response to 807599)Thanks for all your help, I understand whats happening a bit now, not fully though (e.g. why a  can reproduce this effect and the + doesn't). I found this effect very strange as I've never heard of it before. I'll try to implement a suitable work around now as the input data into my program will be decimals ranging from 26 decimal places.
Thanks. 
9. Re: Subtraction with Doubles
796254 Jan 2, 2007 1:24 AM (in response to 807599)Thanks for all your help, I understand whats
No, that's incorrect. Another wrong conclusion.
happening a bit now, not fully though (e.g. why a 
can reproduce this effect and the + doesn't).
I found
So you've never read anything about how floating point numbers are represented in computers?
this effect very strange as I've never heard of it
before.
% 
10. Re: Subtraction with Doubles
3004 Jan 2, 2007 2:42 AM (in response to 807599)Thanks for all your help, I understand whats
Sometimes the error is smaller than the representation precision, or smaller than the default display precision, such that the rounding ends up being a value that looks "exact."
happening a bit now, not fully though (e.g. why a 
can reproduce this effect and the + doesn't). I found
this effect very strange as I've never heard of it
before.
I'm too lazy to find a real example, but, hypothetically, if something ends up as 1.00025, when the correct answer is 1, it could end up rounding to 1.000, which happens to be the right answer anway. Two wrongs made a right, sort of.
I'll try to implement a suitable work around
If you want fixed decimal precision, use BigDecimal. If you need decimal precision of 6 decimal digits, you can probably get away with using double, which has decimal precision of something like 1315 decimal digits, I think. Any error will get rounded away, BUT, errors can accumulate such that you're sixth (for example) decimal digit will be off.
now as the input data into my program will be
decimals ranging from 26 decimal places.
I asume at least one of these has been posted already, but here are my standard references for this topic:
SOME THINGS YOU SHOULD KNOW ABOUT FLOATINGPOINT ARITHMETIC
What Every Computer Scientist Should Know About FloatingPoint Arithmetic
Another good (slightly simpler) FP explanation:
http://mindprod.com/jgloss/floatingpoint.html 
11. Re: Subtraction with Doubles
807599 Jan 2, 2007 10:20 AM (in response to 3004)Thanks, especially for that last link. It explained it very well and helped me a lot.
I think the use of the
class will help me make exact calculations for my program.BigDecimal
If anyone in the future reads this thread, this gives a good example on how to perform exact calculations with floatingpoint numbers:
http://java.sun.com/developer/JDCTechTips/2001/tt0807.html 
12. Re: Subtraction with Doubles
807599 Jan 2, 2007 10:29 AM (in response to 807599)If anyone in the future reads this thread, this gives
You cannot "perform exact calculations with floatingpoint numbers" for ALL floating point numbers. Obvious examples are PI, E and the square root of 2.
a good example on how to perform exact calculations
with floatingpoint numbers:
http://java.sun.com/developer/JDCTechTips/2001/tt0807.
html