From 52d317c8ffff9fcac9b2a48e6353aa3b87cb4518 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:23:51 -0500 Subject: [PATCH] Validate places for negative base conversions --- crates/functions/src/engineering.rs | 28 ++++++++++++++++++++++------ docs/function-parity.md | 4 +++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/functions/src/engineering.rs b/crates/functions/src/engineering.rs index dbe6734d3..d00966671 100644 --- a/crates/functions/src/engineering.rs +++ b/crates/functions/src/engineering.rs @@ -62,6 +62,15 @@ fn format_base(v: i64, radix: u32, places: Option) -> R { if v < min || v > max { return Err(CellError::Num); } + let places = places + .map(|p| { + let p = p.trunc(); + if !(1.0..=10.0).contains(&p) { + return Err(CellError::Num); + } + Ok(p as usize) + }) + .transpose()?; if v < 0 { let u = v + (1i64 << bits); return text_val(to_radix(u, radix)); @@ -70,11 +79,6 @@ fn format_base(v: i64, radix: u32, places: Option) -> R { match places { None => text_val(s), Some(p) => { - let p = p.trunc(); - if !(1.0..=10.0).contains(&p) { - return Err(CellError::Num); - } - let p = p as usize; if s.len() > p { return Err(CellError::Num); } @@ -97,7 +101,13 @@ fn to_radix(mut v: i64, radix: u32) -> String { } fn places_arg(a: &[Arg]) -> R> { - if has(a, 1) { Ok(Some(num(a, 1)?)) } else { Ok(None) } + if !has(a, 1) { + return Ok(None); + } + if matches!(a.get(1).map(scalar), Some(Value::Bool(_))) { + return Err(CellError::Value); + } + Ok(Some(num(a, 1)?)) } fn convert_base(a: &[Arg], from: u32, to: u32) -> R { @@ -402,6 +412,12 @@ mod tests { is_text(ev("BIN2OCT", vec![n(1111111111.0)]), "7777777777"); is_text(ev("DEC2BIN", vec![n(9.0), n(4.0)]), "1001"); is_text(ev("DEC2BIN", vec![n(-100.0)]), "1110011100"); + is_err(ev("DEC2BIN", vec![n(-1.0), n(0.0)]), CellError::Num); + is_err(ev("DEC2HEX", vec![n(-1.0), n(11.0)]), CellError::Num); + is_err(ev("DEC2OCT", vec![n(-1.0), n(-1.0)]), CellError::Num); + is_err(ev("DEC2BIN", vec![n(1.0), crate::Arg::val(Value::Bool(true))]), CellError::Value); + is_text(ev("DEC2BIN", vec![n(-1.0), n(1.0)]), "1111111111"); + is_text(ev("DEC2HEX", vec![n(-1.0), n(10.0)]), "FFFFFFFFFF"); is_err(ev("DEC2BIN", vec![n(512.0)]), CellError::Num); is_text(ev("DEC2HEX", vec![n(100.0), n(4.0)]), "0064"); is_text(ev("DEC2HEX", vec![n(-54.0)]), "FFFFFFFFCA"); diff --git a/docs/function-parity.md b/docs/function-parity.md index d9823e8bf..e23a196fa 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-27 · **Change:** minor (ECMA.CEILING listed with USDOLLAR as not on Microsoft's list; earlier: IMSQRT reproduces the stray real part Excel gives for negative reals, and the other IM* differences from Excel are listed; earlier: the LAMBDA row lists the recursion limit and its `#NUM!`, and the error a defined name holding an error gives when called; earlier: number text with accounting parentheses coerces like Excel 365; INDEX with one index on a 2-D reference is #REF! as in Excel; earlier: SUBTOTAL and AGGREGATE cap the cells they visit, #2175) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-31 · **Last updated:** 2026-10-31 · **Change:** minor (base-conversion places are validated for negative inputs and reject Boolean arguments, as observed in Excel; earlier: ECMA.CEILING listed with USDOLLAR as not on Microsoft's list; IMSQRT reproduces Excel's stray real part for negative reals) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -29,6 +29,8 @@ Excel's worksheet functions, category by category, against GridCraft's function ## Coverage +| 2026-10-31 | minor | DEC2BIN, DEC2HEX and DEC2OCT validate `places` before returning negative two's-complement output, and Boolean places return `#VALUE!`, matching Excel | + **Measured: 513 / 523 by name (98.1%); 504 / 523 working (96.4%).** | Category | Excel | Ours | Working | % working | Missing |