toString-ing floats and doubles

Stuart Ballard kaffe@rufus.w3.org
Wed, 28 Jun 2000 11:34:39 -0400


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?

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

Thanks in advance,
Stuart.