亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Javascript之Math對(duì)象詳解

 更新時(shí)間:2016年06月07日 11:49:00   作者:meaijojo  
本文主要介紹javascript中的Math對(duì)象的用法,講解的很詳細(xì),希望能給大家做一個(gè)參考。

Math對(duì)象不同于上述的對(duì)象,它可以說是一個(gè)公共數(shù)學(xué)類,里面有很多數(shù)學(xué)方法,用于各種數(shù)學(xué)運(yùn)算
但是Math對(duì)象不需要構(gòu)造,對(duì)于其中的方法直接使用即可

1、常量(即屬性)

E       返回算術(shù)常量 e,即自然對(duì)數(shù)的底數(shù)(約等于2.718)

E 返回算術(shù)常量 e,即自然對(duì)數(shù)的底數(shù)(約等于2.718)
LN2 返回 2 的自然對(duì)數(shù)(約等于0.693)
LN10 返回 10 的自然對(duì)數(shù)(約等于2.302)
LOG2E 返回以 2 為底的 e 的對(duì)數(shù)(約等于 1.414)
LOG10E 返回以 10 為底的 e 的對(duì)數(shù)(約等于0.434)
PI 返回圓周率(約等于3.14159)
SQRT1_2 返回返回 2 的平方根的倒數(shù)(約等于 0.707)
SQRT2 返回 2 的平方根(約等于 1.414)

下面是它們的值:

復(fù)制代碼 代碼如下:
document.write("Math.E = "+Math.E+"<br>");
document.write("Math.LN2 = "+Math.LN2+"<br>");
document.write("Math.LN10 = "+Math.LN10+"<br>");
document.write("Math.LOG2E = "+Math.LOG2E+"<br>");
document.write("Math.LOG10E = "+Math.LOG10E+"<br>");
document.write("Math.PI = "+Math.PI+"<br>");
document.write("Math.SQRT1_2 = "+Math.SQRT1_2+"<br>");
document.write("Math.SQRT2 = "+Math.SQRT2+"<br>");

輸出結(jié)果:

Math.E = 2.718281828459045
Math.LN2 = 0.6931471805599453
Math.LN10 = 2.302585092994046
Math.LOG2E = 1.4426950408889634
Math.LOG10E = 0.4342944819032518
Math.PI = 3.141592653589793
Math.SQRT1_2 = 0.7071067811865476
Math.SQRT2 = 1.4142135623730951

2、abs() 方法可返回?cái)?shù)的絕對(duì)值

Math.abs(x);x必須為一個(gè)數(shù)值,此數(shù)可以是整數(shù),小數(shù)都可以
document.write(Math.abs(-2.77));//輸出2.77

3、acos(x) 返回?cái)?shù)的反余弦值。

Math.acos(x);x必須是 -1.0 ~ 1.0 之間的數(shù)
如果x不在上述范圍,則返回NaN

4、asin() 方法可返回一個(gè)數(shù)的反正弦值。

Math.asin(x);x必須是一個(gè)數(shù)值,該值介于 -1.0 ~ 1.0 之間。
如果參數(shù) x 超過了 -1.0 ~ 1.0 的范圍,那么瀏覽器將返回 NaN。

5、atan() 方法可返回?cái)?shù)字的反正切值。

Math.atan(x);x 必需。必須是一個(gè)數(shù)值。
返回的值是 -PI/2 到 PI/2 之間的弧度值。

6、atan2() 方法可返回從 x 軸到點(diǎn) (x,y) 之間的角度。

Math.atan2(y,x)
-PI 到 PI 之間的值,是從 X 軸正向逆時(shí)針旋轉(zhuǎn)到點(diǎn) (x,y) 時(shí)經(jīng)過的角度。

7、ceil() 方法可對(duì)一個(gè)數(shù)進(jìn)行上舍入。

什么是上舍入?即大于等于 x,并且與它最接近的整數(shù)。
Math.ceil(x);x 必需。必須是一個(gè)數(shù)值。

復(fù)制代碼 代碼如下:
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

對(duì)于負(fù)數(shù),你懂的

8、cos() 方法可返回一個(gè)數(shù)字的余弦值。

Math.cos(x);x 必需。必須是一個(gè)數(shù)值。 返回的是 -1.0 到 1.0 之間的數(shù)。、
x其實(shí)要求是輸入一個(gè)弧度值,例如--->
π代表的是180°等,π即Math.PI
document.write(Math.cos(Math.PI));
輸出為-1

但是假如:

復(fù)制代碼 代碼如下:
document.write(Math.cos(Math.PI/2));

輸出為:6.123233995736766e-17

而:

復(fù)制代碼 代碼如下:
document.write(Math.cos(Math.PI/3));

輸出為:0.5000000000000001

為什么會(huì)出現(xiàn)這些怪異的數(shù)字呢?

其實(shí)大家都知道document.write(Math.cos(Math.PI/2));應(yīng)該輸出0,而在Javascript中可能沒有求的0,所以就用了一個(gè)非常非常小的數(shù)代替
類似的document.write(Math.cos(Math.PI/3));應(yīng)該是0.5才對(duì),但是卻在最后面多了一位
這些是小問題,沒啥好說的,本身寄存器就不可能表示所有數(shù)的,因此在計(jì)算過程中出現(xiàn)差錯(cuò)也很正常

9、exp() 方法可返回 e 的 x 次冪的值。

