mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-03 00:39:44 -06:00
Keep huge finite values finite when stepping number fields (#1753)
Found while testing crates/ui-egui/src/widgets/field_math.rs:168 (stepped, used by widgets.rs:131 scrubbed) Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
b957160379
commit
3b5de55587
@@ -167,7 +167,12 @@ pub fn eval(text: &str, cur: Option<f64>) -> Option<f64> {
|
||||
/// (0.1 + 0.2).
|
||||
pub fn stepped(from: f64, steps: f64, step: f64) -> f64 {
|
||||
let v = from + steps * step;
|
||||
if v.is_finite() { (v * 1e6).round() / 1e6 } else { from }
|
||||
if !v.is_finite() {
|
||||
return from;
|
||||
}
|
||||
let rounded = (v * 1e6).round() / 1e6;
|
||||
// Past ~1.8e302 the scaling overflows; such a value has no dust to remove.
|
||||
if rounded.is_finite() { rounded } else { v }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -182,4 +187,10 @@ mod tests {
|
||||
assert_eq!(parse("50 %", true), Some(Typed::Value(50.0)));
|
||||
assert_eq!(parse("3 px + 2 px", true), Some(Typed::Value(5.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stepped_keeps_a_huge_finite_value_finite() {
|
||||
assert_eq!(stepped(1e305, 1.0, 1.0), 1e305);
|
||||
assert!(stepped(f64::MAX / 2.0, 0.0, 1.0).is_finite());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user