📜  JavaScript中的math

📅  最后修改于: 2020-10-23 15:05:24             🧑  作者: Mango

JavaScript数学

JavaScript数学对象提供了一些常数和方法来执行数学运算。与日期对象不同,它没有构造函数。

JavaScript数学方法

让我们看一下带有描述的JavaScript Math方法列表。

Methods Description
abs() It returns the absolute value of the given number.
acos() It returns the arccosine of the given number in radians.
asin() It returns the arcsine of the given number in radians.
atan() It returns the arc-tangent of the given number in radians.
cbrt() It returns the cube root of the given number.
ceil() It returns a smallest integer value, greater than or equal to the given number.
cos() It returns the cosine of the given number.
cosh() It returns the hyperbolic cosine of the given number.
exp() It returns the exponential form of the given number.
floor() It returns largest integer value, lower than or equal to the given number.
hypot() It returns square root of sum of the squares of given numbers.
log() It returns natural logarithm of a number.
max() It returns maximum value of the given numbers.
min() It returns minimum value of the given numbers.
pow() It returns value of base to the power of exponent.
random() It returns random number between 0 (inclusive) and 1 (exclusive).
round() It returns closest integer value of the given number.
sign() It returns the sign of the given number
sin() It returns the sine of the given number.
sinh() It returns the hyperbolic sine of the given number.
sqrt() It returns the square root of the given number
tan() It returns the tangent of the given number.
tanh() It returns the hyperbolic tangent of the given number.
trunc() It returns an integer part of the given number.

Math.sqrt(n)

JavaScript math.sqrt(n)方法返回给定数字的平方根。

Square Root of 17 is:   
  

输出:

Square Root of 17 is: 4.123105625617661  

Math.random()

JavaScript math.random()方法返回0到1之间的随机数。

Random Number is:   
 

输出:

Random Number is: 0.6188330422885244  

Math.pow(m,n)

JavaScript math.pow(m,n)方法将m返回为mn的n的幂。

3 to the power of 4 is:   
  

输出:

3 to the power of 4 is: 81  

Math.floor(n)

JavaScript math.floor(n)方法返回给定数字的最小整数。例如3表示3.7,5表示5.9等。

Floor of 4.6 is:   
  

输出:

Floor of 4.6 is: 4    

Math.ceil(n)

JavaScript math.ceil(n)方法返回给定数字的最大整数。例如3.7表示4,5.9表示6等。

Ceil of 4.6 is:   
  

输出:

Ceil of 4.6 is: 5    

Math.round(n)

JavaScript math.round(n)方法返回最接近给定数字的舍入整数。如果小数部分等于或大于0.5,则变为上限值1,否则变为下限值0。例如3.7的4、3.3的3、5.9的6等。

Round of 4.3 is: 
Round of 4.7 is:

输出:

Round of 4.3 is: 4  
Round of 4.7 is: 5    

Math.abs(n)

JavaScript math.abs(n)方法返回给定数字的绝对值。例如-4为-4,6.6为-6.6等。

Absolute value of -4 is:     
 

输出:

Absolute value of -4 is: 4