Skip to content

WPF - slider precision is not correct. #8819

@hudecekd

Description

@hudecekd

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

previous = Minimum + (Math.Round(((value - Minimum) / TickFrequency)) * TickFrequency);

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

  • Value should be set to 2.0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚧 work in progressGood First Issuemetadata: Issue should be easy to implement, good for first-time contributorsInvestigateRequires further investigation by the WPF team.help wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions