Hi, Pankaj.
Could you please try to use Math.fma instead of direct using of
xxx.toString+BigDecimal
if (value instanceof Double) {
newValue = Math.fma(stepSize.doubleValue(), dir, value.doubleValue());
} else {
newValue = Math.fma(stepSize.floatValue(), dir, value.floatValue());
}
Are you sure that it is necessary to add a new constructor? As far as I
understand it is
possible to pass a float value via:
public SpinnerNumberModel(Number value,
Comparable<?> minimum,
Comparable<?> maximum,
Number stepSize) {
On 2/7/20 12:25 am, Pankaj Bansal wrote:
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
--
Best regards, Sergey.