Sounds logical to me, too... Because if it would be correct then type "double" would be almost useless! Maybe a bug report should be created for the given JVM (with the details of your system) if you are REALLY sure about that result.

Regards,
Norbi

----- Original Message ----- From: "Sebastiaan van Erk" <[EMAIL PROTECTED]>
To: "Tapestry users" <tapestry-user@jakarta.apache.org>
Sent: Tuesday, January 24, 2006 10:54 AM
Subject: Re: [OT] 3.163 + 0.001 = ???


Hi,

I'm replying a few days late, but hopefully it's still useful.

I don't understand all the people saying you should use BigDecimal or saying that this is a double rounding problem. The BigDecimal solution will work (that's beside the point), but that this is a double rounding problem is incorrect and your code below *SHOULD* work.

A double has a huge number of digits of precision, about 15 or 16 or so (this is so ridiculously precise that it could store the circumference of the earth to within 1/1000th of the width of a human hair precision). Since a double is stored in bits and not in decimal digits, there is a problem representing decimal numbers exactly. That is, there may be a small error due to the last bit only being able to be a 0 or a 1. For example if you only had 2 bits after the decimal point you would only have (in binary) 0.00, 0.01, 0.10 and 0.11 which are 0, 0.25, 0.5, and .75 in decimal. Thus any other decimal value would have to be converted to the closest one of these.

However, the last bit being off means you can have a small error in the 15th digit of precision, not in the 4th!!!

A result you could expect, and the result I in fact get when I run your program is: c = 3.1639999999999997

This is the correct result, it is really really terribly close to 3.164, unlike 3.1679999999999997 which is just plain miles off.

I don't have an explanation for your result. It is terribly wrong! Furthermore, as far as I know, all java virtual machines should produce the exact same result so you should get the result that I got above.

Greetings,
Sebastiaan

Gunna Satria wrote:
Hi All,
   I tried this code:
  double a = 3.163;
  double b = 0.001;
  double c = a+b;
  System.out.println("c = "+c);
  and the result is,   c = 3.1679999999999997
  not 3.164 like i expected
  how come?
   regards,
   Gunna



---------------------------------
 What are the most popular cars?  Find out at Yahoo! Autos

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.375 / Virus Database: 267.14.22/238 - Release Date: 2006. 01. 23.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to