3-5 Math類別

1. Math類別提供數學函數和數學常數兩種。

2. 數學函數使用Math.method(parameter)的語法呼叫。

3. 數學常數使用Math.constant的語法呼叫。

4. 在Math類別中,角度的單位為弧度(弳,radian)。

5. 角度的度數(degree)可以利用下面的公式來轉換:

      radian= degree * Math.PI / 180

      Math.PI為數學常數,其值為3.14159265358979

   計算sin30˚值。

val = Math. sin(30*Math.PI/180);

Math. sin為三角函數的正弦函數。

Math函數(方法)

方 法

說  明

Math.abs(x)

|x|,絕對值。

Math.acos (x)

cos-1(x),反餘弦函數,傳回數值的單位為弧度。

Math.asin(x)

sin-1(x),反正弦函數,傳回數值的單位為弧度。

Math.atan(x)

tan -1(y/x),反正切函數,傳回數值的單位為弧度。

Math.atan2(y,x)

tan-1(y/x),反正切函數,傳回數值的單位為弧度。

Math.ceil(x)

傳回大於x的最小整數。

例:Math. ceil(3.5) → 4

Math.cos(x)

cos(x),餘弦函數,x的單位為弧度。

Math.exp(x)

ex,以e為底的指數函數。

Math.floor(x)

傳回小於x的最大整數。

例:Math. floor(3.5) → 3

Math.log(x)

ln(x) ,以e為底的自然對數函數。

Math.max(x, y)

傳回x與y中較大值。

Math.min(x, y)

傳回x與y中較小值。

Math.pow(x, y)

xy,x的y次方。

Math.random( )

傳回虛擬亂數n,其中0<n<1

Math.round(x)

傳回x四捨五入後的整數。

Math.sin(x)

sin(x),正弦函數,x的單位為弧度。

Math.sqrt(x)

√x,平方根,x≧0。

Math.tan(x)

tan(x),正切函數,x的單位為弧度。

Math常數

常數

說明

Math.E

e為2.71828182845905

Math.LN10

ln10為2.30258509299405

Math.LN2

ln2為0.693147180559945

Math.LOG10E

log10 e為0.434294481903252

Math.LOG2E

log2 e為1.44269504088896

 Math.Math.PI

π為3.14159265358979

Math.SQRT1_2

1/ √2為0.707106781186548

Math.SQRT2

√2為1.4142135623731

 

 


 產生5個介於50~60之間隨機整數。

 

 

 

 

 

 

 


程式:

01

02

03

04

05

min = 50;

max = 60;

for( i =1 ; i <= 5 ; i++ ){

      trace(Math.floor(Math. random()*(max-min+1)+min));

}

解說:

行01~02:50~60之間有11個整數。

行04:max-min+1  計算兩個整數之間有多少個整數。

行04:Math.random()產生0≦n<1之間虛擬亂數。

行04:Math.random()*(max-min+1)產生50≦n<61之間虛擬亂數,此亂數為浮點數。

行04:Math.floor函數傳回小於x的最大整數。本範例為將正數中小數部份捨去,只留整數。