Fix IRR error when income > sum of payments#1627
Merged
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1627 +/- ##
===========================================
- Coverage 97.18% 97.18% -0.01%
===========================================
Files 172 172
Lines 14834 14836 +2
Branches 3257 3258 +1
===========================================
+ Hits 14417 14418 +1
- Misses 409 418 +9
+ Partials 8 0 -8
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The IRR function returns
#NUM!error when the initial investment significantly exceeds the sum of returns (e.g.,=IRR({-150000, 12000, 15000, 18000})). Excel correctly returns ~-41% for this case.Root cause: The
irrCoreNewton-Raphson solver overshoots past the lower bound of -1 on the first iteration when the solution is a strongly negative rate. The code then unconditionally returns#NUM!.Fix: Replace the unconditional error with a bisection-based clamp. When Newton-Raphson overshoots past -1, bisect between the current rate and -1:
newRate = (rate - 1) / 2. This is guaranteed to stay in the valid domain (> -1) and converges linearly until close enough for quadratic Newton convergence to take over.How did you test your changes?
Added 5 unit tests in the private tests repo covering:
[-150000, 12000, 15000, 18000]with default guess[150000, -12000, -15000, -18000][-10000, 100, 100, 100]All 42 IRR tests pass (37 existing + 5 new), no regressions.
Types of changes
Related issues:
Checklist:
Note
Low Risk
Small, localized numerical-solver change plus non-runtime test/benchmark script updates; primary risk is altered IRR convergence behavior on edge-case inputs.
Overview
Fixes
IRRreturning#NUM!for strongly negative solutions by clamping Newton-Raphson iterations inirrCorewhen the next step overshoots past-1(bisects back into the valid domain instead of immediately erroring).Updates tooling/docs around the private test suite: renames the setup script to
test:setup-private, adjustsfetch-tests.shto create a missing branch fromdevelop(and pull appropriately), and repoints benchmark scripts totest/hyperformula-tests/performance. Also records the IRR fix inCHANGELOG.md.Written by Cursor Bugbot for commit 34b1265. This will update automatically on new commits. Configure here.