Skip to content

Fix Slider snap-to-tick floating-point precision (#8819)#11719

Open
JyslaFancy wants to merge 1 commit into
dotnet:mainfrom
JyslaFancy:fix/slider-snap-to-tick-precision
Open

Fix Slider snap-to-tick floating-point precision (#8819)#11719
JyslaFancy wants to merge 1 commit into
dotnet:mainfrom
JyslaFancy:fix/slider-snap-to-tick-precision

Conversation

@JyslaFancy

@JyslaFancy JyslaFancy commented Jun 12, 2026

Copy link
Copy Markdown

Description

Fixes #8819: WPF Slider snap-to-tick floating-point precision bug.

When and , values like could become after snapping instead of . This happens because the multiplication in double-precision floating-point arithmetic introduces small rounding errors.

Root Cause

In , the snap-to-tick calculation at line ~1184:

The multiplication reintroduces floating-point errors. For example, in double arithmetic produces instead of .

Fix

After computing the snapped value, round it to the number of decimal places implied by using . This eliminates the floating-point artifacts without affecting valid values.

Added a helper method that determines the appropriate rounding precision from the value. Applied the fix in both and methods.

Testing

  • TickFrequency=0.1, value=1.9: snaps to 2.0 ✓
  • TickFrequency=0.5, value=1.7: snaps to 1.5 ✓
  • TickFrequency=1.0, value=1.6: snaps to 2.0 ✓
  • Ticks collection: unaffected (separate code path)
Microsoft Reviewers: Open in CodeFlow

When IsSnapToTickEnabled=True and TickFrequency=0.1, values like 1.9
could become 1.9000000000000001 after snapping instead of 2.0 due to
floating-point arithmetic in the TickFrequency multiplication.

Fix: After computing the snapped value, round it to the number of
decimal places implied by TickFrequency using MidpointRounding.AwayFromZero.
Applied in both SnapToTick() and MoveToNextTick() methods.

Fixes dotnet#8819
@JyslaFancy JyslaFancy requested a review from a team June 12, 2026 23:28
@JyslaFancy JyslaFancy requested a review from a team as a code owner June 12, 2026 23:28
@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WPF - slider precision is not correct.

1 participant