Javascript 简明教程

JavaScript - Math

JavaScript math 对象提供数学常数和函数的属性和方法。与其他全局对象不同,Math 不是一个构造函数。Math 的所有属性和方法都是静态的,可以在不创建它的前提下使用 Math 作对象进行调用。

The JavaScript math object provides properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.

因此,你可以引用 Math.PI 中的 pi 常量,并且你可以将 Math.sin(x) 调用为正弦函数,其中 x 是该方法的参数。

Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method’s argument.

Syntax

调用 Math 的属性和方法的语法如下 -

The syntax to call the properties and methods of Math are as follows −

var pi_val = Math.PI; // Property

var sine_val = Math.sin(30); // Method

让我们通过下面的示例了解更多关于 Math 对象的属性和方法。

Let’s learn more about the Math object’s properties and method via the examples below.

JavaScript Math Properties

以下是 JavaScript 中 Math 类的属性列表 -

Following is the list of properties of Math class in JavaScript −

Sr.No.

Name & Description

1

EEuler’s constant and the base of natural logarithms, approximately 2.718.

2

LN2Natural logarithm of 2, approximately 0.693.

3

LN10Natural logarithm of 10, approximately 2.302.

4

LOG2EBase 2 logarithm of E, approximately 1.442.

5

LOG10EBase 10 logarithm of E, approximately 0.434.

6

PIThe ratio of a circle’s circumference to its diameter is approximately 3.14159.

7

SQRT1_2The square root of 1/2, equivalently, 1 over the square root of 2, is approximately 0.707.

8

SQRT2The square root of 2, approximately 1.414.

JavaScript Math Methods

以下是 JavaScript 中 Math 类的属性列表 -

Following is the list of methods of Math class in JavaScript −

Sr.No.

Name & Description

1

abs()Returns the absolute value of a number.

2

acos()Returns the arccosine (in radians) of a number.

3

acosh()Returns the inverse hyperbolic consine of a number.

4

asin()Returns the arcsine (in radians) of a number.

5

asinh()Returns the inverse hyperbolic sine of a number.

6

atan()Returns the arctangent (in radians) of a number.

7

atan2()Returns the arctangent of the quotient of its arguments.

8

atanh()Returns the inverse hyperbolic tangent of a number.

9

cbrt()Finds a cube root of a given number.

10

ceil()Returns the smallest integer greater than or equal to a number.

11

clz32()Returns the number of leading zero in 32-bit binary number.

12

cos()Returns the cosine of a number.

13

cosh()It returns the hyperbolic cosine of a number.

14

exp()Returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.

15

expm1()Returns EN - 1, where N is the argument, and E is Euler’s constant, the base of the natural logarithm.

16

floor()Returns the largest integer less than or equal to a number.

17

fround()Returns a nearest 32-bit single precision float representation of the number.

18

hypot()Calculates the square root of the sum of squares of arguments.

19

imul()Calculates the 32-bit multiplication of parameters.

20

log()Returns the natural logarithm (base E) of a number.

21

log10()Returns the logarithm (base 10) of a number.

22

log1p()Return the natural logarithm (base E) of 1 + N, where N is an argument.

23

log2()Returns the base 2 logrithm of a number.

24

max()Returns the largest of zero or more numbers.

25

min()Returns the smallest of zero or more numbers.

26

pow()Returns base to the exponent power that is, base exponent.

27

random()Returns a pseudo-random number between 0 and 1.

28

round()Returns the value of a number rounded to the nearest integer.

29

sign()Return -1 or 1 indicating the sign of the number.

30

sin()Returns the sine of a number.

31

sinh()Return the hyperbolic sin.

32

sqrt()Returns the square root of a number.

33

tan()Returns the tangent of a number.

34

tanh()Returns the hyperbolic tangent of the number.

35

trunc()Returns the integer part of the number.

在以下部分中,我们将通过一些示例来说明与 Math 关联的方法的用法。

In the following sections, we will have a few examples to demonstrate the usage of the methods associated with Math.

Example (Math object Properties)

下面的示例说明 Math 对象的每个属性都具有一个常数值。

The example below demonstrates that each property of the Math object has a constant value.

在这里,我们访问了 ‘E’, ‘LN2’, and ‘PI’ properties 的值。

Here, we have accessed the values of the ‘E’, ‘LN2’, and ‘PI’ properties.

<html>
<head>
<title> JavaScript - Math object's properties </title>
</head>
<body>
<p id = "output"> </p>
<script>
   document.getElementById("output").innerHTML =
      "Math.E == " + Math.E + "<br>" +
      "Math.LN2 == " + Math.LN2 + "<br>" +
      "Math.LN10 == " + Math.LN10 + "<br>" +
      "Math.PI == " + Math.PI + "<br>"+
      "Math.LOG2E == " + Math.LOG2E + "<br>" +
      "Math.LOG10E == " + Math.LOG10E;
</script>
</body>
</html>

Output

在执行上述程序后,它将返回提供的 Math 属性的值。

After executing the above program, it returns the values of the provided Math properties.

Example (Math ceil() method)

在这里,我们正在计算 JavaScript ceil() 方法以返回大于作为参数传递的数字的最小较大的整数。此处,该方法为 5.9 值返回 6。

Here, we are computing the JavaScript ceil() method to return the smallest larger integer value than the number passed as an argument. Here, the method returns 6 for the 5.9 value.

<html>
<head>
<title> JavaScript - Math.ceil() method </title>
</head>
<body>
<p id = "output"> </p>
<script>
   let ans = Math.ceil(5.9);
   document.getElementById("output").innerHTML =
      "Math.ceil(5.9) = " + ans;
</script>
</body>
</html>

Output

在执行上述程序后,它将结果返回为 6。

After executing the above program, it returns the result as 6.

Example (Math max() method)

Math.max() 方法用于获取作为数组传递的参数中的最大值。

The Math.max() method is used to get the maximum value among the arguments passed as an array.

在这里,我们已向 Math.max() 对象传递了六个参数,该方法将从它们中返回最大值。

Here, we have passed six arguments to the Math.max() object, and the method returns the maximum value from them.

<html>
<head>
<title> JavaScript - Math.max() method </title>
</head>
<body>
<p id = "output"> </p>
<script>
   let ans = Math.max(100, 10, -5, 89, 201, 300);
   document.getElementById("output").innerHTML =
      "Math.max(100, 10, -5, 89, 201, 300) = " + ans + "<br>";
</script>
</body>
</html>

Output

在执行上述程序后,它返回 300 作为最大值。

After executing the above program, it returns 300 as maximum value.

Example (Math.cos() method)

Math.cos() 方法返回作为参数传递的数字的余弦值。0 的余弦值为 1,您可以在以下示例的输出中看到它。

The Math.cos() method returns the cosine value of the number passed as an argument. The cosine value of 0 is 1, which you can see in the output of the example below.

<html>
<head>
<title> JavaScript - Math.cos() method </title>
</head>
<body>
<p id = "output"> </p>
<script>
   let ans = Math.cos(0);
   document.getElementById("output").innerHTML = "Math.cos(0) = " + ans;
</script>
</body>
</html>

Output

如果我们执行上述程序,它将返回 "1" 作为结果。

If we execute the above program, it returns "1" as result.