Search code examples
iosuisliderfloating-accuracy

Does setting min/max of UISlider change accuracy by rounding


Assume I have a UISlider representing a continuous range of values from -1 to 2. If I leave the default min=0, max=1, then my value of 0 is represented by the slider being 1/3 of travel and a float value of 1/3 (0.33333333). I am particularly interested in the special value 0 and representing it as 0.333333 which will have to be (slightly) rounded feels wrong. If I were to change the minimum to -1 and the maximum to 2 then my 0 value is float value 0.0 exactly.

Would setting the min/max give me more accuracy? Does the thumb move on pixel boundaries - maybe I could use that information to try some rounding examples?


Solution

  • I suspect you are worried about inconsequential things here. First, single-precision floats have 23 bits in their fractions, so any rounding error is less than one part in 2**24. That is hugely beyond the resolution of the screen, so no effects of rounding will be visible in the display. Second, even if you set round values for the endpoints, the slider coordinates still have to be mapped to screen pixels and vice-versa by other software, and those are likely to involve non-round values.