Math.exp(x);x 必需。任意數(shù)值或表達(dá)式。被用作指數(shù)。
返回 e 的 x 次冪。e 代表自然對(duì)數(shù)的底數(shù),其值近似為 2.71828。
document.write(Math.exp(1) + "<br />");//輸出2.718281828459045

10、floor() 方法可對(duì)一個(gè)數(shù)進(jìn)行下舍入。

和ceil()方法相對(duì)應(yīng),floor()方法是對(duì)一個(gè)數(shù)進(jìn)行下舍入,即小于等于 x,且與 x 最接近的整數(shù)。
Math.floor(x);

復(fù)制代碼 代碼如下:
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

對(duì)于負(fù)數(shù),你懂的

11、log() 方法可返回一個(gè)數(shù)的自然對(duì)數(shù)。

Math.log(x);//參數(shù) x 必須大于 0,大于0則結(jié)果為NaN,等于0則為-Infinity

復(fù)制代碼 代碼如下:
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
從上面我們可以看出

12、max() 方法可返回兩個(gè)指定的數(shù)中帶有較大的值的那個(gè)數(shù)。

Math.max(x...),//x 為0或多個(gè)值。在 ECMASCript v3 之前,該方法只有兩個(gè)參數(shù)。
返回值:
參數(shù)中最大的值。
如果沒有參數(shù),則返回 -Infinity。
如果有某個(gè)參數(shù)為 NaN,或是不能轉(zhuǎn)換成數(shù)字的非數(shù)字值,則返回 NaN。
如下例:

復(fù)制代碼 代碼如下:
document.write(Math.max(5,3,8,1));//8
document.write(Math.max(5,3,8,'M'));//NaN
document.write(Math.max(5));//5
document.write(Math.max());//-Infinity

13、min() 方法可返回指定的數(shù)字中帶有最低值的數(shù)字。

Math.min(x,y);x為0或多個(gè)值。在 ECMASCript v3 之前,該方法只有兩個(gè)參數(shù)。
返回值:
參數(shù)中最小的值。
如果沒有參數(shù),則返回 Infinity。
如果有某個(gè)參數(shù)為 NaN,或是不能轉(zhuǎn)換成數(shù)字的非數(shù)字值,則返回 NaN。
和max()方法使用類似

14、pow() 方法可返回 x 的 y 次冪的值。

Math.pow(x,y);//
x 必需。底數(shù)。必須是數(shù)字。
y 必需。冪數(shù)。必須是數(shù)字。
返回值:
如果結(jié)果是虛數(shù)或負(fù)數(shù),則該方法將返回 NaN。如果由于指數(shù)過大而引起浮點(diǎn)溢出,則該方法將返回 Infinity。
如下例:

復(fù)制代碼 代碼如下:
document.write(Math.pow()+'<br>');
document.write(Math.pow(2)+'<br>');
document.write(Math.pow(2,2)+'<br>');
document.write(Math.pow(2,2,2)+'<br>');
document.write(Math.pow('M',2)+'<br>');

輸出:

NaN
NaN
4
4
NaN

15、random() 方法可返回介于 0 ~ 1 之間的一個(gè)隨機(jī)數(shù)。

Math.random();//無參
返回:
0.0 ~ 1.0 之間的一個(gè)偽隨機(jī)數(shù)。
何為偽隨機(jī)數(shù)?
真正意義的隨機(jī)數(shù)是某次隨機(jī)事件產(chǎn)生的結(jié)果,經(jīng)過無數(shù)次后表現(xiàn)為呈現(xiàn)某種概率論,它是不可預(yù)測(cè)的
而偽隨機(jī)數(shù)是根據(jù)偽隨機(jī)算法實(shí)現(xiàn)的,它是采用了一種模擬隨機(jī)的算法,因此被稱為偽隨機(jī)數(shù)

復(fù)制代碼 代碼如下:
document.write(Math.random())

0.12645312909485157

16、round() 方法可把一個(gè)數(shù)字舍入為最接近的整數(shù)。

Math.round(x),x 必需。必須是數(shù)字。
對(duì)于 0.5,該方法將進(jìn)行上舍入。
例如,3.5 將舍入為 4,而 -3.5 將舍入為 -3。
其實(shí)就感覺此方法是用ceil()和floor()方法結(jié)合實(shí)現(xiàn)的

復(fù)制代碼 代碼如下:
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

17、sin() 方法可返回一個(gè)數(shù)字的正弦。

Math.sin(x),x 必需。一個(gè)以弧度表示的角。將角度乘以 0.017453293 (2PI/360)即可轉(zhuǎn)換為弧度。
返回值:
參數(shù) x 的正弦值。返回值在 -1.0 到 1.0 之間。

復(fù)制代碼 代碼如下:
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

18、sqrt() 方法可返回一個(gè)數(shù)的平方根。

Math.sqrt(x);//x 必需,必須是大于等于 0 的數(shù)。
返回值:
參數(shù) x 的平方根。如果 x 小于 0,則返回 NaN。
它相當(dāng)于Math.pow(x,0.5);

19、tan() 方法可返回一個(gè)表示某個(gè)角的正切的數(shù)字。

Math.tan(x),//x 必需。一個(gè)以弧度表示的角。將角度乘以 0.017453293 (2PI/360)即可轉(zhuǎn)換為弧度。

查看更多JavaScript的語法,大家可以關(guān)注:《JavaScript 參考教程》、《JavaScript代碼風(fēng)格指南》,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論