Improve slider precision and range clamping, fixes #3206

Add clamping to slider values in both ImGui widgets and mjUI to ensure they remain within the defined min/max ranges, preventing potential floating point inaccuracies from causing values to slightly exceed the bounds.

PiperOrigin-RevId: 902660458
Change-Id: Ia153ae7907f07b890f2b5ac2e3a4be93ecae7bd3
This commit is contained in:
Yuval Tassa
2026-04-20 08:53:28 -07:00
committed by Copybara-Service
parent a8a5afc8dc
commit b2281883dd
3 changed files with 6 additions and 5 deletions
@@ -321,7 +321,7 @@ bool ImGui_Slider(const char* name, mjtNum* value, mjtNum min, mjtNum max) {
float f = *value;
const bool res = ImGui::SliderFloat(name, &f, min, max);
if (res) {
*value = f;
*value = mju_clip(f, min, max);
}
return res;
}
+3 -2
View File
@@ -849,8 +849,9 @@ static void setslider(mjuiItem* it, mjUI* ui,
rx = mju_round(rx * it->slider.divisions) / mjMAX(1, it->slider.divisions);
rx = mjMAX(0, mjMIN(1, rx));
// compute value
mjtNum val = (mjtNum)(it->slider.range[0]*(1-rx) + it->slider.range[1]*rx);
// compute value, clamp to range
mjtNum val = mju_clip(it->slider.range[0]*(1-rx) + it->slider.range[1]*rx,
it->slider.range[0], it->slider.range[1]);
// set slider position
if (it->type == mjITEM_SLIDERINT) {