如何在 JavaScript 中指定 Math.log() 的基数?

发布于 2024-09-05 18:06:10 字数 99 浏览 10 评论 0原文

我需要 JavaScript 的 log 函数,但它需要以 10 为基数。我看不到任何相关列表,所以我假设这是不可能的。有没有数学奇才知道这个问题的解决方案?

I need a log function 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

耶耶耶 2024-09-12 18:06:10

“改变基础”公式/身份

可以计算以10为底的对数的数值
具有以下身份。

以 10 为底的对数


由于 JavaScript 中的 Math.log(x) 返回 x 的自然对数(与 < em>ln(x)),对于基数 10,您可以除以 Math.log(10)(与 ln(10) 相同):

function log10(val) {
  return Math.log(val) / Math.LN10;
}

Math.LN10Math.log(10) 的内置预计算常量,因此该函数本质上等同于:

function log10(val) {
  return Math.log(val) / Math.log(10);
}

"Change of Base" Formula / Identity

The numerical value for logarithm to the base 10 can be calculated
with the following identity.

Logarithm for base 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)):

function log10(val) {
  return Math.log(val) / Math.LN10;
}

Math.LN10 is a built-in precomputed constant for Math.log(10), so this function is essentially identical to:

function log10(val) {
  return Math.log(val) / Math.log(10);
}
漫雪独思 2024-09-12 18:06:10

很简单,只需除以 log(10) 即可更改底数。甚至还有一个常量可以帮助你,

Math.log(num) / Math.LN10;

它与以下内容相同:

Math.log(num) / Math.log(10);

Easy, just change the base by dividing by the log(10). There is even a constant to help you

Math.log(num) / Math.LN10;

which is the same as:

Math.log(num) / Math.log(10);
四叶草在未来唯美盛开 2024-09-12 18:06:10

您可以简单地将值的对数与所需底数的对数相除,也可以重写 Math.log 方法以接受可选的底数参数:

Math.log = (function() {
  var log = Math.log;
  return function(n, base) {
    return log(n)/(base ? log(base) : 1);
  };
})();

Math.log(5, 10);

You can simply divide the logarithm of your value, and the logarithm of the desired base, also you could override the Math.log method to accept an optional base argument:

Math.log = (function() {
  var log = Math.log;
  return function(n, base) {
    return log(n)/(base ? log(base) : 1);
  };
})();

Math.log(5, 10);
软的没边 2024-09-12 18:06:10
const logBase = (n, base) => Math.log(n) / Math.log(base);

https://en.wikipedia.org/wiki/Logarithm#Change_of_base

const logBase = (n, base) => Math.log(n) / Math.log(base);

https://en.wikipedia.org/wiki/Logarithm#Change_of_base

灯下孤影 2024-09-12 18:06:10

这里的答案会导致明显的精度问题,并且在某些用例中不可靠

> 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

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
他夏了夏天 2024-09-12 18:06:10
Math.log10 = function(n) {
    return (Math.log(n)) / (Math.log(10));
}

然后你可以做

Math.log10(your_number);

注意:最初我想这样做Math.prototype.log10 = ...来做到这一点,但是用户CMS指出Math不是这样工作的,所以我编辑掉了.prototype部分。

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 CMS pointed out that Math doesn't work this way, so I edited out the .prototype part.

哎呦我呸! 2024-09-12 18:06:10

FF 25+ 支持 Math.log10方法。您可以使用polyfill:

if (!Math.log10) Math.log10 = function(t){ return Math.log(t)/Math.LN10; };

MDN 列出了 支持的浏览器

桌面浏览器

Chrome Firefox (Gecko) Internet Explorer Opera Safari
38 25 (25) 不支持 25 7.1

移动浏览器

Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
不支持 不支持 25.0 (25) 不支持 不支持 iOS 8

FF 25+ supports a Math.log10 method. You may to use polyfill:

if (!Math.log10) Math.log10 = function(t){ return Math.log(t)/Math.LN10; };

MDN lists the supported browsers.

Desktop Browsers

Chrome    Firefox (Gecko) Internet Explorer   Opera   Safari
38        25 (25)         Not supported       25      7.1

Mobile 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
回忆追雨的时光 2024-09-12 18:06:10

Math.log10(x)

最上面的答案对于任意基数都可以,但问题是关于对数基数 10,并且 Math.log10(x)自 2015 年以来所有浏览器的标准。*

*IE 除外,如果出于某种原因这对您很重要的话。

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.*

*Except IE, if that's important to you for some reason.

作死小能手 2024-09-12 18:06:10

如果您有一个数字 x,那么使用 Math.log(x) 本质上就是 lnx。

要将其转换为 e 以外的基数,可以使用以下函数:

function(x){ return Math.log(x)/Math.log(10); }

If you have a number x, then use of Math.log(x) would essentially be lnx.

To convert it to a base other than e, you can use the following function :

function(x){ return Math.log(x)/Math.log(10); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文