From 555635e13e36d45ecc921671934cf13e1a945a7d Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Mon, 12 Oct 2026 08:29:55 +0200 Subject: [PATCH] Return zero from ROUNDDOWN and TRUNC at precisions beyond the number range (#540) --- crates/functions/src/math.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/functions/src/math.rs b/crates/functions/src/math.rs index bab219e..f7d80a0 100644 --- a/crates/functions/src/math.rs +++ b/crates/functions/src/math.rs @@ -75,7 +75,14 @@ fn round_dir(x: f64, digits: i32, up: bool) -> f64 { } let s = clean(scaled); let r = if up { s.ceil() } else { s.floor() }; - let out = if neg { r * f } else { clean(r / f) }; + // r == 0 with an infinite scale would be 0 * inf = NaN (#NUM!); the result is simply zero. + let out = if !neg { + clean(r / f) + } else if r == 0.0 { + 0.0 + } else { + r * f + }; if x < 0.0 { -out } else { out } } @@ -1373,6 +1380,13 @@ mod tests { close(ev2("ROUNDDOWN", -3.9, 0.0), -3.0); close(ev2("ROUNDDOWN", 2.71989, 3.0), 2.719); close(ev2("ROUNDDOWN", 31415.92654, -2.0), 31400.0); + for digits in [-308.0, -309.0, -400.0] { + for x in [1234.0, -1234.0, 0.5] { + close(ev2("ROUNDDOWN", x, digits), 0.0); + close(ev2("TRUNC", x, digits), 0.0); + } + } + assert!(matches!(ev2("ROUNDUP", 1234.0, -309.0), Value::Error(CellError::Num))); close(ev2("ROUNDUP", 0.1 + 0.2, 1.0), 0.3); close(ev2("TRUNC", 8.987, 2.0), 8.98); close(ev1("TRUNC", -8.9), -8.0);