Fix Slider snap-to-tick floating-point precision (#8819)#11719
Open
JyslaFancy wants to merge 1 commit into
Open
Fix Slider snap-to-tick floating-point precision (#8819)#11719JyslaFancy wants to merge 1 commit into
JyslaFancy wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Microsoft Reviewers: Open in CodeFlow