Extend Math.log to allow for bases != e

2009-03-26来源 Lea VerouEnglish

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. ;)

原文:https://www.a8q9.cn/en/article/extend-math-log-to-allow-for-bases-e-00826f.html

相关文章