Math 对象用于执行数学任务。
使用 Math 的属性和方法的语法:
var pi_value=Math.PI;
var sqrt_value=Math.sqrt(15);
注释:Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,不是某个对象的方法。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。
Math 对象属性
属性 | 描述 |
---|---|
E | 返回算术常量 e,即自然对数的底数(约等于2.718)。 |
LN2 | 返回 2 的自然对数(约等于0.693)。 |
LN10 | 返回 10 的自然对数(约等于2.302)。 |
LOG2E | 返回以 2 为底的 e 的对数(约等于 1.414)。 |
LOG10E | 返回以 10 为底的 e 的对数(约等于0.434)。 |
PI | 返回圆周率(约等于3.14159)。 |
SQRT1_2 | 返回返回 2 的平方根的倒数(约等于 0.707)。 |
SQRT2 | 返回 2 的平方根(约等于 1.414)。 |
Math 对象方法
方法 | 描述 |
---|---|
abs(x) | 返回数的绝对值。 |
acos(x) | 返回数的反余弦值。 |
asin(x) | 返回数的反正弦值。 |
atan(x) | 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。 |
atan2(y,x) | 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。 |
ceil(x) | 对数进行上舍入。 |
cos(x) | 返回数的余弦。 |
exp(x) | 返回 e 的指数。 |
floor(x) | 对数进行下舍入。 |
log(x) | 返回数的自然对数(底为e)。 |
max(x,y) | 返回 x 和 y 中的最高值。 |
min(x,y) | 返回 x 和 y 中的最低值。 |
pow(x,y) | 返回 x 的 y 次幂。 |
random() | 返回 0 ~ 1 之间的随机数。 |
round(x) | 把数四舍五入为最接近的整数。 |
sin(x) | 返回数的正弦。 |
sqrt(x) | 返回数的平方根。 |
tan(x) | 返回角的正切。 |
abs() 方法可返回数的绝对值。
语法
Math.abs(x)
返回值
x 的绝对值。
取得正数和负数的绝对值:
console.log(Math.abs(7.25))//7.25
console.log(Math.abs(-7.25))//7.25
console.log(Math.abs(7.25-10))//2.75
acos() 方法可返回一个数的反余弦。
语法
Math.acos(x)
返回值
x 的反余弦值。返回的值是 0 到 PI 之间的弧度值。
<script type="text/javascript">
document.write(Math.acos(0.64) + "<br />")
document.write(Math.acos(0) + "<br />")
document.write(Math.acos(-1) + "<br />")
document.write(Math.acos(1) + "<br />")
document.write(Math.acos(2))
</script>
0.8762980611683406
1.5707963267948965
3.141592653589793
0
NaN
asin() 方法可返回一个数的反正弦值。
语法
Math.asin(x)
返回值
x 的反正弦值。返回的值是 -PI/2 到 PI/2 之间的弧度值。
<script type="text/javascript">
document.write(Math.asin(0.64) + "<br />")
document.write(Math.asin(0) + "<br />")
document.write(Math.asin(-1) + "<br />")
document.write(Math.asin(1) + "<br />")
document.write(Math.asin(2))
</script>
0.6944982656265559
0
-1.5707963267948965
1.5707963267948965
NaN
ceil() 方法可对一个数进行上舍入。
语法
Math.ceil(x)
返回值
大于等于 x,并且与它最接近的整数。
<script type="text/javascript">
document.write(Math.ceil(0.60) + "<br />")
document.write(Math.ceil(0.40) + "<br />")
document.write(Math.ceil(5) + "<br />")
document.write(Math.ceil(5.1) + "<br />")
document.write(Math.ceil(-5.1) + "<br />")
document.write(Math.ceil(-5.9))
</script>
1
1
5
6
-5
-5
floor() 方法可对一个数进行下舍入
语法
Math.floor(x)
返回值
小于等于 x,且与 x 最接近的整数。
<script type="text/javascript">
document.write(Math.floor(0.60) + "<br />")
document.write(Math.floor(0.40) + "<br />")
document.write(Math.floor(5) + "<br />")
document.write(Math.floor(5.1) + "<br />")
document.write(Math.floor(-5.1) + "<br />")
document.write(Math.floor(-5.9))
</script>
0
0
5
5
-6
-6
max() 方法可返回两个指定的数中带有较大的值的那个数。
语法
Math.max(x...)
返回值
参数中最大的值。如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。
<script type="text/javascript">
document.write(Math.max(5,7) + "<br />")
document.write(Math.max(-3,5) + "<br />")
document.write(Math.max(-3,-5) + "<br />")
document.write(Math.max(7.25,7.30))
</script>
7
5
-3
7.3
min() 方法可返回指定的数字中带有最低值的数字。
语法
Math.min(x,y)
返回值
参数中最小的值。如果没有参数,则返回 Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。
<script type="text/javascript">
document.write(Math.min(5,7) + "<br />")
document.write(Math.min(-3,5) + "<br />")
document.write(Math.min(-3,-5) + "<br />")
document.write(Math.min(7.25,7.30))
</script>
5
-3
-5
7.25
pow() 方法可返回 x 的 y 次幂的值。
语法
Math.pow(x,y)
返回值
x 的 y 次幂。
<script type="text/javascript">
document.write(Math.pow(0,0) + "<br />")
document.write(Math.pow(0,1) + "<br />")
document.write(Math.pow(1,1) + "<br />")
document.write(Math.pow(1,10) + "<br />")
document.write(Math.pow(2,3) + "<br />")
document.write(Math.pow(-2,3) + "<br />")
document.write(Math.pow(2,4) + "<br />")
document.write(Math.pow(-2,4) + "<br />")
</script>
1
0
1
1
8
-8
16
16
random() 方法可返回介于 0 ~ 1 之间的一个随机数。
语法
Math.random()
返回值
0.0 ~ 1.0 之间的一个伪随机数。
<script type="text/javascript">
document.write(Math.random())
</script>
0.3546745664884482
round() 方法可把一个数字舍入为最接近的整数。
语法
Math.round(x)
返回值
与 x 最接近的整数。
<script type="text/javascript">
document.write(Math.round(0.60) + "<br />")
document.write(Math.round(0.50) + "<br />")
document.write(Math.round(0.49) + "<br />")
document.write(Math.round(-4.40) + "<br />")
document.write(Math.round(-4.60))
1
1
0
-4
-5