Skip to content

functions: YIELDMAT returns zero when 30/360 has no days left (#4094) (#4094) - #4244

Open
jayzed80 wants to merge 1 commit into
storytold:mainfrom
jayzed80:fix/yieldmat-zero-days-4094
Open

jayzed80 wants to merge 1 commit into
storytold:mainfrom
jayzed80:fix/yieldmat-zero-days-4094

Conversation

@jayzed80

@jayzed80 jayzed80 commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #4094

Root cause

yieldmat in crates/functions/src/financial/securities.rs:186 returned #NUM! whenever the settlement-to-maturity year fraction (dsm) was zero. mat_terms (line 170) already rejects settlement >= maturity, so dsm == 0 only happens when two distinct dates fall on the same 30/360 day. Examples are Jan 30 → Jan 31 under basis 4, or May 30 → May 31 under basis 0. In those cases Excel returns 0, as shown in the issue.

Fix

The pr <= 0 check still returns #NUM!. When dsm == 0, YIELDMAT now returns 0 instead of dividing by zero. Nothing else changes. Equal settlement and maturity dates are still rejected in mat_terms.

Relation to #4154

#4154 (for #4093) changes how PRICEMAT/YIELDMAT count 30/360 days from the issue date. This change does not touch the day count. It only changes the branch in yieldmat that runs after dsm has been computed, so the two should not conflict beyond nearby lines. If #4154 lands first, the dsm == 0 branch still applies. The issue's cases depend only on the settlement-to-maturity count, and that is zero either way.

Tests

New test financial::tests::yieldmat_zero_30_360_days_is_zero in crates/functions/src/financial/tests.rs:

  • the three issue cases now give 0 (basis 4 with price 100 and with price 95; basis 0)
  • the basis-2 control case gives 0.04979253112033974 (tolerance 1e-15)
  • a price of 0 and equal settlement/maturity dates still give #NUM!

Without the fix (on upstream/main, and again with only the fix reverted):

thread 'financial::tests::yieldmat_zero_30_360_days_is_zero' panicked at crates/functions/src/financial/tests.rs:197:5:
expected number 0, got Error(Num)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 181 filtered out

With the fix:

test financial::tests::yieldmat_zero_30_360_days_is_zero ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 181 filtered out

Checks run

  • cargo fmt -p gridcraft-functions --check: clean (only changed lines)
  • cargo clippy -j 4 -p gridcraft-functions --all-targets -- -D warnings: clean
  • cargo test -j 4 -p gridcraft-functions: 182 passed, 0 failed

Not run

  • The full cargo xtask ci (wasm, layers, assets, other crates). This needs CI.
  • Not checked against Excel locally. The expected values come from the issue report, which covers the 1900 date system only. The issue notes that 1904 workbooks have other day-count differences, and this change does not address them.

Summary by CodeRabbit

  • Bug Fixes
    • YIELDMAT now returns zero for positive-priced securities when the remaining year fraction is zero. Non-positive prices and equal settlement and maturity dates continue to return #NUM!.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

YIELDMAT now returns zero when distinct dates produce a zero remaining year fraction and the price is positive. Regression tests cover this case and preserve error expectations for non-positive prices and equal dates.

Changes: YIELDMAT zero-day handling

Layer / File(s) Summary
Zero-day handling and regression tests
crates/functions/src/financial/securities.rs, crates/functions/src/financial/tests.rs
yieldmat returns zero when dsm is zero and pr is positive. Tests cover zero-day cases, a positive-day case, a non-positive price, and equal dates.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, zidan44, dylanmtaylor

Merge Risk: Minimal · up to 1c698

YIELDMAT now returns zero for distinct dates that collapse to the same day count under 30/360, matching the reported Excel behavior. Error handling for nonpositive prices and equal dates is unchanged, and regression tests cover the new case. The change is low risk to merge.

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 change: YIELDMAT now returns zero when the 30/360 day count leaves no days. The raw summary and objectives confirm this change in securities.rs and the added tests. The title …
Linked Issues check Passed Issue #4094 requires YIELDMAT to return 0 for distinct valid dates with zero 30/360 days, keep #NUM! for nonpositive price, and keep equal settlement and maturity rejected. The diff in crates/function…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches only crates/functions/src/financial/securities.rs (the yieldmat branch) and crates/functions/src/financial/tests.rs (one new test). Both changes implem…
Full details: Title check

Explanation

The title names the main change: YIELDMAT now returns zero when the 30/360 day count leaves no days. The raw summary and objectives confirm this change in securities.rs and the added tests. The title also cites #4094 twice, which is redundant but does not make it unrelated. Its wording is specific enough for a teammate scanning history.

Full details: Linked Issues check

Explanation

Issue #4094 requires YIELDMAT to return 0 for distinct valid dates with zero 30/360 days, keep #NUM! for nonpositive price, and keep equal settlement and maturity rejected. The diff in crates/functions/src/financial/securities.rs at yieldmat now returns num_val(0.0) when dsm == 0.0, after the pr <= 0.0 check, which still returns CellError::Num. Equal dates are still rejected upstream by mat_terms, as the PR description states. The new test yieldmat_zero_30_360_days_is_zero in tests.rs covers the three zero-result issue cases (basis 4 at price 100 and 95, basis 0), the basis-2 control with expected 0.04979253112033974 at tolerance 1e-15, zero price, and equal dates. The 1904 date system is out of scope, as the issue states. Excel behavior was not verified locally, and the full CI was not run. These do not affect the coding requirements.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches only crates/functions/src/financial/securities.rs (the yieldmat branch) and crates/functions/src/financial/tests.rs (one new test). Both changes implement issue #4094. No unrelated code, day-count logic, or other functions changed. The PR description states that the day-count change tracked in #4154 is not part of this PR.

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a62a532 and 1c698a9.

Files selected for processing: 2

  • crates/functions/src/financial/securities.rs
  • 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

This branch has not been deployed

No deployments
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.

YIELDMAT returns NUM instead of zero when 30/360 collapses distinct dates

1 participant