Core Object – 數學函式物件(Math Object)

數學函式物件(Math Object)

Math 物件 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.constant
Math.function()

constant:數學常數 function():數學函式

屬性 E、LN2、LN10、LOG2E、LOG10E、PI、SQRT1_2、SQRT2
方法 abs、acos、asin、atan、atan2、ceil、cos、exp、floor、log、max、min、pow、
random、round、sin、sqrt、tan

Math 物件擁有數學常數(屬性)和函式(方法),想使用這個物件的話,並不需要使用 new 來建立物件的實體(副本)。

abs 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
PI 屬性 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.abs(x)
Math.PI

x:值或算數運算式

abs 是用來傳回絕對值。

PI 是用來傳回圓週率的值。

//範例
document.write(Math.abs(1 - 4));				//結果為 3
document.write(Math.PI);						//結果為 3.141592653589793
sin cos tan asin acos atan atan2 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.sin(x) 傳回正弦值 Math.asin(x) 傳回反正弦值
Math.cos(x) 傳回餘弦值 Math.acos(x) 傳回反餘弦值
Math.tan(x) 傳回正切值 Math.atan(x) 傳回反正切值
Math.atan2(x,y) 從(x,y)座標傳回夾角    

用來做三角函數的處理。

//範例
var a = 1;
	document.write(Math.sin(a));				//結果為 0.8414709848078965
	document.write(Math.cos(a));				//結果為 0.5403023058681398
	document.write(Math.tan(a));				//結果為 1.5574077246549023
	document.write(Math.asin(a));				//結果為 1.5707963267948966
	document.write(Math.acos(a));				//結果為 0
	document.write(Math.atan(a));				//結果為 0.7853981633974483
	document.write(Math.atan(a,1));				//結果為 0.7853981633974483
ceil floor round 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.ceil(x) 傳回無條件進入後的結果
Math.floor(x) 傳回無條件捨去後的結果
Math.round(x) 傳回四捨五入後的結果

用來將小數整數化。

//範例
var a = 5.67;var b = 5.43;
	document.write(Math.ceil(a));				//結果為 6
	document.write(Math.floor(a));				//結果為 5
	document.write(Math.round(b));				//結果為 5
log 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
LOG2E LOG10E 屬性 兼容性:IE3+、NN2+、Moz1+、Safari1+
E LN2 LN10 屬性 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.log(x) 傳回自然對數的值 Math.E 傳回自然對數的底數 e(Euler常數)
Math.LOG2E 傳回以 2 為底數時 e 的對數值 Math.LN2 傳回 2 的自然對數值
Math.LOG10E 傳回以 10 為底數時 e 的對數值 Math.LN10 傳回 10 的自然對數值

log、LOG2E、LOG10E 是用來傳回對數值。E、LN2、LN10 是用來傳回自然對數的值。

//範例
document.write(Math.log(2));					//結果為 0.6931471805599453
document.write(Math.LOG2E);						//結果為 1.4426950408889634
document.write(Math.LOG10E);					//結果為 0.4342944819032518
document.write(Math.E);							//結果為 2.718281828459045( Euler 常數)
document.write(Math.LN2);						//結果為 0.6931471805599453
document.write(Math.LN10);						//結果為 2.302585092994046
pow exp 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.pow(x, y) x:底數 y:指數
Math.exp(value) value:值或算數運算式

pow 是用來傳回乘冪值。

exp 是用來傳回 e 的乘冪值。

//範例
document.write(Math.pow(2,3));					//結果為 8( 2 的 3 次方)
document.write(Math.exp(2));					//結果為 7.38905609893065
sqrt 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
SQRT1_2 SQRT2 屬性 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.sqrt(x) 傳回平方根的值
Math.SQRT1_2 傳回 1/2 的平方根
Math.SQRT2 傳回 2 的平方根

這些是用來傳回平方根的值。

//範例
document.write(Math.sqrt(4));					//結果為 2
document.write(Math.SQRT1_2);					//結果為 0.7071067811865476
document.write(Math.SQRT2);						//結果為 1.4142135623730951
max min 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.max(Val1,Val2) 傳回兩個值當中較大的值
Math.min(Val1,Val2) 傳回兩個值當中較小的值
Val1,Val2:值或算數運算式

用來傳回兩個比較後的值。

//範例
document.write(Math.max(1,5));					//結果為 5
document.write(Math.min(7,6));					//結果為 6
random 方法 兼容性:IE3+、NN2+、Moz1+、Safari1+
Math.random()

用來傳回 0 以上未滿 1 的亂數值。

//範例
document.write(Math.random());					//結果為 0 以上未滿 1 的亂數
document.write(Math.round(Math.random() * 10));	//結果為 0 以上(含0)未滿 11 的整數亂數

文章分類

文章標籤