(JavaScript | Math.log2() Method)
Math operations in JavaScript are handled using functions of math library in JavaScript. In this tutorial on Math.log2() method, we will learn about the log2() method
Math.log2()方法的本教程中,我们将学习log2()方法及其示例。
JavaScript has multiple methods in its math library to support mathematical operations in JavaScript.
JavaScript在其数学库中有多种方法来支持JavaScript中的数学运算。
Math.log2() method in JavaScript is inbuilt. This function returns the value of the logarithmic value of base 2 of the number.
Math.log2()方法是内置的。 此函数返回数字以2为底的对数值的值
Syntax:
句法:
Math.log2(n);
Parameter(s):
参数:
- n
n –仅采用一个值,该值是要找到其对数的数字。
Return value:
返回值:
The return type of this method is number, it returns the log2 of the given number.
该方法的返回类型为number ,它返回给定数字的log 2 。
Example 1: finding the log of an integer.
示例1:查找整数的对数。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
document.write(Math.log2(3) + "<br>");
document.write(Math.log2(87) + "<br>");
document.write(Math.log2(102) + "<br>");
document.write(Math.log2(512));
</script>
</body>
</html>
Output
输出量
Example 2: finding log of a string.
示例2:查找字符串的日志。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
document.write(Math.log2("Include help"));
</script>
</body>
</html>
Output
输出量
When a string is passed to the log2() method. It returns NaN
log 2 ()方法时
Example 3: Find the log of a complex number.
示例3:查找复数的日志。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
document.write(Math.log2(1 + 2 i));
</script>
</body>
</html>
Output
输出量
Invalid or unexpected token
翻译自: https://www.includehelp.com/code-snippets/math-log2-method-with-example-in-javascript.aspx