UNPKG

effective-interest-rate

Version:

A library to calculate effective interest rate. Also know as XIRR or effective APR.

28 lines (22 loc) 645 B
'use strict'; /** * Newton-Raphsons method to do a numerical analysis to find the effective interest. * * {@link https://en.wikipedia.org/wiki/Newton%27s_method} * * @author Tobias Nyholm <tobias.nyholm@gmail.com> */ class NewtonRaphson { static run(fx, fdx, guess) { let precision = 4; let errorLimit = Math.pow(10, -1 * precision); let previousValue = 0; do { guess = Number(guess); previousValue = Number(guess); guess = previousValue - (Number(fx(guess)) / Number(fdx(guess))); } while (Math.abs(guess - previousValue) > errorLimit); return guess; } } module.exports = NewtonRaphson;