Repository navigation
fix(functions): reject a negative fractional basis instead of truncating it to zero (#4058) - #4257
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…ing it to zero (storytold#4058) Upstream PR: storytold#4257 Head: 7769efd
Merges the zcode-pr-transfer branch (61 commits transferring open, not-yet-merged upstream PRs into our fork) into main. Docs (docs/ui-parity.md, docs/file-format-parity.md) resolved in favor of main's current changelog state; code from all transferred PRs carried over cleanly. Commits carried over (21 from this session + 40 earlier transfers): storytold#4408, storytold#4405, storytold#4406, storytold#4386, storytold#4379, storytold#4378, storytold#4376, storytold#4375, storytold#4373, storytold#4370, storytold#4369, storytold#4348, storytold#4347, storytold#4345, storytold#4344, storytold#4343, storytold#4341, storytold#4340, storytold#4337, storytold#4336, storytold#4318, storytold#4316, storytold#4315, storytold#4312, storytold#4310, storytold#4307, storytold#4306, storytold#4305, storytold#4303, storytold#4298, storytold#4295, storytold#4287, storytold#4286, storytold#4257, storytold#4256, storytold#4254, storytold#4252, storytold#4251, storytold#4248, storytold#4244, storytold#4243, storytold#4229, storytold#4228, storytold#4227, storytold#4225, storytold#4221, storytold#4220, storytold#4219, storytold#4217, storytold#4424, storytold#4423, storytold#4420, storytold#4419, storytold#4418, storytold#4414, storytold#3617, storytold#2642, storytold#2556, storytold#2321
FredApps
left a comment
There was a problem hiding this comment.
Reviewed the two day-count validation paths. Negative fractional bases are rejected before truncation, while positive fractions, defaults and existing error handling are preserved. The focused YEARFRAC and financial tests both passed on this exact head. The relevant production files and test cases are unchanged in the newer preview; its full workspace combination has not run.
What and why
A basis argument between -1 and 0 behaved as basis 0 because the validators truncated toward zero before range-checking:
-0.9.trunc()is-0.0, which passes0.0..=4.0. Native Excel returns#NUM!for those values, soPRICE(...,-0.9),COUPDAYS(...,-0.9),YEARFRAC(...,-0.9)and the other basis-taking functions returned plausible numbers where Excel refuses (issue #4058).Two validators are involved and both now reject any negative value before truncation:
crates/functions/src/financial/day_count.rsbasis_arg— shared by PRICE, YIELD, DURATION, MDURATION, PRICEMAT, YIELDMAT, DISC, INTRATE, YIELDDISC, PRICEDISC, RECEIVED, ACCRINT, ACCRINTM, the COUP* family, the ODDF*/ODDL* family and AMORLINC/AMORDEGRC.crates/functions/src/date.rsyearfrac— its own basis parsing.Positive fractions still truncate to their integer basis (0.9 → 0, matching Excel), omitted basis is still 0, and
-1/5remain#NUM!.to_int,opt_numand frequency arguments are untouched.Evidence
New tests, both failing before the change and passing after:
financial::tests::negative_fractional_basis_is_rejected— PRICE/COUPDAYS/PRICEMAT at -0.9 and ACCRINTM at -0.5 are#NUM!; controls: PRICE at 0.9 and 0.0 both equal 101.40114791702759, COUPDAYS 0.9 = 180, PRICE -1 still#NUM!.date::tests::yearfrac_rejects_negative_fractional_basis— YEARFRAC(…, -0.9) is#NUM!; 0.9 equals 0.Before the fix the new tests failed with:
Commands run
cargo test -p gridcraft-functions --lib fractional_basis— 2 passedcargo test -p gridcraft-functions --lib financial::testsand... date::tests— 14 and 19 passedcargo clippy -p gridcraft-functions --all-targets -- -D warnings— cleanrustfmt --edition 2024 --checkon the three touched files — cleanAll cargo commands ran via
nice -n 19withCARGO_BUILD_JOBS=2 CARGO_INCREMENTAL=0in an isolated target directory. Not run: workspace-wide CI, wasm.Fixes #4058
Summary by CodeRabbit
#NUM!for negative basis values, including negative fractions that were previously treated as zero.