Nyholm / effective-interest-rate-js

Calculate effective interest, XIRR or effective APR.
MIT License
9 stars 5 forks source link

Infinite loop with nonconverging values #3

Open thykka opened 5 years ago

thykka commented 5 years ago

Test case:

const { performance } = require('perf_hooks');
const EffectiveInterestCalculator = require('effective-interest-rate');

let principal = 230;
let guess = 5.189166;
let payments = {"1999-12-01":229,"2000-01-01":21.409052531921994,"2000-02-01":21.409052531921994,"2000-03-01":21.409052531921994,"2000-04-01":21.409052531921994,"2000-05-01":21.409052531921994,"2000-06-01":21.409052531921994,"2000-07-01":21.409052531921994,"2000-08-01":21.409052531921994,"2000-09-01":21.409052531921994,"2000-10-01":21.409052531921994,"2000-11-01":21.409052531921994,"2000-12-01":21.409052531921994,"2001-01-01":21.409052531921994,"2001-02-01":21.409052531921994,"2001-03-01":21.409052531921994,"2001-04-01":21.409052531921994,"2001-05-01":21.409052531921994,"2001-06-01":21.409052531921994,"2001-07-01":21.409052531921994,"2001-08-01":21.409052531921994,"2001-09-01":21.409052531921994,"2001-10-01":21.409052531921994,"2001-11-01":21.409052531921994,"2001-12-01":21.409052531921994,"2002-01-01":21.409052531921994,"2002-02-01":21.409052531921994,"2002-03-01":21.409052531921994,"2002-04-01":21.409052531921994,"2002-05-01":21.409052531921994,"2002-06-01":21.409052531921994,"2002-07-01":21.409052531921994,"2002-08-01":21.409052531921994,"2002-09-01":21.409052531921994,"2002-10-01":21.409052531921994,"2002-11-01":21.409052531921994,"2002-12-01":21.409052531921994}
let startDate = "1999-12-01";

let interest = EffectiveInterestCalculator.withSpecifiedPayments(principal, startDate, payments, guess);
console.log(`Found EIR at ${interest}`);

Expected result: EffectiveInterestCalculator throws an error, due to the given values not converging within a reasonable number of iterations.

Actual result: EffectiveInterestCalculator enters an infinite loop and freezes.

Nyholm commented 5 years ago

Interesting. Thank you.

We should probably add a test for this and throw an error. Could you provide a PR?

thykka commented 5 years ago

Trying to make tests for it, but the test suite for NewtonRhapson is failing on latest master...:

PhantomJS 2.1.1 (Mac OS X 0.0.0) Given an instance of NewtonRaphson it calculates correct with example 0 FAILED
    ReferenceError: Can't find variable: NewtonRaphson in test/spec/NewtonRaphson.spec.js (line 20)
    test/spec/NewtonRaphson.spec.js:20:34

It also seems like es6 syntax isn't supported by the test runner.

Any pointers?