<Swing Dev> [15] RFR JDK-8220811: SpinnerNumberModel floating point rounding issue

Pankaj Bansal pankaj.b.bansal at oracle.com
Fri Feb 7 08:25:05 UTC 2020


Hi All,

Please review the following fix for jdk15.


Bug: 

https://bugs.openjdk.java.net/browse/JDK-8220811

 

webrev:

http://cr.openjdk.java.net/~pbansal/8220811/webrev00/

 

Issue:

In case of JSpinner with double/float values,  sometime the spinner value does not change even though the value is within the min, max range. 

 

Cause:

The bug is caused by errors in floating point math. 

 

Eg, if we create a JSpinner with min=0.15, max=1.0, stepsize =.05, if current value is -.10, it is not possible to go to -.15 by pressing the decrement button.

a=-.10, b=-.05, the c=a+b is not equal to -.15. Instead is something like -.150000000345. This caused issues as this values is considered lower than -.15, which minimum value allowed for the JSpinner. So the value of spinner cannot be decreased to -.15, though it should be possible.

 

Fix:

The fix is different for double and float values.

 

For double values, just using the BigDecimal class to do the floating point math operations solves the issues. This change is needed for float values as well along with the change mentioned below.

 

For float, there is one addition issue. There is no constructor in SpinnerNumberModel, which will accept float values. There is a constructor which accepts double values. So, even if all float values are passed to SpinnerNumberModel, the constructor accepting double values is called. So, the float values are implicitly casted to double. This implicit casting causes issue because if float a=.95, double b = a, then b is not exactly equal to .95. Instead it is something like .950000045. So in case of float values, the issue starts on from the creation of SpinnerNumberModel. So a new constructor for float values is added to the SpinnerNumberModel class.

 

This fix will need CSR, I will get to it after the review is completed here.


Regards,
Pankaj Bansal

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/swing-dev/attachments/20200207/ae4083a9/attachment.htm>


More information about the swing-dev mailing list