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

Alexey Ivanov alexey.ivanov at oracle.com
Wed Feb 12 16:21:06 UTC 2020


Hi Sergey, Pankaj,

On 12/02/2020 07:59, Sergey Bylokhov wrote:
> On 2/11/20 11:52 pm, Pankaj Bansal wrote:
>> Hi Sergey,
>>
>> << Sometimes there are difference:
>> Ok, I see it now. So, should I just close this bug as not an issue 
>> with proper comments or make changes using Math.fma for best possible 
>> results? The probability of issue happening will decrease using 
>> Math.fma(b, c, a) as compared to normal a+b*c.
>
> According to the source code, the multiplication is needed only to 
> change the sign of the result so c is always -1 or 1, not sure is it 
> possible to get some rounding issues or not(need to check somehow)?

Can it be replaced with simple addition / subtraction:
newValue = currentValue - step;
or
newValue = currentValue + step;



On another note, we know there's always a possibility for rounding 
errors; in addition, many decimal fractions cannot be represented 
exactly as a binary fraction. Can we solve the problem another way then?

The bug report says that going from -0.15 to -0.10 does not allow going 
back to -0.15. This happens because the result of this sequence of 
operations cannot be represented exactly, or, in other words, because of 
rounding errors; or rather the result is less than the set minimal value.

Can we set the value of the spinner to the set minimal value instead of 
disallowing the operation. I mean, after going up the displayed value is 
-0.10; going down by 0.05 gives the result which is less than the 
minimal value for the spinner, and thus going down is not allowed. What 
if we set the value of the spinner to its minimal value instead?

Would it work? It should.

>
>>
>> Regards,
>> Pankaj
>>
>> -----Original Message-----
>> From: Sergey Bylokhov
>> Sent: Wednesday, February 12, 2020 1:09 PM
>> To: Pankaj Bansal <pankaj.b.bansal at oracle.com>; 
>> swing-dev at openjdk.java.net
>> Subject: Re: <Swing Dev> [15] RFR JDK-8220811: SpinnerNumberModel 
>> floating point rounding issue
>>
>>> I am not able to find any issue with this approach.
>> Sometimes there are difference:
>>
>> double b = 0.10000000000000011;
>>
>> constructor [1]: 
>> 0.1000000000000001165734175856414367444813251495361328125
>> constructor [2]: 0.10000000000000012
>>
>>> <<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)
>>>
>>> Yes, it is true. If we create "Float" objects from primitive float 
>>> and pass to the constructor, things work fine. But what if the user 
>>> is passing the primitive float values directly? As the double 
>>> constructor is there, it is called instead of this constructor which 
>>> accepts objects. This creates issue.
>>> I don’t see this in spec that users should not use primitive float 
>>> directly and should first create "Float" object and then create 
>>> SpinnerNumberModel. So they will use primitive floats directly and 
>>> run into these issues.
>>
>> This is described in the top level specification of this class.
>>
>>> As I suggested earlier, either we should remove constructors with 
>>> primitive double or add constructor for primitive float.
>>
>> The primitive variants are there because it was decided that 
>> "integers and doubles" are quite common, so they have special 
>> constructors, this is also described in the top-level spec of the class.
>>
-- 
Regards,
Alexey


More information about the swing-dev mailing list