[PATCH] Sunbug 6358355: Rounding error in Float.parseFloat (original) (raw)

Louis Wasserman wasserman.louis at gmail.com
Thu Jul 12 13:15:16 UTC 2012


Float.parseFloat doesn't currently round correctly, or even monotonically, in certain cases.

In particular, the following test program prints false:

public class Foo { public static void main(String[] args) { System.out.println(144115196665790480f <= 144115196665790481f); } }

A patch is attached, and can also be found at https://bugs.openjdk.java.net/show_bug.cgi?id=100208.

There was a comment in sun.misc.FloatingDecimal claiming this would take 400 lines of code, but by eliminating the (fallacious) "sticky rounding" logic, and just duplicating the double-parsing logic, it only ends up costing ~40 net lines of code added.

The added code is mostly identical to the preexisting double-parsing code.

This is a prerequisite for the separate, previously sent patch improving the performance of BigInteger.doubleValue() and floatValue(). (Testing for that patch revealed this bug.)

Louis Wasserman wasserman.louis at gmail.com http://profiles.google.com/wasserman.louis



More information about the core-libs-dev mailing list