dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.1k stars 1.17k forks source link

WPF - slider precision is not correct. #8819

Open hudecekd opened 9 months ago

hudecekd commented 9 months ago

Description

Hello,

we use slider with this configuration

        <Slider
            Grid.Row="3"
            Grid.Column="1"
            IsSnapToTickEnabled="True"
            TickFrequency="0.1"
            Minimum="{Binding Gamma.Minimum}"
            Maximum="{Binding Gamma.Maximum}"
            Value="{Binding Gamma.Value}"/>

Gamma.Minimum is 0.5. Gamma.Maximum is 4. Gamma.Value is bounded. All properties are double. Problem is that when Value is 1.9 and i move slider very little then value does not change to 2 or 1.8 but it changes to 1.9000000000000001 which is not correct. Value should be 2.

I have tried to debug it and I think that I have found problem at this line https://github.com/dotnet/wpf/blob/2fdb74d7b11465f36246b7caf06fa00e9c6d8454/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Slider.cs#L1135 When * TickFrequency is used value result of multiplication brings precision problem.

Part of the code from the link above.

                else if (DoubleUtil.GreaterThanZero(TickFrequency))
                {
                    previous = Minimum + (Math.Round(((value - Minimum) / TickFrequency)) * TickFrequency);
                    next = Math.Min(Maximum, previous + TickFrequency);
                }

Reproduction Steps

Add Slider with binding to double value

        <Slider
            Grid.Row="3"
            Grid.Column="1"
            IsSnapToTickEnabled="True"
            TickFrequency="0.1"
            Minimum="0.5"
            Maximum="4"
            Value="{Binding Value}"/>

Set value to 1.9 from code. Try to move slidre a little. See problem.

Expected behavior

Actual behavior

Value is set to 1.9000000000000001.

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

lindexi commented 9 months ago

@hudecekd Thank you for your feedback. Are you willing to fix this problem?

wbc2048 commented 8 months ago

Hi! Do you still need help? I would love to work on this issue as a first-time contributor.

hudecekd commented 8 months ago

@lindexi @wbc2048 I will be glad if you try to fix it because there are many other things related to this which should be considered and I do not thing that I would be able to consider all of them. For example: I can have Min=0.5001 and use TickFrequency=0.1 which in the end should generate 1.9001. What I am trying to say is that it is not just about using Math.Round but it depends on precision on minimum and maximum also and I am not sure whether there are not other issues to consider.

So please If you know then solve it @wbc2048 . Thanks.

mr-sergito commented 1 month ago

Hey @wbc2048 are you working on this? If not, I'd like to give it a shot 😄