Every loan payment function starts with the same formula, and almost every tutorial version breaks on a 0% promotion. Zero-interest financing is common enough, so that matters.
Here is the formula, the failure, and a version that holds up. All outputs below are from Node 24.
The standard formula
For a principal P, monthly rate r and n payments:
function pmtNaive(P, apr, n) {
const r = apr / 1200; // annual percent -> monthly decimal
const f = Math.pow(1 + r, n);
return P * r * f / (f - 1);
}
It works for normal rates:
pmtNaive(12000, 7, 24); // 537.2709492377435
That matches what you would expect for a $12,000 loan over 24 months at 7%. Now try a 0% deal:
pmtNaive(12000, 0, 24); // NaN
With r = 0, the numerator is P * 0 * 1 = 0 and the denominator is 1 - 1 = 0. You get 0 / 0, which is NaN. If you then render that into an input or sum it into a total, the NaN spreads.
The easy fix
Handle the zero rate as a special case. At 0% interest the payment is just the principal divided by the number of payments:
function pmt(P, apr, n) {
const r = apr / 1200;
if (r === 0) return P / n;
const f = Math.pow(1 + r, n);
return P * r * f / (f - 1);
}
pmt(12000, 0, 24); // 500
That's what most calculators should do. The EMI calculator on my site handles it this way: enter a rate of 0 and it returns the principal divided by the months.
The harder problem: almost zero
A special case for exactly zero doesn't cover rates that are almost zero. Those come from user input, from rate conversions, or from a bug upstream. Look at what the formula does as the rate shrinks:
for (const apr of [1e-14, 1e-12, 1e-9, 1e-6, 0.001]) {
console.log(apr, pmtNaive(12000, apr, 24));
}
// 1e-14 Infinity
// 1e-12 469.12496118443664
// 1e-9 499.999958639818
// 1e-6 499.99996307870373
// 0.001 500.00520841443813
The true answer for every one of those is a hair above 500. The naive formula returns Infinity at 1e-14 and a wrong payment of $469.12 at 1e-12, which is a 6% error on a number that should be 500.
This is catastrophic cancellation. When r is around 1e-14, 1 + r can't hold the difference from 1. Doubles carry about 16 significant digits, so the + r is mostly rounded off. Then f - 1 subtracts two nearly equal numbers and leaves mostly rounding noise. Dividing by that noise gives wild results.
A stable version
Rewrite the formula so the dangerous subtractions disappear. Start from the equivalent form:
payment = P * r / (1 - (1 + r)^-n)
and compute 1 - (1 + r)^-n with the two functions JavaScript provides for exactly this problem: Math.log1p(x) computes ln(1 + x) accurately for small x, and Math.expm1(x) computes e^x - 1 accurately for small x. See MDN on Math.expm1 and Math.log1p.
function pmtStable(P, apr, n) {
const r = apr / 1200;
if (r === 0) return P / n;
return P * r / (-Math.expm1(-n * Math.log1p(r)));
}
Since (1 + r)^-n = e^(-n * ln(1 + r)), we get 1 - (1 + r)^-n = -expm1(-n * log1p(r)). No subtraction of nearly equal numbers appears.
Same inputs:
for (const apr of [0, 1e-14, 1e-12, 1e-9, 1e-6, 0.001, 7]) {
console.log(apr, pmtStable(12000, apr, 24));
}
// 0 500
// 1e-14 500.0000000000001
// 1e-12 500.0000000000052
// 1e-9 500.0000000052084
// 1e-6 500.0000052083334
// 0.001 500.00520834997116
// 7 537.270949237745
The values now rise smoothly from 500, and there is no special failure at the bottom. For a 7% rate it agrees with the naive formula to 12 digits (537.2709492377435 vs 537.270949237745). You can keep the r === 0 guard for clarity and speed, but the function no longer needs it to be correct.
Rounding to cents is a separate problem
Once the payment is right, a real schedule has to deal with cents. Round the payment to $537.27 and run 24 months of interest rounded each month, and the balance does not land on zero:
// balance after 24 payments of 537.27, interest rounded each month
// leftover: 0.03
Three cents are left. The usual fix is to let the final payment absorb the difference: $537.30 in this run, so that the total paid is $12,894.51 rather than 24 x $537.27 = $12,894.48. Whichever convention your product uses, write a test for the last row of the schedule, not just the first.
A zero-rate schedule has the same problem in a different shape. Divide $10,000 into 7 payments and you get 1,428.5714285714287, which becomes $1,428.57 when rounded. Seven of those is $9,999.99, one cent short.
Where this shows up in real code
Rate inputs that are "almost zero" are more common than you might think. A promotional rate of 0.0001% is a typo someone will eventually enter. A rate derived from a conversion (say, an effective rate calculated back from a yield) can land at 1e-13 because of rounding. A tax or inflation adjustment can cancel a nominal rate to within floating point noise. If a payment function feeds a UI, a database column or a downstream sum, a single NaN or Infinity will spread, and the symptom (a blank field, a failed JSON parse, a chart that disappears) appears far from the cause. Putting the stable formula in one shared helper, with the test cases below, removes that whole class of bug.
Test cases worth keeping
// rate 0 -> principal / n
// rate 0, n = 1 -> principal
// very small rate (1e-12) -> within 1e-6 of principal / n
// rate 7, 24 months, 12000 -> 537.27
// final row balance === 0 after adjusting last payment
The very small rate test is the one that catches the naive formula. A property test that says "the payment should increase as the rate increases" also catches it, because the naive version dips below 500 at 1e-12 before climbing back.
Summary
-
P * r * f / (f - 1)returnsNaNat a 0% rate. Special-case it. - It also returns garbage near zero. Use
Math.expm1andMath.log1p. - Rounded payments leave a few cents. Decide how the last payment absorbs them and test it.
Disclosure: I run MultiCalify, a free calculator site, and the EMI calculator link above points to it.
Top comments (0)