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

Sergey Bylokhov Sergey.Bylokhov at oracle.com
Tue Feb 11 20:47:02 UTC 2020


On 2/11/20 3:40 am, Pankaj Bansal wrote:
> << Could you please try to use Math.fma instead of direct using of xxx.toString+BigDecimal
> I tried using this, but Math.fma will not solve our issue of precision here. Math.fma internally creates BigDecimal, but it uses the [1] type constructor to create the BigDecimal. The [1] constructor takes the primitive double value and it tries to represent the primitive double as precisely as possible and adds the extra precision. After the calculations in fma, the BigDecimal is rounded off to create double, but as it has very high precision, the issue in spinner is still there.

So if the constructor [2] does not add an additional precision means that we actually lost the precision. What will happen if the user will use a tiny double parameters which is outside of your proposal? like 0.00000000000000001?

I guess we should do the best to try not to lost a precision, and leave rounding issues as-is since this is well just floating arithmetics which works according the specification of SpinnerNumberModel.getNextValue: " @return <code>value + stepSize</code> "

So we can use fma for the best result, or we could close this as not a bug.

> In my changes in webrev00, I have used the [2] form of constructor, which does not add extra precision and works fine for our case.
> I have created a dummy patch if you would like to try out this change with Math.fma (http://cr.openjdk.java.net/~pbansal/8220811/dummy.patch)
> 
> [1] new BigDecimal(double d)
> [2] new BigDecimal(String s)
> https://stackoverflow.com/questions/7186204/bigdecimal-to-use-new-or-valueof
> 
> 
> 
> << 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:
> In java the implicit type promotion (auto-widening) is preferred over the auto boxing/unboxing. The SpinnerNumberModel has two constructors, one accepts all primitive "double" arguments [4] and other accepts Objects [3]. Now when SpinnerNumberModel is passed primitive "float" values, it looks for constructor which accepts all primitive floats. As such constructor is not available, it checks for alternatives. It has two alternates, either box the primitive "float" into object "Float" and call [3] or widen the primitive "float" to primitive  "double". As auto widening is preferred over auto boxing, [4] is called instead of [3]. As I mentioned in previous mail, implicitly casting primitive  float to primitive  double adds precision issues

But it is possible to create Float.valueOf(float) and pass it to this constructor, isn't it? this will work for any primitives.
[3]     public SpinnerNumberModel(Number value,
                                 Comparable<?> minimum,
                                 Comparable<?> maximum,
                                 Number stepSize)





-- 
Best regards, Sergey.


More information about the swing-dev mailing list