Skip to content

Comments

Fix Slider snapping precision issue (#8819)#11431

Open
sachPundir wants to merge 1 commit intodotnet:mainfrom
sachPundir:fix/8819-slider-snapping-precision
Open

Fix Slider snapping precision issue (#8819)#11431
sachPundir wants to merge 1 commit intodotnet:mainfrom
sachPundir:fix/8819-slider-snapping-precision

Conversation

@sachPundir
Copy link

@sachPundir sachPundir commented Feb 8, 2026

Fixes #8819

Description

This PR fixes floating-point precision issues in Slider snapping when TickFrequency is used.

Due to double precision limitations, calculations such as:

19 * 0.1 → 1.9000000000000001

can produce small rounding errors that affect snapping behavior.

This change ensures the final snapped value is rounded to a stable precision to avoid floating-point artifacts while preserving existing Slider logic.

Rounding is applied only to the final snapped value and does not modify Minimum, Maximum, or TickFrequency. This ensures that real precision coming from Min/Max offsets is preserved.

For example:

Minimum = 0.5001
TickFrequency = 0.1

continues to correctly produce values like 1.9001.

The intent is to remove IEEE floating-point noise, not to change logical tick precision.

Customer Impact

Improves numerical stability of Slider snapping when fractional TickFrequency values are used, resulting in more predictable snapped values.

Regression

No — this addresses a long-standing floating-point precision behavior rather than a recently introduced regression.

Testing

  • Reproduced the issue using a minimal WPF sample with fractional TickFrequency values (e.g., 0.1, 0.112).
  • Confirmed incorrect snapping behavior prior to the fix due to floating-point precision artifacts.
  • Verified corrected snapping behavior after the change across multiple ranges and tick frequencies.
  • Tested slider movement in both increasing and decreasing directions.
  • Tested with non-zero Minimum values to ensure correct offset handling.
  • Tested with non-zero Minimum values to ensure correct offset handling.
  • Validated behavior using values with multiple decimal places in a sample application to ensure stable snapping results.
  • Tested values close to Minimum and Maximum limits.
  • Ran the full WPF unit test suite locally — all tests pass.
  • Confirmed no behavioral changes for integer TickFrequency scenarios.

Risk

Low.

The change only affects the final snapped value calculation and does not alter core Slider logic or public APIs.

Microsoft Reviewers: Open in CodeFlow

@sachPundir sachPundir requested a review from a team February 8, 2026 14:32
@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 Feb 8, 2026
@miloush
Copy link
Contributor

miloush commented Feb 9, 2026

I don't think that's a correct approach, it will use wrong values if the tick frequency has higher precision, and the rounding number seems arbitrary.

@sachPundir
Copy link
Author

@miloush, Thanks for the clarification — you're right that snapping must remain strictly “closest tick” and not bias toward the next one.

My intent was only to remove IEEE noise when the computed value is already on a tick (e.g., 1.9000000000000001).

I agree arbitrary rounding could affect higher-precision TickFrequency.

I’ll explore a solution based on tick-index rounding or DoubleUtil.AreClose so logical tick precision is preserved.

@sachPundir
Copy link
Author

Thanks for the feedback — that makes sense.

I’ll add unit tests to cover snapping behavior and edge cases so we can validate the expected behavior more precisely before finalizing the fix.

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.

2 participants