dotnet / dotnet/wpf

WPF - slider precision is not correct.

Open
#8,819 11 comments 1 reaction 0 assignees View on GitHub
:construction: work in progress Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

Hello,

we use slider with this configuration
```xaml

```
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.
```csharp
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

```xaml

```

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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.