Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Expressions: floor and ceil of 3 in / 1 in are 3, not 2 (#12489)

Steve Jasonc29f396f7299
diff --git a/crates/doc/src/expr.rs b/crates/doc/src/expr.rs
index 8a95cda3a1..47a878ec52 100644
--- a/crates/doc/src/expr.rs
+++ b/crates/doc/src/expr.rs
@@ -982,6 +982,12 @@ fn call(f: &str, args: &[Value], d: Defaults) -> Result<Value, DocError> {
// Round in the value's own default unit (round(2.4 in) = 2 in for an inch parameter).
let scale = scale_bare(Value::num(1.0), a.len, a.ang, d);
let x = if scale != 0.0 { a.v / scale } else { a.v };
+ // Unit conversions leave a rounding error of an ulp or two (`3 in / 1 in` is
+ // 2.9999999999999996): a value that close to a whole number is that number, so `floor`
+ // does not drop to 2. The tolerance is relative to the whole number, so a tiny value
+ // never snaps to 0, and it stays under 1/8 so no real fraction is absorbed.
+ let nearest = x.round();
+ let x = if (x - nearest).abs() <= (4.0 * f64::EPSILON * nearest.abs()).min(0.125) { nearest } else { x };
let r = match f {
"floor" => x.floor(),
"ceil" => x.ceil(),
@@ -1444,6 +1450,43 @@ mod tests {
}
}
+ /// `floor`, `ceil`, `trunc` and `round` work on the number a person typed, not on float noise:
+ /// `3 in / 1 in` is 2.9999999999999996 in floats, and `floor` of it must still be 3.
+ #[test]
+ fn rounding_functions_ignore_float_noise() {
+ for (e, want) in [("floor(3 in / 1 in)", 3.0), ("ceil(3 in / 1 in)", 3.0), ("trunc(3 in / 1 in)", 3.0), ("floor(-3 in / 1 in)", -3.0)] {
+ assert_eq!(ev(e, Kind::Unitless).unwrap(), want, "{e}");
+ }
+ let d = Defaults { len: 25.4, ang: std::f64::consts::PI / 180.0 };
+ let look = |_: &str| Err(DocError::Expr("no parameters".into()));
+ for e in ["floor(3 in)", "ceil(3 in)", "trunc(3 in)", "round(3 in)"] {
+ let got = eval_with_defaults(e, &look, d).unwrap().to_kind_in(Kind::Length, d).unwrap();
+ assert!((got - 76.2).abs() < 1e-9, "{e} gave {got}");
+ }
+ let got = eval_with_defaults("floor(30 deg)", &look, Defaults::default()).unwrap().to_kind(Kind::Angle).unwrap();
+ assert!((got - 30f64.to_radians()).abs() < 1e-9, "floor(30 deg) gave {got}");
+ // A real fraction still rounds the way its name says, however small, and large values keep working.
+ for (e, want) in [
+ ("floor(2.9999)", 2.0),
+ ("ceil(3.0001)", 4.0),
+ ("floor(100.99999995)", 100.0),
+ ("ceil(100.00000005)", 101.0),
+ ("floor(1000000000.7)", 1_000_000_000.0),
+ ("ceil(1000000000.7)", 1_000_000_001.0),
+ ("trunc(1000000000.7)", 1_000_000_000.0),
+ ("round(1000000000.7)", 1_000_000_001.0),
+ ("ceil(1e-16)", 1.0),
+ ("floor(-1e-16)", -1.0),
+ ("trunc(1e-16)", 0.0),
+ ("trunc(-1e-16)", 0.0),
+ ("round(1e-16)", 0.0),
+ ("round(-1e-16)", 0.0),
+ ("floor(1000000000000000.5)", 1_000_000_000_000_000.0),
+ ] {
+ assert_eq!(ev(e, Kind::Unitless).unwrap(), want, "{e}");
+ }
+ }
+
/// A count from a length (`(L - 20) / 210`, the 210 read as unit-less) names the fix (#5901).
#[test]
fn a_length_where_a_count_is_expected_says_to_divide_by_a_length() {