Skip to content

Commit af8bdac

Browse files
committed
Use r128.h for high precision snapping
1 parent eee7ce6 commit af8bdac

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

scene/gui/range.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@
3030

3131
#include "range.h"
3232

33+
#include "thirdparty/misc/r128.h"
34+
35+
double Range::_snapped_r128(double p_value, double p_step) {
36+
if (p_step != 0) {
37+
// All these lines are the equivalent of: p_value = Math::floor(p_value / p_step + 0.5) * p_step;
38+
// Convert to String to force rounding to a decimal value (not a binary one).
39+
String step_str = String::num(p_step);
40+
String value_str = String::num(p_value);
41+
R128 step_r128;
42+
R128 value_r128;
43+
const R128 half_r128 = R128(0.5);
44+
r128FromString(&step_r128, step_str.ascii().get_data(), nullptr);
45+
r128FromString(&value_r128, value_str.ascii().get_data(), nullptr);
46+
r128Div(&value_r128, &value_r128, &step_r128);
47+
r128Add(&value_r128, &value_r128, &half_r128);
48+
r128Floor(&value_r128, &value_r128);
49+
r128Mul(&value_r128, &value_r128, &step_r128);
50+
p_value = value_r128;
51+
}
52+
return p_value;
53+
}
54+
3355
PackedStringArray Range::get_configuration_warnings() const {
3456
PackedStringArray warnings = Control::get_configuration_warnings();
3557

@@ -140,9 +162,8 @@ void Range::_set_value_no_signal(double p_val) {
140162

141163
double Range::_calc_value(double p_val, double p_step) const {
142164
if (p_step > 0) {
143-
// TODO: In the future, change `step` to a more suitable type for more robust precise calculations.
144165
// Subtract min to support cases like min = 0.1, step = 0.2, snaps to 0.1, 0.3, 0.5, etc.
145-
p_val = Math::snapped(p_val - shared->min, p_step) + shared->min;
166+
p_val = _snapped_r128(p_val - shared->min, p_step) + shared->min;
146167
}
147168

148169
if (_rounded_values) {

scene/gui/range.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Range : public Control {
6262
void _set_value_no_signal(double p_val);
6363

6464
protected:
65+
static double _snapped_r128(double p_value, double p_step);
6566
double _calc_value(double p_val, double p_step) const;
6667
virtual void _value_changed(double p_value);
6768
void _notify_shared_value_changed() { shared->emit_value_changed(); }

0 commit comments

Comments
 (0)