JDK-6358355 : Direct String-to-float conversion does not preserve monotonicity
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0,7
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: linux_ubuntu,windows_xp
  • CPU: x86
  • Submitted: 2005-12-02
  • Updated: 2013-10-21
  • Resolved: 2013-10-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8Resolved
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
Windows:
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

Linux:
java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
* Microsoft Windows XP [Version 5.1.2600]
* Linux ernie 2.4.30 #1 Wed Apr 13 14:59:04 CEST 2005 i586 unknown


EXTRA RELEVANT SYSTEM CONFIGURATION :
Seems to be CPU independent: Happens on AMD Athlon XP, AMD K6, Intel Celeron and Pentium 4.

A DESCRIPTION OF THE PROBLEM :
  Description by example:

0.050000002607703200f converts to 0.05 (3D4CCCCD internal)
0.050000002607703201f converts to 0.05
0.050000002607703202f converts to 0.050000004 (3D4CCCCE internal)
0.050000002607703203f converts to 0.050000004
0.050000002607703204f converts to 0.050000004
0.050000002607703205f converts to 0.050000004
0.050000002607703206f converts to 0.05
0.050000002607703207f converts to 0.05
0.050000002607703208f converts to 0.05
0.050000002607703209f converts to 0.050000004

As this is obviously not a simple rounding issue, I think that  the note on direct String-to-float conversions in the documentation of Float#valueOf(ava.lang.String) does not apply here. Even if the conversion is not guaranteed to produce correctly rounded float values, I  would at least expect that the conversion of sequences of floating point literals preserves monotony.

This problem also affects the javac compiler as float literals are converted the same way.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile & run the test case.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Depending on what the note on direct String-to-float conversion in Float#valueOf(String) wants to tell me, I would expect different results.

The dream result would be:

0.05
0.050000004

But as it seems that round-to-nearest is not guaranteed to take place as expected in direct String-to-float conversion, I would also accept:

0.05
0.05

or

0.050000004
0.050000004

ACTUAL -
0.050000004
0.05


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class FloatProblem {
    public static void main( String[] args) {
        float a = 0.050000002607703203f;
        float b = 0.050000002607703207f;
        System.out.println( a );
        System.out.println( b );
    }
} 
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Avoiding direct String-to-float conversion by using intermediate doubles.

Compile time:
float a = (float) 0.050000002607703203;
   instead of
float a=0.050000002607703203f;

Run time:
(float)Double.parseDouble( someString );
  instead of
Float.parseFloat( someString );

Comments
Duplicates JDK-7192954.
21-10-2013

See core-libs-dev message: http://mail.openjdk.java.net/pipermail/core-libs-dev/2012-June/010561.html
17-09-2013