Board index » jbuilder » Floating Point Math Bug

Floating Point Math Bug


2004-04-09 12:11:52 AM
jbuilder14
I've got a big problem here. There seems to be a major bug in Java's Math
library.
Here is what I am getting:
float var0 = Float.parseFloat( fieldAmount.getText() );
int var1 = dataSet.getInt("var1");
float var2 = dataSet.getFloat("var2");
float diff = var0 - (var1 * var2);
Unformatted printouts show:
var0: 4.59
var1: 440
var2: 1982.6
diff should be 7.00 but its NOT. Its 7.000122! Why? This is VERY wrong!
I am using 1.3.x and 1.4.x JDK's in JBuilder 7.
Any ideas? Is this a known bug?
Thanks.
Ken
 
 

Re:Floating Point Math Bug

Not a bug at all. Because of certain well-known limitations, floating point
numbers are represented in memory as approximations, not exact values. A
couple of obvious examples:
pi = 3.14159265369...
1/7 = 0.14285714285714285714285714285714...
Both of these numbers repeat to infinite precision, something that is simply
impossible to represent on any computer, no matter how powerful.
If you want exact values, for which there is a significant performance
penalty, look at the java.math.BigDecimal class.
For a really good discussion of IEEE floating point arithmetic, see:
www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"GUI-Majician" < XXXX@XXXXX.COM >wrote in message
Quote
I've got a big problem here. There seems to be a major bug in Java's Math
library.

Here is what I am getting:

float var0 = Float.parseFloat( fieldAmount.getText() );
int var1 = dataSet.getInt("var1");
float var2 = dataSet.getFloat("var2");
float diff = var0 - (var1 * var2);

Unformatted printouts show:

var0: 4.59
var1: 440
var2: 1982.6

diff should be 7.00 but its NOT. Its 7.000122! Why? This is VERY wrong!
I am using 1.3.x and 1.4.x JDK's in JBuilder 7.

Any ideas? Is this a known bug?

Thanks.

Ken


 

Re:Floating Point Math Bug

Thanks, I forgot about BigDecimal.
But, there is approximation and then there is gross approximation. Being
accurate to only 3 decimal places is worthless! I don't know why I didn't
noticed this inaccuracy before.
BTW, in an effort to strip off my var names I got the numbers out of order.
It should be 1982.6 - (440 * 4.49) = 7.0000. Sorry bout that.
Thanks.
"GUI-Majician" < XXXX@XXXXX.COM >wrote in message
Quote
I've got a big problem here. There seems to be a major bug in Java's Math
library.

Here is what I am getting:

float var0 = Float.parseFloat( fieldAmount.getText() );
int var1 = dataSet.getInt("var1");
float var2 = dataSet.getFloat("var2");
float diff = var0 - (var1 * var2);

Unformatted printouts show:

var0: 4.59
var1: 440
var2: 1982.6

diff should be 7.00 but its NOT. Its 7.000122! Why? This is VERY wrong!
I am using 1.3.x and 1.4.x JDK's in JBuilder 7.

Any ideas? Is this a known bug?

Thanks.

Ken


 

{smallsort}

Re:Floating Point Math Bug

On 4/8/2004 at 12:11:52 PM, GUI-Majician wrote:
Quote
float diff = var0 - (var1 * var2);
var0: 4.59
var1: 440
var2: 1982.6

diff should be 7.00 but its NOT. 7.000122!
How do you figure that?
diff = var0 - (var1 * var2)
= 4.59 - (440 * 1982.6)
= -872339.41
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Floating Point Math Bug

Try using double instead of float. That may resolve your problem.
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"GUI-Majician" < XXXX@XXXXX.COM >wrote in message
Quote
Thanks, I forgot about BigDecimal.

But, there is approximation and then there is gross approximation. Being
accurate to only 3 decimal places is worthless! I don't know why I didn't
noticed this inaccuracy before.

BTW, in an effort to strip off my var names I got the numbers out of
order.
It should be 1982.6 - (440 * 4.49) = 7.0000. Sorry bout that.

Thanks.

"GUI-Majician" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>I've got a big problem here. There seems to be a major bug in Java's
Math
>library.
>
>Here is what I am getting:
>
>float var0 = Float.parseFloat( fieldAmount.getText() );
>int var1 = dataSet.getInt("var1");
>float var2 = dataSet.getFloat("var2");
>float diff = var0 - (var1 * var2);
>
>Unformatted printouts show:
>
>var0: 4.59
>var1: 440
>var2: 1982.6
>
>diff should be 7.00 but its NOT. Its 7.000122! Why? This is VERY
wrong!
>I am using 1.3.x and 1.4.x JDK's in JBuilder 7.
>
>Any ideas? Is this a known bug?
>
>Thanks.
>
>Ken
>
>


 

Re:Floating Point Math Bug

GUI-Majician wrote:
Quote
Unfortunately BigDecimal doesn't fix the problem because the data comes
from
a database and dataSet.getFloat() is just as innacurate as before. Back
where we started...

