Skip to content

Commit 91efc2c

Browse files
committed
By applying some math, improving log1p is easily possible
1 parent f4e1a56 commit 91efc2c

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

functions/math/log1p.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function log1p(x) {
22
// discuss at: http://phpjs.org/functions/log1p/
33
// original by: Brett Zamir (http://brett-zamir.me)
4+
// improved by: Robert Eisele (http://www.xarg.org/)
45
// note: Precision 'n' can be adjusted as desired
56
// example 1: log1p(1e-15);
67
// returns 1: 9.999999999999995e-16
@@ -16,11 +17,7 @@ function log1p(x) {
1617
return Math.log(1 + x);
1718
}
1819
for (var i = 1; i < n; i++) {
19-
if ((i % 2) === 0) {
20-
ret -= Math.pow(x, i) / i;
21-
} else {
22-
ret += Math.pow(x, i) / i;
23-
}
20+
ret += Math.pow(-x, i) / i;
2421
}
25-
return ret;
26-
}
22+
return -ret;
23+
}

0 commit comments

Comments
 (0)