Skip to content

TVM: Fix to the interest rate calculator#146

Merged
jenbreese merged 1 commit into
devfrom
error-doc-tvm-0702
Jul 2, 2026
Merged

TVM: Fix to the interest rate calculator#146
jenbreese merged 1 commit into
devfrom
error-doc-tvm-0702

Conversation

@jenbreese

@jenbreese jenbreese commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

READY FOR REVIEW

This fix was found by client's test case:
Present value: $1,000
Payment per period: -$50
Future value: $0
Number of periods: 60
Compounding frequency: weekly
Payments occur: different
Payment frequency: quarterly

---> our calculator outputs "Invalid result. Please check your inputs"

Claude's explaination.

Fix TVM RATE solver: incorrect Newton-Raphson seed for mismatched payment/compounding frequencies

Problem

The RATE solver returned "Invalid result. Please check your inputs." for valid inputs whenever paymentFrequencyMode was "different" and the two frequencies were far apart (e.g. weekly compounding with quarterly payments). Confirmed with PV=$1,000, PMT=-$50, FV=$0, n=60 quarters, compounding weekly, payments quarterly. Expected output: 18.32%.

Root cause

The Newton-Raphson loop seeds its initial guess with 0.1 / compFreq. But guess represents the rate per payment period, since it's applied directly to n (a count of payment periods). When compFreq and pmtFreq diverge significantly, this seed lands far from the true root, causing the solver to diverge. In this case it landed on a spurious negative root where 1 + guess < 0, and the annualization step raises that negative base to a fractional exponent (pmtFreq / compFreq), producing NaN. That NaN then trips the generic invalid-result check.

Fix

  • Changed the seed to 0.1 / pmtFreq, which matches what guess actually represents. No behavior change when paymentFrequencyMode is "same," since pmtFreq === compFreq in that case.
  • Added a guard rail clamping guess to -0.999999 if it ever drops to or below -1 during iteration, to prevent the same NaN failure mode from resurfacing under other extreme inputs.

Verification

  • Reported case now returns 18.32%, matching an independent reference calculator.
  • Re-ran same-frequency RATE cases (annual/annual, monthly/monthly) to confirm no regression in the common path.

@majimmy88 majimmy88 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

@jenbreese jenbreese merged commit 210ab50 into dev Jul 2, 2026
@jenbreese jenbreese mentioned this pull request Jul 10, 2026
14 tasks
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.

2 participants