We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f588ab5 commit af32d77Copy full SHA for af32d77
functions/math/expm1.js
@@ -1,23 +1,10 @@
1
function expm1(x) {
2
// discuss at: http://phpjs.org/functions/expm1/
3
// original by: Brett Zamir (http://brett-zamir.me)
4
+ // improved by: Robert Eisele (http://www.xarg.org/)
5
// note: Precision 'n' can be adjusted as desired
6
// example 1: expm1(1e-15);
7
// returns 1: 1.0000000000000007e-15
8
- var ret = 0,
9
- // degree of precision
10
- n = 50;
11
- var factorial = function factorial(n) {
12
- if ((n === 0) || (n === 1)) {
13
- return 1;
14
- } else {
15
- var result = (n * factorial(n - 1));
16
- return result;
17
- }
18
- };
19
- for (var i = 1; i < n; i++) {
20
- ret += Math.pow(x, i) / factorial(i);
21
22
- return ret;
+ return (x < 1e-5 && -1e-5 < x) ? x + 0.5 * x * x : Math.exp(x) - 1;
23
}
0 commit comments