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

Alexey Ivanov alexey.ivanov at oracle.com
Mon Feb 17 10:25:22 UTC 2020


On 17/02/2020 10:10, Sergey Bylokhov wrote:
> On 2/17/20 1:50 am, Alexey Ivanov wrote:
>> Otherwise there will always be cases where Spinner with float/double 
>> behaves unexpectedly, i.e. does not allow increasing/decreasing a 
>> value where one expects it to.
>
> But what value is expected/unexpected? If the programmer wants to use 
> float arithmetic and allow the user to select some value in the 
> diapason by the JSpinner, is it expected that spinner return values 
> which are exactly duplicate simple steps of float addition or not?

This would be expected, I'd say. I mean the steps of float addition 
subtractions because this is how it works in maths. But it's not always 
possible with floating point calculations in computers.

>
>> This new approach should also work for int too; although it's 
>> redundant as integer arithmetic is precise.
>
> As far as I understand your approach, is to select the minimum if the 
> resulted float value after the calculation is a little bit smaller 
> than the minimum.
>
> So if the minimum is -0.15 and resulted value is (-0.10d - 0.05d) = 
> (-0.15000000000000002), what value should be shown by the spinner?
> the same logic for int:
>       the minimum is -15 and resulted value is (-11 - 5) = -16, what 
> value should be shown by the spinner?
>
The set minimum value: -0.15d and -15 correspondingly.

-- 
Regards,
Alexey


More information about the swing-dev mailing list