toString-ing floats and doubles

Archie Cobbs kaffe@rufus.w3.org
Wed, 28 Jun 2000 10:34:25 -0700 (PDT)


Stuart Ballard writes:
> Is the conversion from a float or double to a String in Java supposed to
> be 100% deterministic and give identical results from one implementation
> to another?

Yes... emphasis on "supposed to"..

> I notice that System.out.println(Math.E) gives a slightly different
> result under Kaffe from JDK1.1, and since Math.E is a compiletime
> constant, I know I'm printing the value that my code was *compiled*
> against, so the problem isn't just that Kaffe defines E slightly
> differently than JDK. A similar result occurs with Math.PI, or any other
> float/double constant.
> 
> Trivial and obvious sample code:
> public class TestE {
>   public static void main(String[] args) {
>     System.out.println(Math.E);
>   }
> }
> 
> Compile it (against either JDK or Kaffe) and then run the same bytecode
> against both Kaffe and JDK. Kaffe's output:
> 2.7182818284590451
> 
> JDK1.1 output:
> 2.718281828459045

This is a known problem.. from FAQ.Known-Bugs..

  * The conversion of floats or doubles to strings does not completely
    conform to Sun's spec. For example, Double.toString() will display
    the double 0x400B333333333333 as "3.3999999999999999" instead of "3.4".

If you would like to work on improving this code that would be
warmly encouraged.

-Archie

___________________________________________________________________________
Archie Cobbs   *   Whistle Communications, Inc.  *   http://www.whistle.com