mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-30 10:13:56 +01:00
Merge fix/xirr-blank-cash-flows: XIRR counts a blank cash flow as 0 alongside Excel's solvers
This commit is contained in:
commit
fb38290e45
2 files changed
+40
-6
No files matched your search
@@ -257,8 +257,13 @@ fn npv(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
|
||||
}
|
||||
|
||||
/// Values and dates for XNPV/XIRR: both must be numeric and of equal length. XNPV rejects a
|
||||
/// date before the first; XIRR (`before_first`) takes it, as Excel does.
|
||||
fn dated_flows(values: &Value, dates: &Value, before_first: bool) -> R<(Vec<f64>, Vec<f64>)> {
|
||||
/// date before the first; XIRR (`lenient`) takes it, as Excel does.
|
||||
///
|
||||
/// Blank cells follow what Excel returns (observed on synthetic workbooks). With `lenient` (XIRR)
|
||||
/// a blank value counts as 0 and a blank date is ignored when its value is blank or 0; a nonzero
|
||||
/// value without a date is `#NUM!`. XNPV is strict: a blank value is `#NUM!`, and so is a blank
|
||||
/// date next to a blank or 0 value (`#VALUE!` next to a nonzero one).
|
||||
fn dated_flows(values: &Value, dates: &Value, lenient: bool) -> R<(Vec<f64>, Vec<f64>)> {
|
||||
let va = as_array(values);
|
||||
let da = as_array(dates);
|
||||
if va.data.len() != da.data.len() {
|
||||
@@ -267,20 +272,25 @@ fn dated_flows(values: &Value, dates: &Value, before_first: bool) -> R<(Vec<f64>
|
||||
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()) {
|
||||
match x {
|
||||
Value::Number(n) => v.push(*n),
|
||||
let value = match x {
|
||||
Value::Number(n) => *n,
|
||||
Value::Empty if lenient => 0.0,
|
||||
Value::Empty => return Err(CellError::Num),
|
||||
Value::Error(e) => return Err(*e),
|
||||
_ => return Err(CellError::Value),
|
||||
}
|
||||
};
|
||||
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),
|
||||
_ => return Err(CellError::Value),
|
||||
}
|
||||
v.push(value);
|
||||
}
|
||||
let d0 = *d.first().ok_or(CellError::Num)?;
|
||||
if d.iter().any(|&x| (x < d0 && !before_first) || x < 0.0) {
|
||||
if d.iter().any(|&x| (x < d0 && !lenient) || x < 0.0) {
|
||||
return Err(CellError::Num);
|
||||
}
|
||||
Ok((v, d))
|
||||
@@ -2087,6 +2097,29 @@ mod tests {
|
||||
close_tol(ev("PMT", vec![n(0.05), n(10.0), n(100.0), n(50.0), n(1.0)]), -16.119701185541427, 1e-15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blank_cash_flows_follow_excel_for_xirr_and_xnpv() {
|
||||
// Each result below was observed in Excel on a synthetic workbook: five rows, the first
|
||||
// four -10000, 2750, 4250, 3250 on 39448, 39508, 39751, 39859; the last row varies.
|
||||
let dates = col(&[39448.0, 39508.0, 39751.0, 39859.0, 39904.0]);
|
||||
let gap = arr([39448.0, 39508.0, 39751.0, 39859.0].iter().map(|x| vec![nv(*x)]).chain([vec![Value::Empty]]).collect());
|
||||
let flows = |last: Value| arr([-10000.0, 2750.0, 4250.0, 3250.0].iter().map(|x| vec![nv(*x)]).chain([vec![last]]).collect());
|
||||
let rate = 0.033791377645041754;
|
||||
// XIRR: a blank value counts as 0 and a blank date is ignored when its value is blank or 0. The answer is a bisection midpoint, within about 1e-8 of the exact rate.
|
||||
close_tol(ev("XIRR", vec![rf(flows(Value::Empty)), rf(dates.clone())]), rate, 1e-7);
|
||||
close_tol(ev("XIRR", vec![rf(flows(nv(0.0))), rf(dates.clone())]), rate, 1e-7);
|
||||
close_tol(ev("XIRR", vec![rf(flows(Value::Empty)), rf(gap.clone())]), rate, 1e-7);
|
||||
close_tol(ev("XIRR", vec![rf(flows(nv(0.0))), rf(gap.clone())]), rate, 1e-7);
|
||||
is_err(ev("XIRR", vec![rf(flows(nv(2750.0))), rf(gap.clone())]), CellError::Num);
|
||||
is_err(ev("XIRR", vec![rf(flows(tv("-"))), rf(dates.clone())]), CellError::Value);
|
||||
// XNPV is strict: a blank value is #NUM! where 0 is a number.
|
||||
close_tol(ev("XNPV", vec![n(0.09), rf(flows(nv(0.0))), rf(dates.clone())]), -382.66, 1e-2);
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cash_flows() {
|
||||
close_tol(ev("NPV", vec![n(0.1), n(-10000.0), n(3000.0), n(4200.0), n(6800.0)]), 1188.443412, 1e-9);
|
||||
|
||||
@@ -141,6 +141,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-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) |
|
||||
| 2026-10-14 | minor | MATCH, XMATCH, VLOOKUP, HLOOKUP, XLOOKUP and LOOKUP compare numbers exactly in every match and search mode, as Excel 365 does: `MATCH(0.1+0.2,{0.3},0)` is `#N/A` and 0.3 sorts below 0.30000000000000004 in approximate and binary search. `=`, SWITCH and the criteria functions (COUNTIF(S), SUMIF(S), AVERAGEIF(S), MAXIFS…) keep comparing at 15 significant digits, also as observed |
|
||||
|
||||
Reference in new issue
Block a user