Fix Slider snapping precision issue (#8819)#11431
Fix Slider snapping precision issue (#8819)#11431sachPundir wants to merge 2 commits intodotnet:mainfrom
Conversation
|
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. |
|
@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. |
|
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. |
|
I ran additional edge-case tests beyond the original unit tests to validate the behavior across different tick magnitudes. Observations: ✅ The change improves decimal-grid snapping for common cases (e.g., 0.1, 0.0001) by eliminating visible floating-point artifacts such as 1.9000000000000001. Example:
Because of this regression, the current approach is not safe for all tick magnitudes. I am refining the solution to preserve small-tick precision while still addressing decimal-grid floating artifacts. I’ll follow up with an updated approach shortly. |
bfbe9b6 to
8136472
Compare
|
@dotnet-policy-service agree |
|
I’ve updated the snapping reconstruction step to use decimal arithmetic when rebuilding the snapped tick value. The rounding of the tick index remains unchanged. Previously, reconstructing the snapped value using double multiplication (e.g. 19 * 0.1) could introduce binary floating-point representation artifacts such as 1.9000000000000001. This change keeps the existing snapping logic intact and replaces only the reconstruction step with decimal arithmetic. This eliminates representation noise for common base-10 tick frequencies (0.1, 0.01, 0.0001, etc.) while preserving full precision for high-precision tick frequencies (e.g. 1e-12). A double-based fallback is retained for values outside the decimal range to avoid overflow. Existing snapping behavior is unchanged apart from stabilizing the reconstructed value. |
…facts Reconstructing the snapped tick value using double multiplication (e.g. 19 * 0.1) can introduce binary floating-point artifacts such as 1.9000000000000001. This change keeps the existing snapping logic intact and performs the final reconstruction step using decimal arithmetic. Decimal is exact for common base-10 tick frequencies (0.1, 0.01, 0.0001, etc.), which eliminates representation noise while preserving behavior. A double-based fallback is retained for values outside decimal's supported range.
7e89dad to
e2f7a87
Compare
|
@dipeshmsft @miloush This PR fixes the slider snapping precision issue caused by floating point rounding when calculating tick values. I verified the behavior using a Slider with TickFrequency and confirmed the corrected snapping logic resolves the precision problem. Could you please review it when you have time? Happy to update the PR if any changes or additional tests are required. |
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
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