JavaScript Math 对象

来源:互联网 发布:mac版的photoshop cc 编辑:程序博客网 时间:2024/06/05 08:50

abs() 方法


abs() 方法可返回数的绝对值。


document.write(Math.abs(7.25))


document.write(Math.abs(-7.25))


document.write(Math.abs(7.25-10))


输出的是:7.257.252.75


acos() 方法

acos() 方法可返回一个数的反余弦。


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))


输出的是:0.8762980611683406
1.5707963267948966
3.141592653589793
0
NaN


asin() 方法


asin() 方法可返回一个数的反正弦值。


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))


输出的是:0.694498265626556
0
-1.5707963267948966
1.5707963267948966
NaN


ceil() 方法


ceil() 方法可对一个数进行上舍入。


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))


输出的是:1
1
5
6
-5
-5


cos() 方法


cos() 方法可返回一个数字的余弦值。


document.write(Math.cos(3) + "<br />")


document.write(Math.cos(-3) + "<br />")


document.write(Math.cos(0) + "<br />")


document.write(Math.cos(Math.PI) + "<br />")


document.write(Math.cos(2*Math.PI))


输出的是:-0.9899924966004454
-0.9899924966004454
1
-1
1


exp() 方法


exp() 方法可返回 e 的 x 次幂的值。


document.write(Math.exp(1))


输出的是:2.718281828459045


floor() 方法

floor() 方法可对一个数进行下舍入


document.write(Math.floor(0.60))


输出的是:0


sin() 方法

sin() 方法可返回一个数字的正弦。


document.write(Math.sin(3) + "<br />")


document.write(Math.sin(-3) + "<br />")


输出的是:0.1411200080598672
-0.1411200080598672


sqrt() 方法

sqrt() 方法可返回一个数的平方根。


var a=Math.sqrt(0);


var b=Math.sqrt(1);


var c=Math.sqrt(9);


var d=Math.sqrt(0.64);


var e=Math.sqrt(-9);


输出的是:0
1
3
0.8
NaN


tan() 方法

tan() 方法可返回一个表示某个角的正切的数字。


document.write(Math.tan(0.50) + "<br />")


document.write(Math.tan(-0.50) + "<br />")


document.write(Math.tan(5) + "<br />")


document.write(Math.tan(10) + "<br />")


document.write(Math.tan(-5) + "<br />")


document.write(Math.tan(-10))


输出的是:0.5463024898437905
-0.5463024898437905
-3.380515006246586
0.6483608274590866
3.380515006246586
-0.6483608274590866


max() 方法

max() 方法可返回两个指定的数中带有较大的值的那个数。


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))


输出的是:7
5
-3
7.3


pow() 方法


pow() 方法可返回 x 的 y 次幂的值。



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 />")


输出的是:1
0
1
1
8
-8
16
16