Repository navigation
Keep snapped slider values exactly on their tick - #5102
Open
rootkiller6788 wants to merge 2 commits into
Open
rootkiller6788 wants to merge 2 commits into
rootkiller6788 wants to merge 2 commits into
Conversation
(int) ((valueTo - valueFrom) / stepSize) truncates, so a range that comes out as 51.999996 in float arithmetic silently loses its last tick. With valueFrom=-1.5, valueTo=3.7 and stepSize=0.1 there are exactly 52 steps, but the slider snapped to 51 of them and every value landed off the 0.1 grid - touching -1 gave -0.9901961. The step count is now divided on the decimal values, the same way valueLandsOnTick already does it.
getValueOfTouchPosition() scaled the snapped position back up by the value range, which throws away the tick alignment it just computed. With valueFrom=0.2, valueTo=4 and stepSize=0.05 the 2.65 tick came out as 2.6499999. Same thing for 1.45, 1.7, 2.4, 2.9 and a few others, which is what material-components#4978 and getodk/collect#6424 are hitting. Build the value from the tick index instead, on the decimal values. Fixes material-components#4978
rootkiller6788
marked this pull request as ready for review
October 7, 2026 04:24
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.
Touching a slider with a fractional valueFrom/stepSize gives slightly wrong values. From #4978: with valueFrom=0.2, valueTo=4.0, stepSize=0.05, sliding to 2.65 gives 2.6499999.
Two things were wrong in getValueOfTouchPosition():
snapPosition() worked out the step count as
(int) ((valueTo - valueFrom) / stepSize). That cast truncates, so a range whose float division lands just below a whole number loses its last tick. valueFrom=-1.5, valueTo=3.7, stepSize=0.1 is exactly 52 steps but came out as 51, and then every value was off the 0.1 grid - touching -1 gave -0.9901961 and valueTo was never quite reachable.Even with the right step count, the snapped position was scaled back up by the value range (
position * (valueTo - valueFrom) + valueFrom), which throws away the tick alignment it had just computed.So the step count and the tick value are now both computed on the decimal values, the same way valueLandsOnTick already does for validation.
I checked this with a small harness that pulls the real method bodies out of BaseSlider and runs them over 19 valueFrom/valueTo/stepSize combinations, RTL included, comparing against valueFrom + k * stepSize as an exact decimal. Before the change 6 of those combinations produced values that were not exactly on a tick - one of them for all but one tick. Now none do, and every value the drag path produces passes the library's own valueLandsOnTick check.
One caveat: I don't have the Android SDK set up here, so I could not run the Robolectric suite - I only exercised the private methods directly. The two new tests in SliderRoundingErrorTest follow the existing testKnownValues_snapTouchToValue_NoRoundingError pattern and are worth a CI run to confirm.