Wednesday, January 8, 2014

Geek: Java oddity...

Geek: Java oddity...  I'm writing a replacement for Java's DecimalFormat class – faster, threadsafe, and slightly extended.  In the course of writing tests for my code, I discovered some unexpected behavior in DecimalFormat when formatting float values.  Here's some code that illustrates the behavior:

DecimalFormat df = new DecimalFormat( "#0" );
String fmt = df.format( 11111111E15f );
Run that code, and fmt will contain "11111110989762554000000" – way more significant digits than should be there.  WTF!?!

With a little bit of digging, I figured out what's going on.  The DecimalFormat class doesn't have a method with a format( float ) signature.  So what's happening is that my float parameter is being cast to a double, and the method with float( double ) signature is being called.  Inside that method, it assumes that there are 53 bits of significance in the mantissa, not the 24 actually present in the float – so it emits digits to cover all that.

I'm really surprised that was done in the first place, and even more surprised that the Java folks haven't fixed it.  That could lead to some mighty misleading results!

My class won't do that :)

No comments:

Post a Comment