如何在 JavaScript 中指定 Math.log() 的底数?
在本教程中,我们将学习如何计算任何数字值的对数并为 Math.log() 方法指定底数。
数学中的对数运算在很多地方都有用。例如,如果要显示数十亿个值的图形表示,则很难在屏幕上渲染。为此,我们使用对数更改图形的比例,这样就可以轻松生成和可视化任何特定操作的输出。
我们还可以使用对数函数的更多示例。
使用 Math.log() 计算对数
为 Math.log() 指定不同的底数
使用 Math.log10() 指定底数 10
使用 Math.log() 计算对数
在 JavaScript 中,Math.log() 是 Math 库的内置方法,用于计算不同数字值的自然对数。它将数字值作为参数,并返回以 E 为底的数字的对数,这意味着自然对数。用数学语言来说,我们可以说 Math.log() 返回 ln( number ) 的值。
语法
以下是 JavaScript 中使用 Math.log() 函数的语法。
let output = Math.log( number )
参数
number − 我们要计算其自然对数的值。
返回值
Math.log() 函数返回正数值的自然对数。对于 0,它返回 -Infinity,对于负数和非数值,它返回 NaN。
对于字符串数字输入,函数会自动从字符串中解析数字值并返回输出。
示例
以下示例演示了使用 Math.log() 方法和不同的输入值。
<!DOCTYPE html>
<html>
<head>
<title> Calculating the log using the Math.log(). </title>
</head>
<body>
<h2> Calculate the natural logarithm using the Math.log() function </h2>
<h4> Natural logarithm of 10000 </h4>
<div id="output1"> </div>
<h4> Natural logarithm of -165 </h4>
<div id="output2"> </div>
<h4> Natural logarithm of "223" </h4>
<div id="output3"> </div>
<h4> natural logarithm of "2ws" </h4>
<div id="output4"> </div>
<script type="text/javascript">
let output1 = document.getElementById("output1");
let output2 = document.getElementById("output2");
let output3 = document.getElementById("output3");
let output4 = document.getElementById("output4");
var number = 10000;
output1.innerHTML = Math.log(number);
number = -165;
output2.innerHTML = Math.log(number);
number = "223"; // function automatically parses the number from string
output3.innerHTML = Math.log(number);
number = "2ws";
output4.innerHTML = Math.log(number);
</script>
</body>
</html>在以上输出中,用户可以看到,对于负数和非数值,Math.log() 函数返回 NaN。
为 Math.log() 指定不同的底数
在本节中,我们将学习如何为对数函数指定不同的底数。JavaScript 中没有内置的功能来更改对数的底数,但我们可以应用一些数学逻辑来更改底数。
在数学中,
$$\mathrm{\mathit{log_{b}}\left ( \mathit{X} \right )=\:\mathit{log_{nb}\left ( X \right )}/\mathit{log_{nb}\left ( b \right )}}$$
使用上述方法,我们可以更改数学中对数的底数。上述公式表明,如果用户想要更改对数的底数,他们可以通过为两者设置一个新底数 (nb) 来将对数值除以底数的对数。在我们的例子中,我们将为两个值都设置自然对数底数 (e)。
语法
用户可以按照以下语法更改底数。
let output = Math.log( number ) / Math.log( base );
示例
以下示例演示了如何在 JavaScript 中使用对数函数的不同底数。
<!DOCTYPE html>
<html>
<head>
<title> Calculating the log using the Math.log(). </title>
</head>
<body>
<h2> Calculate the logarithm of numbers with different bases </h2>
<h4> Logarithm of 4000 with respect to base 40. </h4>
<div id="output1"> </div>
<h4> Logarithm of 4000 with respect to base 20. </h4>
<div id="output2"> </div>
<script type="text/javascript">
let output1 = document.getElementById("output1");
let output2 = document.getElementById("output2");
var number = 4000;
output1.innerHTML = Math.log(number) / Math.log(40);
output2.innerHTML = Math.log(number) / Math.log(20);
</script>
</body>
</html>使用 Math.log10() 方法指定底数 10
Math.log10() 方法与 Math.log() 方法相同。两种方法之间的区别在于 Math.log10() 方法使用 底数 10,而 Math.log() 方法使用 自然对数 (e) 作为底数。
语法
用户可以按照以下语法使用 Math.log10() 方法。
Let output = Math.log10( number );
示例
用户可以从以下示例中了解如何使用 Math.log10() 方法。
<!DOCTYPE html>
<html>
<head>
<title> Calculating the log using the Math.log10(). </title>
</head>
<body>
<h2> Calculate the logarithm of numbers using the Math.log10() Method</h2>
<h4> Logarithm of 100000 with base 10 </h4>
<div id="output1"> </div>
<h4> Logarithm of 500000 with respect to base 10. </h4>
<div id="output2"> </div>
<script>
let output1 = document.getElementById("output1");
let output2 = document.getElementById("output2");
var number = 100000;
output1.innerHTML = Math.log10(number);
number = 500000;
output2.innerHTML = Math.log10(number);
</script>
</body>
</html>结论
本教程教我们如何更改 Math.log() 函数的底数。如果用户想将对数的底数更改为 10,则应使用第三种方法。对于不同的底数,用户可以使用第二种方法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP