mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-28 17:30:24 +01:00
Merge branch 'main' into fix/2454-left-align-font-name
This commit is contained in:
3 files changed
+44
-2
No files matched your search
@@ -496,7 +496,16 @@ fn yearfrac(a: &[Arg], c: &mut dyn Ctx) -> R<Value> {
|
||||
let sys = c.date_system();
|
||||
let s = day_arg(a, 0, sys)?;
|
||||
let e = day_arg(a, 1, sys)?;
|
||||
let basis = if has(a, 2) { to_int(num(a, 2)?)? } else { 0 };
|
||||
let basis = if has(a, 2) {
|
||||
let raw = num(a, 2)?;
|
||||
// Excel rejects any negative basis before truncation: -0.9 would truncate to 0 and slip through.
|
||||
if raw < 0.0 {
|
||||
return Err(CellError::Num);
|
||||
}
|
||||
to_int(raw)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
num_val(yearfrac_core(sys, s, e, basis)?)
|
||||
}
|
||||
|
||||
@@ -850,6 +859,19 @@ mod tests {
|
||||
close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn yearfrac_rejects_negative_fractional_basis() {
|
||||
// Excel rejects any negative basis: -0.9 must not truncate to 0.
|
||||
let (s, e) = (n(d(2024.0, 1.0, 15.0)), n(d(2025.0, 6.0, 30.0)));
|
||||
is_err(ev("YEARFRAC", vec![s.clone(), e.clone(), n(-0.9)]), CellError::Num);
|
||||
// A positive fraction still truncates to basis 0.
|
||||
let whole = match ev("YEARFRAC", vec![s.clone(), e.clone(), n(0.0)]) {
|
||||
Value::Number(x) => x,
|
||||
v => panic!("YEARFRAC basis 0 gave {v:?}"),
|
||||
};
|
||||
close_tol(ev("YEARFRAC", vec![s, e, n(0.9)]), whole, 1e-12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn date_lifting() {
|
||||
let v = ev("YEAR", vec![av(row(&[43831.0, 46302.0]))]);
|
||||
|
||||
@@ -24,7 +24,12 @@ fn serial(sys: DateSystem, y: i32, m: u32, d: u32) -> R<f64> {
|
||||
}
|
||||
|
||||
pub(super) fn basis_arg(a: &[Arg], i: usize) -> R<u32> {
|
||||
let b = opt_num(a, i, 0.0)?.trunc();
|
||||
let raw = opt_num(a, i, 0.0)?;
|
||||
// Excel rejects any negative basis before truncation: -0.9 would truncate to -0.0 and slip through.
|
||||
if raw < 0.0 {
|
||||
return Err(CellError::Num);
|
||||
}
|
||||
let b = raw.trunc();
|
||||
if !(0.0..=4.0).contains(&b) {
|
||||
return Err(CellError::Num);
|
||||
}
|
||||
|
||||
@@ -273,6 +273,21 @@ fn number(v: Value) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// A negative fractional basis is #NUM!, not truncation toward zero (#4058).
|
||||
#[test]
|
||||
fn negative_fractional_basis_is_rejected() {
|
||||
let price = |basis: f64| ev("PRICE", vec![d(2024, 1, 15), d(2025, 6, 30), n(0.05), n(0.04), n(100.0), n(2.0), n(basis)]);
|
||||
is_err(price(-0.9), CellError::Num);
|
||||
is_err(ev("COUPDAYS", vec![d(2011, 1, 25), d(2011, 11, 15), n(2.0), n(-0.9)]), CellError::Num);
|
||||
is_err(ev("PRICEMAT", vec![d(2008, 2, 15), d(2008, 4, 13), d(2007, 11, 11), n(0.061), n(0.061), n(-0.9)]), CellError::Num);
|
||||
is_err(ev("ACCRINTM", vec![d(2008, 4, 1), d(2008, 6, 15), n(0.1), n(1000.0), n(-0.5)]), CellError::Num);
|
||||
// Controls: a positive fraction still truncates to basis 0, and -1 is still #NUM!.
|
||||
close_tol(price(0.9), 101.40114791702759, 1e-9);
|
||||
close_tol(price(0.0), 101.40114791702759, 1e-9);
|
||||
close(ev("COUPDAYS", vec![d(2011, 1, 25), d(2011, 11, 15), n(2.0), n(0.9)]), 180.0);
|
||||
is_err(price(-1.0), CellError::Num);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn odd_periods() {
|
||||
// The worked examples on Microsoft's function pages (results as printed there).
|
||||
|
||||
Reference in new issue
Block a user