You enter 4.49 into a data field and the dataset sees 4.489997... Geez!
Which databases drivers are you using? The bug could be with the driver
itself.
 

Re:Floating Point Math Bug

On 4/8/2004 at 2:10:03 PM, GUI-Majician wrote:
Quote
But, there is approximation and then there is gross approximation.
Being accurate to only 3 decimal places is worthless! I don't
know why I didn't noticed this inaccuracy before.
First, let me say that it is not *inaccurate*. In fact, it is 100%
accurate. It is just not very *precise*.
But why is that?
You are using the Java _float_ type, and that means 32-bit IEEE 754
floating point numbers, which have 23 bits of mantissa. Since 2 ^ 23
= 8388608, that means you can get 7 or 8 digits of precision in a Java
float. But remember that this is the best precision you can have. In
any operation, you can lose precision, and once precision is lost, you
can never get it back.
In your program, the final operation performed is to subtract two numbers:
the value of the variable "var0" and the temporary result of the
expression (var1 * var2). With infinite precision, this would be (1982.6
- 1975.6) = 7.0, but since those numbers are at best represented with 7 or
8 digits of precision, you get something like (1982.6000 - 1975.5999) =
0007.0001, about 5 digits of precision.
Note that this is the way floating point numbers work, regardless of the
language you are using. Precision is limited, and some operations, such
as subtracting numbers that are very close, result in lost precision.
There is no way to avoid this. You can use wider floating point numbers,
but that just pushes the problem out to larger numbers. The only way to
avoid it is to use a different data type for your numbers.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Floating Point Math Bug

JDataStore that came with JBuilder Ent 7
Ken
"pnichols" < XXXX@XXXXX.COM >wrote in message
Quote
GUI-Majician wrote:

Which databases drivers are you using? The bug could be with the driver
itself.
 

Re:Floating Point Math Bug

I'm not going to argue semantics, accuracy vs. precision. I've had way too
many years of Math for that.
The problem is that JDataStore is causing the loss of precision, not the
mathmatical operation. AND, if IEEE 754 FP specifies a certain level of
precision then I would think a database should be able to at least MAINTAIN
that precision, which it is NOT.
This may be the wrong NG for this topic now that we've narrowed it down to
JDataStore. If it were 8 significant digits that would be acceptable but 3
sd's is not, and rounding will not correct the precision, so we are stuck.
I think JDS supports BigDecimal so I may have to convert my columns to that
DT, a major pain. I wish Borland had warned us about this.
Thanks for your help.
Ken
 

Re:Floating Point Math Bug

On 4/9/2004 at 9:55:59 AM, GUI-Majician wrote:
Quote
The problem is that JDataStore is causing the loss of precision,
not the mathmatical operation.
I do not understand how you could come to that conclusion. According to
your original message, you were calculating the following:
float diff = var0 - (var1 * var2);
You knew the values for the three right-side variables, and felt that the
value assigned to diff was incorrect. Since JDataStore is not involved in
this calculation, it could not be the cause of the "problem".
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Floating Point Math Bug

GUI-Majician wrote:
Quote
I'm not going to argue semantics, accuracy vs. precision. I've had way too
many years of Math for that.
Really? You've worked for years and years with IEEE Floating-point, in
Fortran and/or C? I find that hard to swallow after reading your
ignorant rants in your previous posts.
IEEE FP benefits *and limitations* are basic CS101 material. Since you
seem to be utterly clueless about them, I suggest you read the tutorial
link earlier in this thread, *really understand and absorb* the issues,
and then come back to this issue.
You're doing no one any favors by bumbling along in this illiterate fashion.
 

Re:Floating Point Math Bug

On 4/12/2004 at 8:53:31 PM, Shankar Unni wrote:
Quote
your ignorant rants
you seem to be utterly clueless
by bumbling along in this illiterate fashion.
Please try to avoid comments like these.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Floating Point Math Bug

John McGrath [TeamB] wrote:
Quote
Please try to avoid comments like these.
Sorry. Long day.. Tax problems.. mumble mumble.. feeble excuses..
 

Re:Floating Point Math Bug

On 4/14/2004 at 8:56:11 PM, Shankar Unni wrote:
Quote
Sorry. Long day.. Tax problems.. mumble mumble.. feeble excuses..
:o)
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Floating Point Math Bug

I don't know whats going on with this thread but one should carefully read a
thread before responding.
Anyway, the problem is indeed with JDataStore and not IEEE FP. IEEE FP was
NOT my idea! Understanding IEEE FP has nothing to do with this problem nor
was it my suggestion.
The original post assumed that the loss of precision was in the math
statement given. It is not. The imprecision, 1% is unnacceptable, comes
from the fact that these values are coming from a JDataStore. The values
are accurately entered but when you query the JDS you get back imprecise
values.
After talking to someone else with more JDS experience the solution is to
NEVER use floating point numbers in a JDS. Always use BigDecimal. Problem
solved.
Thank you TeamB for your help.
GM