Quality Outreach Heads-up - JDK 19 - Double.toString() and Float.toString() changesDavid Delabassee on September 23, 2022
The 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 -
A bug affecting
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
9.999999999999999E22 are the same double, passing
Double.toString(double) was returning
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.
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.