This content originally appeared on Lea Verou’s blog and was authored by Lea Verou
As Math.log currently stands, it’s a bit useless. It only calculates natural logarithms (base e). We can easily modify it however, to calculate logarithms of any base:
Math.log = (function() {
var log = Math.log;
return function(n, a) {
return log(n)/(a? log(a) : 1);
}
})();
We can now specify the base as a second parameter, or still use the default one (Math.E) if we don’t specify one, so older scripts won’t break or if we want a shortcut to the natural logarithm. ;)
This content originally appeared on Lea Verou’s blog and was authored by Lea Verou
Lea Verou | Sciencx (2009-03-26T00:00:00+00:00) Extend Math.log to allow for bases != e. Retrieved from https://www.scien.cx/2009/03/26/extend-math-log-to-allow-for-bases-e/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.