Skip to content

Commit b1174ee

Browse files
committed
Update change log
1 parent 30493c5 commit b1174ee

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
1.0.8
22
-----
33

4+
* Merged [PR 47](https://github.com/sutoiku/formula.js/pull/47)
45
* Merged [PR 49](https://github.com/sutoiku/formula.js/pull/49)
56

67
1.0.7

dist/formula.js

+20-30
Original file line numberDiff line numberDiff line change
@@ -7358,40 +7358,30 @@ return /******/ (function(modules) { // webpackBootstrap
73587358
}
73597359

73607360
// Set maximum epsilon for end of iteration
7361-
var epsMax = 1e-10;
7361+
var epsMax = 1e-6;
73627362

73637363
// Set maximum number of iterations
7364-
var iterMax = 50;
7365-
7366-
// Implement Newton's method
7367-
var y, y0, y1, x0, x1 = 0,
7368-
f = 0,
7369-
i = 0;
7364+
var iterMax = 100;
7365+
var iter = 0;
7366+
var close = false;
73707367
var rate = guess;
7371-
if (Math.abs(rate) < epsMax) {
7372-
y = present * (1 + periods * rate) + payment * (1 + rate * type) * periods + future;
7373-
} else {
7374-
f = Math.exp(periods * Math.log(1 + rate));
7375-
y = present * f + payment * (1 / rate + type) * (f - 1) + future;
7376-
}
7377-
y0 = present + payment * periods + future;
7378-
y1 = present * f + payment * (1 / rate + type) * (f - 1) + future;
7379-
i = x0 = 0;
7380-
x1 = rate;
7381-
while ((Math.abs(y0 - y1) > epsMax) && (i < iterMax)) {
7382-
rate = (y1 * x0 - y0 * x1) / (y1 - y0);
7383-
x0 = x1;
7384-
x1 = rate;
7385-
if (Math.abs(rate) < epsMax) {
7386-
y = present * (1 + periods * rate) + payment * (1 + rate * type) * periods + future;
7387-
} else {
7388-
f = Math.exp(periods * Math.log(1 + rate));
7389-
y = present * f + payment * (1 / rate + type) * (f - 1) + future;
7390-
}
7391-
y0 = y1;
7392-
y1 = y;
7393-
++i;
7368+
7369+
while (iter < iterMax && !close) {
7370+
var t1 = Math.pow(rate + 1, periods);
7371+
var t2 = Math.pow(rate + 1, periods - 1);
7372+
7373+
var f1 = future + t1 * present + payment * (t1 - 1) * (rate * type + 1) / rate;
7374+
var f2 = periods * t2 * present - payment * (t1 - 1) *(rate * type + 1) / Math.pow(rate,2);
7375+
var f3 = periods * payment * t2 * (rate * type + 1) / rate + payment * (t1 - 1) * type / rate;
7376+
7377+
var newRate = rate - f1 / (f2 + f3);
7378+
7379+
if (Math.abs(newRate - rate) < epsMax) close = true;
7380+
iter++
7381+
rate = newRate;
73947382
}
7383+
7384+
if (!close) return Number.NaN + rate;
73957385
return rate;
73967386
};
73977387

0 commit comments

Comments
 (0)