From d3cd393dba85dcf72dba4f1bc2e8bd6ba8f01af4 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 30 Oct 2026 20:25:09 -0500 Subject: [PATCH] G719: return NA for empty harmonic mean input --- crates/functions/src/stat/descriptive.rs | 5 ++++- crates/functions/src/stat/tests.rs | 13 +++++++++++++ docs/function-parity.md | 7 ++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/functions/src/stat/descriptive.rs b/crates/functions/src/stat/descriptive.rs index af4c53607..0cf8068f6 100644 --- a/crates/functions/src/stat/descriptive.rs +++ b/crates/functions/src/stat/descriptive.rs @@ -420,7 +420,10 @@ pub(super) fn geomean(v: &[f64]) -> R { } pub(super) fn harmean(v: &[f64]) -> R { - if v.is_empty() || v.iter().any(|&x| x <= 0.0) { + if v.is_empty() { + return Err(CellError::NA); + } + if v.iter().any(|&x| x <= 0.0) { return Err(CellError::Num); } Ok(v.len() as f64 / v.iter().map(|x| 1.0 / x).sum::()) diff --git a/crates/functions/src/stat/tests.rs b/crates/functions/src/stat/tests.rs index 87ce19fac..8e259c546 100644 --- a/crates/functions/src/stat/tests.rs +++ b/crates/functions/src/stat/tests.rs @@ -151,6 +151,19 @@ fn spread() { let g = [4.0, 5.0, 8.0, 7.0, 11.0, 4.0, 3.0]; close_tol(ev("GEOMEAN", vec![c(&g)]), 5.476986969, 1e-9); close_tol(ev("HARMEAN", vec![c(&g)]), 5.028375962, 1e-9); + let text_values = arr(vec![vec![tv("1"), tv("2")]]); + let bool_values = arr(vec![vec![Value::Bool(true), Value::Bool(false)]]); + let mixed_values = arr(vec![vec![nv(2.0), tv("ignored"), Value::Bool(true)]]); + is_err(ev("HARMEAN", vec![av(text_values.clone())]), CellError::NA); + is_err(ev("HARMEAN", vec![av(bool_values.clone())]), CellError::NA); + is_err(ev("HARMEAN", vec![rf(text_values)]), CellError::NA); + is_err(ev("HARMEAN", vec![rf(bool_values)]), CellError::NA); + close(ev("HARMEAN", vec![av(mixed_values.clone())]), 2.0); + close(ev("HARMEAN", vec![rf(mixed_values)]), 2.0); + close(ev("HARMEAN", vec![b(true)]), 1.0); + close(ev("HARMEAN", vec![n(2.0), n(4.0)]), 8.0 / 3.0); + is_err(ev("HARMEAN", vec![av(arr(vec![vec![nv(0.0)]]))]), CellError::Num); + is_err(ev("HARMEAN", vec![av(arr(vec![vec![nv(-1.0)]]))]), CellError::Num); is_err(ev("GEOMEAN", vec![c(&[1.0, 0.0])]), CellError::Num); close_tol(ev("TRIMMEAN", vec![c(&[4.0, 5.0, 6.0, 7.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0]), n(0.2)]), 3.777777778, 1e-9); is_err(ev("TRIMMEAN", vec![c(&[1.0]), n(1.0)]), CellError::Num); diff --git a/docs/function-parity.md b/docs/function-parity.md index bf57fcd3e..735c4b5e2 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-25 · **Change:** minor (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 (HARMEAN returns `#N/A` when its arguments contain no numeric observations) · **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). @@ -97,6 +97,10 @@ This improves series preparation; it does not change the model-fitting or season ## Depth (estimated) +| Function | Excel parity note | +|---|---| +| HARMEAN | Returns `#N/A` when arrays and references contain no numeric observations; zero and negative numeric observations still return `#NUM!`. | + **~82% ready.** The library is broad and has 96 test functions in `crates/functions` with many assertions each, but there is no conformance suite against Excel-computed results, and user reports keep finding edge cases: MIN() with no arguments (#138), COUNTBLANK past the used range @@ -123,6 +127,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-31 | minor | HARMEAN returns `#N/A` when array or reference inputs contain no numbers; nonpositive numeric observations remain `#NUM!` | | 2026-10-25 | minor | LAMBDA row: a LAMBDA that calls itself more than 140 times, and a formula nested past the depth limit, give `#NUM!` (#1809; nesting gave `#CALC!` before). `=Foo(1)` where the defined name Foo holds `#N/A` gives `#N/A`, not `#NAME?` | | 2026-10-23 | minor | Number text coercion (VALUE, arithmetic, typed entry): a dollar sign before accounting parentheses (`$(1,234)`, `$ (5)`) and a percent sign after them (`(5)%`) read as negatives; `(-5)`, `((5))`, `($-5)`, `$5%`, `$(5)%` and dates or times in parentheses are `#VALUE!`, as observed in Excel 365 | | 2026-10-23 | minor | INDEX with only a row index on a reference of several rows and columns (`INDEX(A1:C3,2)`, `INDEX(A1:C3,0)`) is `#REF!`, as observed in Excel, so it also fails as a range endpoint and inside other functions; an empty column argument (`INDEX(A1:C3,2,)`) still gives the row. Checked that a column array with one index stays a 1×1 array (`TYPE` 64), which is what Excel returns |