如何在 JavaScript 中指定 Math.log() 的基础?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3019278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I specify the base for Math.log() in JavaScript?
提问by MetaGuru
I need a logfunction for JavaScript, but it needs to be base 10. I can't see any listing for this, so I'm assuming it's not possible. Are there any math wizards out there who know a solution for this?
我需要一个log用于 JavaScript的函数,但它需要以 10 为基数。我看不到任何列表,所以我假设这是不可能的。是否有任何数学奇才知道解决方案?
回答by Peter
"Change of Base" Formula / Identity
“改变基础”公式/身份
The numerical value for logarithm to the base 10 can be calculated with the following identity.
可以使用以下恒等式计算以 10 为底的对数数值。
Since Math.log(x)in JavaScript returns the natural logarithm of x(same as ln(x)), for base 10 you can divide by Math.log(10)(same as ln(10)):
由于Math.log(x)在 JavaScript 中返回x(与ln(x)相同)的自然对数,对于以 10 为底的您可以除以Math.log(10)(与ln(10)相同):
function log10(val) {
return Math.log(val) / Math.LN10;
}
Math.LN10is a built-in precomputed constant for Math.log(10), so this function is essentially identical to:
Math.LN10是 的内置预计算常量Math.log(10),因此该函数本质上等同于:
function log10(val) {
return Math.log(val) / Math.log(10);
}
回答by bramp
Easy, just change the base by dividing by the log(10). There is even a constant to help you
很简单,只需通过除以 log(10) 来更改基数。甚至有一个常数可以帮助您
Math.log(num) / Math.LN10;
which is the same as:
这与:
Math.log(num) / Math.log(10);
回答by CMS
You can simply divide the logarithm of your value, and the logarithm of the desired base, also you could override the Math.logmethod to accept an optional base argument:
您可以简单地将您的值的对数和所需基数的对数相除,您也可以覆盖该Math.log方法以接受可选的基数参数:
Math.log = (function() {
var log = Math.log;
return function(n, base) {
return log(n)/(base ? log(base) : 1);
};
})();
Math.log(5, 10);
回答by Royce Chao
the answer here would cause obvious precision problem and is not reliable in some use cases
这里的答案会导致明显的精度问题,并且在某些用例中不可靠
> Math.log(10)/Math.LN10
1
> Math.log(100)/Math.LN10
2
> Math.log(1000)/Math.LN10
2.9999999999999996
> Math.log(10000)/Math.LN10
4
回答by Yukulélé
const logBase = (n, base) => Math.log(n) / Math.log(base);
回答by artlung
Math.log10 = function(n) {
return (Math.log(n)) / (Math.log(10));
}
Then you can do
然后你可以做
Math.log10(your_number);
NOTE:Initially I thought to do Math.prototype.log10 = ...to do this, but user CMSpointed out that Math doesn't work this way, so I edited out the .prototypepart.
注意:最初我想这样Math.prototype.log10 = ...做,但用户CMS指出 Math 不能以这种方式工作,因此我编辑了该.prototype部分。
回答by Ivan Black
FF 25+ supports a Math.log10method. You may to use polyfill:
FF 25+ 支持一种Math.log10方法。您可以使用 polyfill:
if (!Math.log10) Math.log10 = function(t){ return Math.log(t)/Math.LN10; };
MDN lists the supported browsers.
MDN 列出了支持的浏览器。
Desktop Browsers
Chrome Firefox (Gecko) Internet Explorer Opera Safari 38 25 (25) Not supported 25 7.1Mobile Browsers
Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Not supported Not supported 25.0 (25) Not supported Not supported iOS 8
桌面浏览器
Chrome Firefox (Gecko) Internet Explorer Opera Safari 38 25 (25) Not supported 25 7.1移动浏览器
Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Not supported Not supported 25.0 (25) Not supported Not supported iOS 8
回答by Haumed Rahmani
Math.log10(x)!
Math.log10(x)!
The top answer is fine for an arbitrary base, but the question is regarding log base 10, and Math.log10(x)has been standard across all browsers since 2015.*
最佳答案适用于任意基数,但问题是关于日志基数 10,自 2015 年以来Math.log10(x)一直是所有浏览器的标准。*
*Except IE, if that's important to you for some reason.
*除了 IE,如果出于某种原因这对您很重要。
回答by Lukasz Czerwinski
For base 10 use Math.log10().
对于基数 10 使用Math.log10().
See docs at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
请参阅以下文档:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
回答by Kartik Singh
If you have a number x, then use of Math.log(x)would essentially be lnx.
如果你有一个数字 x,那么使用的Math.log(x)本质上就是 lnx。
To convert it to a base other than e, you can use the following function :
要将其转换为 e 以外的基数,可以使用以下函数:
function(x){ return Math.log(x)/Math.log(10); }


