JavaScript expm1
Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 | function expm1 (x) { // Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero // // version: 1008.1718 // discuss at: http://phpjs.org/functions/expm1 // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Precision 'n' can be adjusted as desired // * example 1: expm1(1e-15); // * returns 1: 1.0000000000000007e-15 var ret=0, n = 50; // degree of precision var factorial = function factorial (n) { if ((n === 0) || (n === 1)) { return 1; } else { var result = (n * factorial(n-1) ); return result; } }; for (var i=1; i < n; i++) { ret += Math.pow(x, i)/factorial(i); } return ret; } |
Examples
Running
1 | expm1(1e-15); |
Should return
1 | 1.0000000000000007e-15 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have expm1 goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>