toString-ing floats and doubles

Stuart Ballard kaffe@rufus.w3.org
Wed, 28 Jun 2000 14:54:51 -0400


Archie Cobbs wrote:
> 
>   * 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.

Hmm. Well, the intricacies of IEEE floating point are a bit beyond me at
this point, but after looking at the implementations in Kaffe, Classpath
and libgcj, libgcj's looks like it might be interesting. I can't tell
you whether or not it gives the right answer for all cases, but it is
implemented from scratch rather than using printf, so it seems there
might be a good chance. It will obviously need some changes - libgcj
doesn't use JNI, for a start. But here's a URL into their cvsweb...

http://sourceware.cygnus.com/cgi-bin/cvsweb.cgi/libgcj/libjava/java/lang/natDouble.cc?rev=1.8&content-type=text/x-cvsweb-markup&cvsroot=java

Note: libgcj is under the GPL with an exception clause; it's certainly
compatible with Kaffe's GPL. As of a few months ago, I believe the
copyright is held by the FSF.

Stuart.