-1

I have a silly question. Why this is giving 488.15999999999997 instead of 488.16

double dbl = 427.14 + 61.02;
System.out.println(dbl);

I heard that we must use BigDecimal if we are considering this much accuracy (like currency related logics). But I want to know what is behind this.

ironwood
  • 8,936
  • 15
  • 65
  • 114
  • 3
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Sotirios Delimanolis Aug 06 '13 at 14:04
  • 2
    Try this [IEEE 754 Calculator](http://www.binaryconvert.com/convert_double.html): paste your value for decimal, and watch what happens. It's worth a thousand words! – Sergey Kalinichenko Aug 06 '13 at 14:06
  • Thank you all for your comments. I noted that this topic is available in many places. But I wanted to know the exact reason behind this. OK for minus votes. I deserved it. But your references are very helpful to me. I gained a lot of knowledge about floating point values in java – ironwood Aug 06 '13 at 15:43
  • When you use floating point you have to apply appropriate rounding. If you do so you can use double for trading system and most bank use double or long (fixed precision) rather than BigDecimal esp those written in C++. – Peter Lawrey Aug 06 '13 at 18:08
  • 1
    The exact reason is that you get a number slightly less than you expect is that 427.14 is actually 427.1399999999999863575794734060764312744140625 and 61.02 is actually 61.02000000000000312638803734444081783294677734375 and when you add these together you get the nearest double value to 488.15999999999998948396751075051724910736083984375 which is slightly less than you expected. If you round this to 2 decimal places you get the expected answer 488.16 – Peter Lawrey Aug 06 '13 at 18:12
  • Thank you Peter. That explains the real scene behind the screen. But how do we know the actual value for a floating point. I mean how did u get 427.1399999999999863575794734060764312744140625 is the actual value for 427.14? – ironwood Aug 07 '13 at 00:23

2 Answers2

1

It's "normal" behavior. Remember, for the general case decimal values can not be represented exactly in a computer, so little errors will begin to creep. Take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic, it's mandatory reading for anyone writing code dealing with decimal values.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    *"Remember, a decimal value can not be represented exactly in a computer"* It *can*, it just *isn't* in IEEE 754 floating point. – T.J. Crowder Aug 06 '13 at 14:07
  • It can, if you have infinite memory. In a practical sense, and for the general case, it can't. – Óscar López Aug 06 '13 at 14:07
  • @ Óscar: No, that's not true at all. It's just a matter of how many decimal places you want to hold. Or more accurately, how many significant digits you want. To get about the same range as a `double` but with perfect decimal accuracy, you only need about 56 significant digits and an indicator of where the decimal point goes. Of course, decimal numbers have their own issues, such as not being able to accurately store `1 / 3`. ;-) – T.J. Crowder Aug 06 '13 at 14:08
1

How to resolve a Java Rounding Double issue This similar question's answer points here https://blogs.oracle.com/CoreJavaTechTips/entry/the_need_for_bigdecimal

Community
  • 1
  • 1
rbauer
  • 78
  • 5