XIRR and XNPV: match Excel for blank first rows, blank dates, text values and a zero first flow

This commit is contained in:
Harrison Booth committed 2026-10-26 07:31:02 -05:00
1 parent 4b86dc9dc9
commit e3fa42607f
2 files changed
+56 -11

No files matched your search

+52 -9
View File
@@ -271,20 +271,27 @@ fn dated_flows(values: &Value, dates: &Value, lenient: bool) -> R<(Vec<f64>, Vec
}
let mut v = Vec::with_capacity(va.data.len());
let mut d = Vec::with_capacity(da.data.len());
for (x, y) in va.data.iter().zip(da.data.iter()) {
for (i, (x, y)) in va.data.iter().zip(da.data.iter()).enumerate() {
// Excel (XNPV): a blank or text value or a blank date is #NUM!, except in the first row,
// where it is #VALUE!.
let strict_err = if i == 0 { CellError::Value } else { CellError::Num };
let value = match x {
Value::Number(n) => *n,
Value::Empty if lenient => 0.0,
Value::Empty => return Err(CellError::Num),
Value::Empty => return Err(strict_err),
Value::Error(e) => return Err(*e),
_ => return Err(CellError::Value),
_ if lenient => return Err(CellError::Value),
_ => return Err(strict_err),
};
match y {
Value::Number(n) => d.push(n.trunc()),
Value::Error(e) => return Err(*e),
Value::Text(_) => d.push(y.to_number()?.trunc()),
Value::Empty if lenient && value == 0.0 => continue,
Value::Empty if lenient || value == 0.0 => return Err(CellError::Num),
Value::Empty if !lenient => return Err(strict_err),
// XIRR skips a blank date next to a 0 flow and reads one in the first row as day 0.
Value::Empty if value == 0.0 => continue,
Value::Empty if i == 0 => d.push(0.0),
Value::Empty => return Err(CellError::Num),
_ => return Err(CellError::Value),
}
v.push(value);
@@ -455,6 +462,22 @@ fn xirr(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
if guess <= -1.0 || !v.iter().any(|x| *x > 0.0) || !v.iter().any(|x| *x < 0.0) {
return Err(CellError::Num);
}
if v.first() == Some(&0.0) {
// Excel with a 0 (or blank) first flow: the residual scale is 0, so the search just
// halves the guess towards 0 until the step is under the tolerance; a negative guess is #NUM!.
if guess < 0.0 {
return Err(CellError::Num);
}
let mut x = if guess == 0.0 { 1e-5 } else { guess };
// Bounded: a huge guess halves to the tolerance within about 1100 steps.
for _ in 0..1100 {
if x / 2.0 < XirrSolver::tol(x / 2.0) {
break;
}
x /= 2.0;
}
return num_val(x / 2.0);
}
let d0 = d.first().copied().unwrap_or(0.0);
let first = v.iter().copied().find(|x| *x != 0.0).unwrap_or(0.0);
let mut solver = XirrSolver { values: &v, first, years: d.iter().map(|t| (t - d0) / 365.0).collect(), evals: 0, residual: 2e-8 * first.abs() };
@@ -2031,9 +2054,16 @@ mod tests {
xirr_is(&[-100.0, 100.01], &[b, b + 1.0], None, 0.037_172_409_892_082_23);
xirr_is(&[-100.0, 99.99], &[b, b + 1.0], None, -0.035_843_667_387_962_34);
xirr_is(&[-100.0, 110.0], &[b, b + 1.0], None, 1_283_305_579_426_611.5);
// A leading zero cash flow adds nothing, so the root is the one without it (10%). The
// exact Excel answer for this input is unobserved, so only the root is checked.
assert!(matches!(xirr_of(&[0.0, -100.0, 110.0], &[b, y1, y2], None), Value::Number(r) if (r - 0.1).abs() < 1e-7));
// A leading zero cash flow scales the residual to 0: Excel halves the guess to a tiny
// number whatever the flows (observed), and a negative guess is #NUM!.
let z = [0.0, -100.0, 110.0];
let zd = [b, y1, y2];
xirr_is(&z, &zd, None, 2.980_232_238_769_531_4e-9);
xirr_is(&z, &zd, Some(0.2), 2.980_232_238_769_531_4e-9);
xirr_is(&z, &zd, Some(0.0), 4.882_812_5e-9);
xirr_is(&z, &zd, Some(5.0), 4.656_612_873_077_392_6e-9);
xirr_is(&[0.0, -100.0, 50.0], &zd, None, 2.980_232_238_769_531_4e-9);
is_err(xirr_of(&z, &zd, Some(-0.5)), CellError::Num);
// Dates before the first are allowed (time runs from the first date); in the second
// case the search finds no sign change.
xirr_is(&[-2416.93, 447.77, 1743.6], &[44331.0, 44498.0, 43849.0], None, -0.974_587_937_071_919_6);
@@ -2121,7 +2151,20 @@ mod tests {
is_err(ev("XNPV", vec![n(0.09), rf(flows(Value::Empty)), rf(dates)]), CellError::Num);
is_err(ev("XNPV", vec![n(0.09), rf(flows(nv(0.0))), rf(gap.clone())]), CellError::Num);
is_err(ev("XNPV", vec![n(0.09), rf(flows(Value::Empty)), rf(gap.clone())]), CellError::Num);
is_err(ev("XNPV", vec![n(0.09), rf(flows(nv(2750.0))), rf(gap)]), CellError::Value);
is_err(ev("XNPV", vec![n(0.09), rf(flows(nv(2750.0))), rf(gap)]), CellError::Num);
is_err(ev("XNPV", vec![n(0.09), rf(flows(tv("-"))), rf(dates.clone())]), CellError::Num);
// In the first row a blank value or date is #VALUE! for XNPV; XIRR reads a blank date there as day 0.
let first_row = |value: Value, date: Value| {
let cells = |x: Value, rest: [f64; 4]| arr(std::iter::once(vec![x]).chain(rest.iter().map(|y| vec![nv(*y)])).collect());
(rf(cells(value, [2750.0, 4250.0, 3250.0, 500.0])), rf(cells(date, [39508.0, 39751.0, 39859.0, 39904.0])))
};
let (v0, d0) = first_row(Value::Empty, nv(39448.0));
is_err(ev("XNPV", vec![n(0.09), v0, d0]), CellError::Value);
let (v0, d0) = first_row(nv(0.0), Value::Empty);
is_err(ev("XNPV", vec![n(0.09), v0, d0]), CellError::Value);
let (v0, d0) = first_row(nv(-10000.0), Value::Empty);
is_err(ev("XNPV", vec![n(0.09), v0.clone(), d0.clone()]), CellError::Value);
close_tol(ev("XIRR", vec![v0, d0]), 0.000_664_654_374_122_619_6, 1e-7);
}
#[test]
+4 -2
View File
@@ -107,7 +107,9 @@ reconstructed from observation: a sign-only search that brackets the root from t
(doubling, halving towards 0, or stepping halfway to -100%) and bisects until half the bracket is
under 1e-8 x (|rate| + 0.5) and XNPV is under 2e-8 x the first value, so its answer is the same
midpoint as Excel's, about 1e-8 from the exact root, bit for bit in 888 of 910 synthetic cases
with an answer (97.7% agreement on 1,283 cases including `#NUM!`). Known differences: flows with
with an answer (97.7% agreement on 1,283 cases including `#NUM!`). A 0 or blank first flow
gives the tiny value Excel gives (the guess halved until the step is under the tolerance: about
2.98e-9 for the default guess, `#NUM!` for a negative guess). Known differences: flows with
several sign changes where Excel returns about 3e-9 (a halving that reached 0), or `#NUM!` for
two roots close to the guess. IRR uses Newton on the discount factor 1/(1+rate), at most 100 steps,
stopping at a step under 1e-8; Excel's last digits differ by about 1e-12 (its exact iteration is
@@ -142,7 +144,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds
| 2026-10-17 | minor | 15-significant-digit comparison moved from missing to done (#57), and the resolved caveats on the 3-D reference, dynamic-array and legacy-array rows cleared (#140, #133, #139, #156) |
| 2026-10-16 | minor | FORECAST.ETS gap interpolation uses a forward-only known-point scan instead of rescanning each gap for every missing value; numerical behavior and missing-point limits are preserved (#1042) |
| 2026-10-15 | minor | XIRR follows Excel's bisection (answers bit for bit, about 1e-8 from the exact root; dates before the first allowed; guess <= -1 is `#NUM!`); IRR uses Newton on the discount factor (`IRR` of -100, 19 zeros, 1E-5 is `#NUM!` like Excel; guess <= -1 is `#VALUE!`) |
| 2026-10-20 | minor | XIRR counts a blank cash-flow cell as 0 and ignores a blank date next to a blank or 0 flow, instead of returning `#VALUE!`; XNPV now returns the `#NUM!` or `#VALUE!` Excel gives for blank values and dates (observed on synthetic workbooks) |
| 2026-10-20 | minor | XIRR counts a blank cash-flow cell as 0 and ignores a blank date next to a blank or 0 flow, instead of returning `#VALUE!`; XNPV now returns the `#NUM!` Excel gives for a blank or text value or a blank date after the first row, and `#VALUE!` for a blank value or date in the first row (observed on synthetic workbooks); XIRR with a 0 or blank first flow returns Excel's tiny value (about 2.98e-9), and a first nonzero flow with a blank date reads the date as day 0 |
| 2026-10-15 | minor | Iterative calculation: Maximum Iterations and Maximum Change are set in File › Options › Formulas (dropped "no Options dialog yet") |
| 2026-10-15 | minor | MEDIAN and AGGREGATE median use an overflow-safe midpoint (#1579); regressions cover finite extremes, tiny values and dependent recalculation |
| 2026-10-15 | minor | ADDRESS quotes a sheet name the way Excel does: names that read as an A1 cell (`A1`) or an R1C1 reference (`R1C1`, `RC`) and names starting with `.` get quotes; `TRUE` / `FALSE` stay bare (#1458) |