如何在 JavaScript 中指定 Math.log() 的基础? [英] How can I specify the base for Math.log() in JavaScript?

查看:23
本文介绍了如何在 JavaScript 中指定 Math.log() 的基础?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个用于 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?

推荐答案

改变基础"公式/标识

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

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

由于 Math.log(x) 在 JavaScript 中返回 x 的自然对数(与 ln(x) 相同),对于基数10 你可以除以Math.log(10)(同ln(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.LN10Math.log(10) 的内置预计算常量,因此该函数本质上等同于:

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

这篇关于如何在 JavaScript 中指定 Math.log() 的基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