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

查看:197
本文介绍了如何在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.

因为JavaScript中的 Math.log(x)返回 x (与 ln(x)相同),对于基数10,你可以除以 Math.log(10)(与<相同) em> 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.LN10 Math.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天全站免登陆