Hi,
Sorry if this is not relevent to this forum (and if it isn't where should I ask/report this?)
My setup:
JBuilder 4 Enterprise Edition.
JDK1.3
jakartatomcat3.2.2
Ok, now I have a problem with the following code:
double a = 0.03;
double b = 2300.3;
double c = a + b;
System.err.println(c);
The result is:
2300.3300000000004
Now I would have thought that is would have been 2300.33 I'm no genius, but am I right in assuming this to be a bug? or am I missing some sort of "feature" that I am unware of?
To test that code, I placed that code in my "init(ServletConfig config)" method for a servlet that I have that runs on startup of Tomcat.
What do I do? (or what can I do?)
regards,
Eric

A bug? (5 messages)
 Posted by: Eric Sambach
 Posted on: September 05 2001 10:15 EDT
Threaded Messages (5)
 A bug? by Stephen Davies on September 05 2001 19:51 EDT
 A bug? by Eric Sambach on September 06 2001 04:15 EDT

A bug? by Stephen Davies on September 06 2001 06:37 EDT

A bug? by Eric Sambach on September 06 2001 09:45 EDT
 A bug? by Saruman White on September 06 2001 06:12 EDT

A bug? by Eric Sambach on September 06 2001 09:45 EDT

A bug? by Stephen Davies on September 06 2001 06:37 EDT
 A bug? by Eric Sambach on September 06 2001 04:15 EDT

A bug?[ Go to top ]
 Posted by: Stephen Davies
 Posted on: September 05 2001 19:51 EDT
 in response to Eric Sambach
Not a bug Eric, its a feature of using floating point numbers. Floating point number are represented as a binary fraction and often there is no exact value for a given decimal value, kind of like the way that there is no exact decimal fraction to represent 1/3. The result of all this is that floating point number have to be treated as approximations. In your example the result is within one 40 thousand billionith of the correct answer, pretty good as far as approximations go.
As far as deling with this problem you could round the result to remove insignificant decimal places or instead you could use BigDecimal objects instead of floating point.
Regards,
Stephen 
A bug?[ Go to top ]
 Posted by: Eric Sambach
 Posted on: September 06 2001 04:15 EDT
 in response to Stephen Davies
Stephen,
I understand what you are saying, but I don't see how that applies to me!? If the calculation involved a division I could accept that! But it doesn't! I entered EXACT figures and I do an ADDITION!!! There really should be NO need to approximate anything!
Hmmm, guess I'll just have to deal with it! I'll try your rounding idea....
grumble, grumble, grumble, never had this problem in C, grumble, grumble..... ;)
Thanks for your reply.....

A bug?[ Go to top ]
 Posted by: Stephen Davies
 Posted on: September 06 2001 06:37 EDT
 in response to Eric Sambach
Eric,
you may have entered exact decimal values but these are converted to doubles by the compiler and it is during this conversion that the approximation is made. There is no exact value for 0.3 in binary, same as there is no exact value for 1/3 in decimal.
Consider the value 1/3 for a moment, when represented as a fraction this is an exact value. If I convert it to a decimal then my result is 0.33... There is no exact value for this as a decimal and so I must take the closest value that can be represented by my chosen number system, I must approximate.
Now consider 1/3 + 1/3 + 1/3 = 1. Simple really but what if I try to perform this using my decimal values?
I get 0.33 + 0.33 + 0.33 = 0.99!
The use of the approximation results in an error.
If you use floating point in ANY langauge you must be prepared to accept a certain amount of error in the results. Some values are worse than others, 1/4 can be represented exactly in base 2 floating point, 1/10 cannot.
If you want the EXACT result then you can only use the Java BigDecimal class and pay the performance penalty. Languages such as COBOL and RPG actually have datatypes to support these kinds of operations. IBM mainframe assembler has different kinds of adds, those for floating point and those for decimal.
Hope this helps you out,
Stephen 
A bug?[ Go to top ]
 Posted by: Eric Sambach
 Posted on: September 06 2001 09:45 EDT
 in response to Stephen Davies
Stephen,
Hello again. Ok, once again I understand what you are saying, but I ran a little "test" and I'm sorry, but I totally believe there is a bug within either the Java engine or maybe the compiler...
Here is my test code:
double a = 0.03;
System.err.println(a);
double b = 2300.3;
System.err.println(b);
double c = a + b;
System.err.println(c);
double d = 2300.33;
System.err.println("d = " + d);
And here is the output:
0.03
2300.3
2300.3300000000004
d = 2300.33
Ok, so if what you are saying is true the "approximation" error should show in either (or both) of the first two print operations, but it doesn't! The "approximation" error ONLY occurs when the addition is performed, but as you can see the resultant value CAN be stored as I can print it out correctly with the 4th print statement... This to me means "bug". But, as I mentioned before: I'm over it, and I'll try and use BigDecimal or something....
Cya,
Eric 
A bug?[ Go to top ]
 Posted by: Saruman White
 Posted on: September 06 2001 18:12 EDT
 in response to Eric Sambach
You should not use decimal representation of your numbers to
argue that you have no division of rounding problems. According to Java Language Specification all double and float numbers are represented using IEEE754 standard. This standard uses not the powers of 10, but powers of 2 with integer multipliers. Results of arithmetic operations are then rounded to the nearest IEEE754 value. So it's absolutely possible to pick this 0.00000004 somewhere. For precise double decimal arithmetic usage of BigDecimal is now recommended by JDK creators.