Skip to content

fix(functions): reject a negative fractional basis instead of truncating it to zero (#4058) - #4257

Merged
FredApps merged 2 commits into
storytold:mainfrom
gmrmad1:fix/negative-fractional-basis-4058
Nov 26, 2026
Merged

FredApps merged 2 commits into
storytold:mainfrom
gmrmad1:fix/negative-fractional-basis-4058

Conversation

@gmrmad1

@gmrmad1 gmrmad1 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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 passes 0.0..=4.0. Native Excel returns #NUM! for those values, so PRICE(...,-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.rs basis_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.rs yearfrac — its own basis parsing.

Positive fractions still truncate to their integer basis (0.9 → 0, matching Excel), omitted basis is still 0, and -1 / 5 remain #NUM!. to_int, opt_num and 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:

left: Number(101.40114791702759), right: Error(Num)
left: Number(1.4583333333333333), right: Error(Num)

Commands run

  • cargo test -p gridcraft-functions --lib fractional_basis — 2 passed
  • cargo test -p gridcraft-functions --lib financial::tests and ... date::tests — 14 and 19 passed
  • cargo clippy -p gridcraft-functions --all-targets -- -D warnings — clean
  • rustfmt --edition 2024 --check on the three touched files — clean

All cargo commands ran via nice -n 19 with CARGO_BUILD_JOBS=2 CARGO_INCREMENTAL=0 in an isolated target directory. Not run: workspace-wide CI, wasm.

Fixes #4058

Summary by CodeRabbit

  • Bug Fixes
    • Financial date and pricing functions now return #NUM! for negative basis values, including negative fractions that were previously treated as zero.
    • Nonnegative fractional basis values continue to be truncated to an integer basis.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

YEARFRAC and the financial day-count basis parser now reject negative basis values before integer conversion. Nonnegative fractional values continue to be truncated. Tests cover negative and positive fractional basis inputs.

Changes: Basis validation

Layer / File(s) Summary
Reject negative raw basis values
crates/functions/src/date.rs, crates/functions/src/financial/day_count.rs, crates/functions/src/financial/tests.rs
YEARFRAC and the financial day-count basis parser return #NUM! for negative basis values before truncation. Tests cover negative fractional inputs and verify that positive fractions match basis 0 for selected functions.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, harrison-booth, echelon

Merge Risk: Minimal · up to c068f

This change makes negative fractional basis values return #NUM!, matching Excel, while leaving other basis handling unchanged. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main behavior change: negative fractional basis values are now rejected instead of being truncated to zero. It matches the raw summary, which describes the basis_arg and YEARFRAC v…
Linked Issues check Passed [#4058] requires that negative fractional basis values between -1 and 0 return #NUM!, and that basis 0, basis 0.9, and basis -1 keep their current behavior. At head, basis_arg in `crates/functions/s…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes two validators and adds tests. The basis_arg change in day_count.rs and the yearfrac change in date.rs both implement the negative-basis requir…
Full details: Title check

Explanation

The title names the main behavior change: negative fractional basis values are now rejected instead of being truncated to zero. It matches the raw summary, which describes the basis_arg and YEARFRAC validation changes. The title uses the conventional fix(scope) prefix and includes the issue number. The title is 91 characters, which is slightly long but still clear.

Full details: Linked Issues check

Explanation

[#4058] requires that negative fractional basis values between -1 and 0 return #NUM!, and that basis 0, basis 0.9, and basis -1 keep their current behavior. At head, basis_arg in crates/functions/src/financial/day_count.rs checks raw < 0.0 and returns CellError::Num before trunc(). The existing 0.0..=4.0 check remains, so -1 and 5 still return #NUM!, and 0.9 still truncates to 0. In crates/functions/src/date.rs, yearfrac applies the same raw-value check before to_int, so YEARFRAC(…,-0.9) returns #NUM!, while 0 and 0.9 are unchanged. The diff against the merge base matches this behavior. The new tests financial::tests::negative_fractional_basis_is_rejected and date::tests::yearfrac_rejects_negative_fractional_basis cover PRICE, COUPDAYS, PRICEMAT, ACCRINTM, and YEARFRAC, with positive and zero controls. Residual uncertainty: this review did not re-read each listed call site (for example, AMORLINC, AMORDEGRC, ODDF*/ODDL*) to confirm that it calls basis_arg. The PR description states that it does. The change since the previous review was not separately identified, and the current diff for both files is the same logic assessed earlier.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes two validators and adds tests. The basis_arg change in day_count.rs and the yearfrac change in date.rs both implement the negative-basis requirement of #4058. The added tests in financial/tests.rs and date.rs verify that requirement, including positive-fraction and zero controls. The diffs of day_count.rs and date.rs do not modify to_int, opt_num, or frequency argument handling. No unrelated change was found in the inspected diffs. The financial/tests.rs hunk was not re-inspected in this pass; the raw summary describes it as test-only.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7769efd and c068f77.

Files selected for processing: 1

  • crates/functions/src/financial/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main merged, no code change since approval (approved 7769efd, head now c068f77; only merge commits were added since). CI is green on the new head. Re-approving on the new head.

@FredApps
FredApps merged commit fde057f into storytold:main Nov 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Financial and date functions accept negative fractional basis values instead of #NUM!

2 participants