Quality Outreach Heads-up - JDK 19 - Double.toString() and Float.toString() changes
David Delabassee on September 23, 2022The OpenJDK Quality Group promotes the testing of FOSS projects with OpenJDK Early-Access builds as a way to improve the overall quality of the release. This heads-up is part of a regular communication sent to the projects involved. To learn more about the program, and how-to join, please check here.
JDK 19 - Double.toString()
and Float.toString()
changes
A bug affecting Double.toString(double)
and Float.toString(float)
has
been fixed in Java 19.
According to the Javadoc, Double.toString(double)
should produce a string with a number of digits as small as possible, that still uniquely distinguishes this double from its adjacent double. That was not the case for many doubles. For example, whereas 1e23
and 9.999999999999999E22
are the same double, passing 1e23
to Double.toString(double)
was returning "9.999999999999999E22"
.
The specification has been updated, and the implementation along with it. As a consequence and starting with JDK 19 some of the strings produced are now shorter than the ones produced in earlier releases. For example, passing 1e23
to this method in JDK 19 will return "1.0E23"
instead of "9.999999999999999E22"
in earlier JDK releases. Note that in JDK 19, passing 9.999999999999999E22
to this method also returns "1.0E23"
because they are the same double. Many string representations of doubles and floats have changed to match the specification update.
More information
For more details, please check this release note and the fix itself.
You can also learn more about floating point arithmetic in Joe Darcy’s excellent “Floating-Point Arithmetic : What Every Java Programmer Should Know!” session below.