From: "Sylvain Roche" <[EMAIL PROTECTED]>
> I'm using Blackdown's 1.2.2 JDK, but I remember having the same trouble in
> the past with others jdk too. I must be misdoing something :
> take two floats and multiply them : it works in most cases. Most cases ?
> for example (float)0.05 * (float)2179 = 108.950005 instead of 108.95

You ain't doing anything wrong, nor is Java.  It's the other languages which
are wrong.  :-)  As you may know, floating point maths is not exact.  Many
numbers, like 0.05 AND 2179, are impossible to represent exactly. The
encoding of both numbers is very small, but multiplying scales the error.

Differently from many other languages, Java defaults to a larger precision
when formatting FP numbers, that's why sometimes you get an exact result in
C++ or VB but a ugly thing terminating in 0.000001 or .99999 in Java.

> in some cases, an operation like x / ( 1/y) works. In some others
> (including this one) it doesn't.

If you mean 0.05/(1/2179), the result should be "Infinity" and it's right,
you are dividing by zero. Try 0.05 / (1.0 / 2179).  Integer numbers are not
promoted to floating-point types only because you're dividing.

> The margin error is always less than 0.00001.
> Is it a cast problem, or is it worse. (By the way, the same thing happens
> also in javascript both in IE and Netscape).

Yes, they are all correct!  If you want more precision, use doubles.
If you want total precision, use bigdecimals.  Otherwise, live with that.
And please don't ever apply equality operators (==, !=) to FP numbers;
error propagation often make results just wrong enough to fail in exact
equality tests.  Use >,<,>=,<= only.

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to