Skip to content

Improve nper accuracy for small nonzero rates - #162

Draft
lcrmorin wants to merge 1 commit into
numpy:mainfrom
lcrmorin:fix/nper-small-rates
Draft

lcrmorin wants to merge 1 commit into
numpy:mainfrom
lcrmorin:fix/nper-small-rates

Conversation

@lcrmorin

Copy link
Copy Markdown

nper loses accuracy for very small nonzero rates because intermediate arithmetic rounds the logarithm's argument towards one. This can affect otherwise ordinary loan and savings calculations.

Use log1p and an algebraically equivalent correction near zero, avoiding division of payments by the rate in that branch. Retain the existing path outside this region and the existing zero-rate, interest-only and invalid-domain behavior. This addresses the numerical stability part of #119; it does not settle the separate proposals for undefined or infinite solutions.

Validation: 138 tests pass, including 28 added cases checked against high-precision Decimal calculations or existing boundary behavior. Ruff, Pyright and Mypy pass.

Prepared with AI assistance.

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.

1 participant