Skip to content

Keep snapped slider values exactly on their tick - #5102

Open
rootkiller6788 wants to merge 2 commits into
material-components:masterfrom
rootkiller6788:fix-slider-step-count-rounding
Open

rootkiller6788 wants to merge 2 commits into
material-components:masterfrom
rootkiller6788:fix-slider-step-count-rounding

Conversation

@rootkiller6788

Copy link
Copy Markdown

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.

(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
rootkiller6788 marked this pull request as ready for review October 7, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant