"Kilkenny, Rory" <[EMAIL PROTECTED]> wrote:
> If you are referring to the1.2000000000000002E-8 where you have an actual
> math error, it is because double and float math don't work properly, for
> whatever reason.

Actually, you are completely wrong.  "Double and float math" works exactly as
the IEEE designed it to work.

The problem is not with double and float; it is with people's naive
expectations of what they can do.  Multiples of powers of one-tenth cannot in
general be represented accurately with a finite number of binary fraction
digits.  When decimals are converted to binary there is an unstable
equilibrium; if you don't touch it, you can convert it back with no apparent
loss of precision.  That equilibrium is broken by the roundoff error from
*ALMOST ANY ARITHMETIC INVOLVING FRACTIONS*.

This roundoff error is not related to Java.  The same identical results can be
obtained with IEEE machine floating point math in C, C++, Fortran, Cobol, Lisp,
Perl, or Python.  The same errors occur (although they manifest differently) in
older proprietary binary floating point formats as well.  The roundoff error is
inherent in decimal-to-binary-to-decimal conversions.

> If you switch to using BigDecimal then you are able to
> obtain a "better" answer.

BigDecimal resolves the problem by not converting the number to binary form in
the first place.  It actually does math with decimal digits instead of bits. 
The math is mind-bogglingly slow, but you never get any roundoff errors that
you wouldn't get doing the math by hand.

Another solution to the rounding error problem is to go ahead and do math with
double or float, but only show a limited number of significant figures on your
output, thus hiding the roundoff error (which is generally confined to the 13th
or 14th significant figure).  To find out how to do this in Java, check out the
javadoc for the class java.text.NumberFormat .

-- Roger

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to