Skip to content

[Slider] Displayed slider values (minus valueFrom) are sometimes not integer multiples of stepsize due to floating point arithmetic #4978

Description

@capmlbpkel

Description:

  1. Open slider
  2. Slide to 2.65x
  3. Value is shown as 2.6499999x

Expected behavior:

Value is shown as 2.65x (rounding would mitigate the issue)

Source code: Taken from https://github.com/libre-tube/LibreTube/blob/master/app/src/main/res/layout/playback_bottom_sheet.xml:

                        <com.google.android.material.slider.Slider
                            android:id="@+id/speed"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:stepSize="0.05"
                            android:value="1.0"
                            android:valueFrom="0.2"
                            android:valueTo="4.0"
                            android:layout_marginTop="5dp"
                            android:layout_marginHorizontal="10dp" />

Minimal sample app repro: Not a minimal example since I do not develop for android, but a repo where the problem occurs: https://github.com/libre-tube/LibreTube. Related issue: libre-tube/LibreTube#7949

Android API version: 35 (taken from https://github.com/libre-tube/LibreTube/blob/master/app/build.gradle.kts, I hope this is correct)

Material Library version: 1.14.0-alpha06

Device: G7 Power / ocean

Activity

  1. lognaturel commented on Dec 9, 2025

    @lognaturel

    We've also run into this: getodk/collect#6424 We were going to address it at the application level but I agree that ideally the component would avoid this.

  2. removed their assignment
    on Jul 24, 2026
  3. rootkiller6788 commented on Sep 17, 2026

    @rootkiller6788

    Looking at this one. The 2.65 case comes from getValueOfTouchPosition() scaling the snapped position back up by the value range (position * (valueTo - valueFrom) + valueFrom) instead of counting whole steps from valueFrom, so the tick alignment snapPosition() just computed gets thrown away again.

    There's a second one right next to it: snapPosition() does int stepCount = (int) ((valueTo - valueFrom) / stepSize), and that cast truncates when the float division lands just below a whole number. With valueFrom=-1.5, valueTo=3.7, stepSize=0.1 the range is exactly 52 steps but it comes out as 51, so every value ends up off the 0.1 grid (touching -1 gives -0.9901961) and valueTo is never quite reachable.

    Working on a fix for both, with tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions