屬性
1. E
代表算術常量 e,即自然對數的底數,其值近似於 2.71828
Math.E
document.write("Euler's number: " + Math.E);
2. LN2
是 loge2,即 2 的自然對數,其值近似於 0.69314718055994528623
Math.LN2
document.write("LN2: " + Math.LN2);
3. LN10
是 loge10,即 10 的自然對數,其值近似於 2.3025850929940459011
Math.LN10
document.write("LN10: " + Math.LN10);
4. LOG2E
是 log2e,即 2 爲底 e 的對數,其值近似於 1.442695040888963387
Math.LOG2E
document.write("LOG2E: " + Math.LOG2E);
5. LOG10E
是 log10e,即 10 爲底 e 的對數,其值近似於 0.43429448190325181667
Math.LOG10E
document.write("LOG10E: " + Math.LOG10E);
6. PI
是 π,即圓的周長和它的直徑之比。這個值近似爲 3.141592653589793
Math.PI
document.write("PI: " + Math.PI);
7. SQRT1_2
返回 2 的平方根的倒數。這個值近似爲 0.7071067811865476
Math.SQRT1_2
document.write("SQRT1_2: " + Math.SQRT1_2);
8. SQRT2
返回 2 的平方根。這個值近似爲 1.4142135623730951
Math.SQRT2
document.write("SQRT2: " + Math.SQRT2);
方法
1. abs
返回數的絕對值
返回值
x 的絕對值。
Math.abs(x)
參數
- 必需
- x 必須是一個數值。
document.write(Math.abs(7.25) + "<br />")
document.write(Math.abs(-7.25) + "<br />")
document.write(Math.abs(7.25-10))
2. acos
返回一個數的反餘弦
返回值
x 的反餘弦值。返回的值是 0 到 PI 之間的弧度值。
註釋:如果參數 x 超過了 -1.0 ~ 1.0 的範圍,那麼瀏覽器將返回 NaN。
註釋:如果參數 x 取值 -1,那麼將返回 PI。
Math.acos(x)
參數
- 必需
- x 必須是 -1.0 ~ 1.0 之間的數。
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.5707963267948965
3.141592653589793
0
NaN
3. asin
返回一個數的反正弦值
返回值
x 的反正弦值。返回的值是 -PI/2 到 PI/2 之間的弧度值。
註釋:如果參數 x 超過了 -1.0 ~ 1.0 的範圍,那麼瀏覽器將返回 NaN。
註釋:如果參數 x 取值 -1,那麼將返回 PI/2。
Math.asin(x)
參數
- 必需
- x 必須是 -1.0 ~ 1.0 之間的數。
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.6944982656265559
0
-1.5707963267948965
1.5707963267948965
NaN
4. atan
返回數字的反正切值
返回值
x 的反正切值。返回的值是 -PI/2 到 PI/2 之間的弧度值。
Math.atan(x)
參數
- 必需
- x 必須是 -1.0 ~ 1.0 之間的數。
document.write(Math.atan(0.50) + "<br />")
document.write(Math.atan(-0.50) + "<br />")
document.write(Math.atan(5) + "<br />")
document.write(Math.atan(10) + "<br />")
document.write(Math.atan(-5) + "<br />")
document.write(Math.atan(-10))
結果:
0.4636476090008061
-0.4636476090008061
1.373400766945016
1.4711276743037347
-1.373400766945016
-1.4711276743037347
5. atan2
返回從 x 軸到點 (x,y) 之間的角度
返回值
-PI 到 PI 之間的值,是從 X 軸正向逆時針旋轉到點 (x,y) 時經過的角度。
註釋:請注意這個函數的參數順序,Y 座標在 X 座標之前傳遞。
Math.atan2(x,y)
參數
- 必需
- x 指定點的 X 座標。
- y 指定點的 Y 座標。
document.write(Math.atan2(0.50,0.50) + "<br />")
document.write(Math.atan2(-0.50,-0.50) + "<br />")
document.write(Math.atan2(5,5) + "<br />")
document.write(Math.atan2(10,20) + "<br />")
document.write(Math.atan2(-5,-5) + "<br />")
document.write(Math.atan2(-10,10))
結果:
0.7853981633974483
-2.356194490192345
0.7853981633974483
0.4636476090008061
-2.356194490192345
-0.7853981633974483
6. ceil
可對一個數進行上舍入
返回值
大於等於 x,並且與它最接近的整數。
說明
ceil() 方法執行的是向上取整計算,它返回的是大於或等於函數參數,並且與之最接近的整數。
Math.ceil(x)
參數
- 必需
- x 必須是一個數值。
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
7. cos
返回一個數字的餘弦值
返回值
x 的餘弦值。返回的是 -1.0 到 1.0 之間的數。
Math.cos(x)
參數
- 必需
- x 必須是一個數值。
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
8. exp
返回 e 的 x 次冪的值
返回值
返回 e 的 x 次冪。e 代表自然對數的底數,其值近似爲 2.71828。
Math.exp(x)
參數
- 必需
- x 任意數值或表達式。被用作指數。
document.write(Math.exp(1) + "<br />")
document.write(Math.exp(-1) + "<br />")
document.write(Math.exp(5) + "<br />")
document.write(Math.exp(10) + "<br />")
結果:
2.718281828459045
0.36787944117144233
148.4131591025766
22026.465794806718
9. floor
對一個數進行下舍入
返回值
小於等於 x,且與 x 最接近的整數。
說明
floor() 方法執行的是向下取整計算,它返回的是小於或等於函數參數,並且與之最接近的整數。
Math.floor(x)
參數
- 必需
- x 任意數值或表達式。被用作指數。
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))
結果:
0
0
5
5
-6
-6
10. log
返回一個數的自然對數
返回值
x 的自然對數。
說明
參數 x 必須大於 0。
Math.log(x)
參數
- 必需
- x 任意數值或表達式。
document.write(Math.log(2.7183) + "<br />")
document.write(Math.log(2) + "<br />")
document.write(Math.log(1) + "<br />")
document.write(Math.log(0) + "<br />")
document.write(Math.log(-1))
結果:
1.0000066849139877
0.6931471805599453
0
-Infinity
NaN
11. max
返回兩個指定的數中帶有較大的值的那個數
返回值
參數中最大的值。如果沒有參數,則返回 -Infinity。如果有某個參數爲 NaN,或是不能轉換成數字的非數字值,則返回 NaN。
Math.max(x...)
參數
x 0 或多個值。在 ECMASCript v3 之前,該方法只有兩個參數。
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
12. min
返回指定的數字中帶有最低值的數字
返回值
參數中最小的值。如果沒有參數,則返回 Infinity。如果有某個參數爲 NaN,或是不能轉換成數字的非數字值,則返回 NaN。
Math.min(x...)
參數
x 0 或多個值。在 ECMASCript v3 之前,該方法只有兩個參數。
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))
結果:
5
-3
-5
7.25
13. pow
可返回 x 的 y 次冪的值
返回值
x 的 y 次冪。
說明
如果結果是虛數或負數,則該方法將返回 NaN。如果由於指數過大而引起浮點溢出,則該方法將返回 Infinity。
Math.pow(x,y)
參數
- 必需
- 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
14. random
可返回介於 0 ~ 1 之間的一個隨機數
返回值
0.0 ~ 1.0 之間的一個僞隨機數。
Math.random()
document.write(Math.random())
15. round
可把一個數字舍入爲最接近的整數
返回值
與 x 最接近的整數。
說明
對於 0.5,該方法將進行上舍入。
例如,3.5 將舍入爲 4,而 -3.5 將舍入爲 -3。
Math.round(x)
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
16. sin
返回一個數字的正弦
返回值
參數 x 的正弦值。返回值在 -1.0 到 1.0 之間。
Math.sin(x)
參數
- 必需
- x 一個以弧度表示的角。將角度乘以 0.017453293 (2PI/360)即可轉換爲弧度。
document.write(Math.sin(3) + "<br />")
document.write(Math.sin(-3) + "<br />")
document.write(Math.sin(0) + "<br />")
document.write(Math.sin(Math.PI) + "<br />")
document.write(Math.sin(Math.PI/2)
結果
0.1411200080598672
-0.1411200080598672
0
1.2246063538223772e-16
1
17. sqrt
可返回一個數的平方根
返回值
參數 x 的平方根。如果 x 小於 0,則返回 NaN。
Math.sqrt(x)
參數
- 必需
- x 必須是大於等於 0 的數。
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
18. tan
返回一個表示某個角的正切的數字
返回值
參數 x 的正切值。
Math.tan(x)
參數
- 必需
- x 一個以弧度表示的角。將角度乘以 0.017453293 (2PI/360)即可轉換爲弧度。
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
文檔內容出自 W3cSchool和菜鳥教程,
如需查看更詳細的有關內容 請登錄 http://www.w3school.com.cn/ 和 http://www.runoob.com/