DIV CSS 佈局教程網

Math 物件詳解:常數、取整、三角函數與隨機數
編輯:JavaScript基礎知識     

Math 是一個內建的運算工具集,裡頭放滿數學用的常數與方法。它不需要 new,直接用即可。

一、常數

名稱意義近似值
E自然對數底數2.718
LN22 的自然對數0.693
LN1010 的自然對數2.302
LOG2E以 2 為底的 e 的對數1.443
LOG10E以 10 為底的 e 的對數0.434
PI圓周率3.14159
SQRT1_22 的平方根的倒數0.707
SQRT22 的平方根1.414
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);

輸出為:

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

二、取絕對值與取整

abs(x) 回傳絕對值,整數小數皆可。

document.write(Math.abs(-2.77)); // 2.77

ceil(x) 向上取整,即大於等於 x 且最接近的整數。floor(x) 向下取整。round(x) 則是四捨五入到最接近的整數,遇到 0.5 一律向上。

document.write(Math.ceil(0.60)  + "<br />"); // 1
document.write(Math.ceil(0.40)  + "<br />"); // 1
document.write(Math.ceil(5)     + "<br />"); // 5
document.write(Math.ceil(5.1)   + "<br />"); // 6
document.write(Math.ceil(-5.1)  + "<br />"); // -5
document.write(Math.ceil(-5.9));              // -5
document.write(Math.floor(0.60) + "<br />"); // 0
document.write(Math.floor(0.40) + "<br />"); // 0
document.write(Math.floor(5)    + "<br />"); // 5
document.write(Math.floor(5.1)  + "<br />"); // 5
document.write(Math.floor(-5.1) + "<br />"); // -6
document.write(Math.floor(-5.9));             // -6
document.write(Math.round(0.60)  + "<br />"); // 1
document.write(Math.round(0.50)  + "<br />"); // 1
document.write(Math.round(0.49)  + "<br />"); // 0
document.write(Math.round(-4.40) + "<br />"); // -4
document.write(Math.round(-4.60));             // -5

負數的行為最容易搞錯:ceil 往大的方向、floor 往小的方向,round 則是加 0.5 後取 floor,所以 -3.5 會得到 -3。

三、三角函數

acos(x)、asin(x) 的參數須介於 -1.0 到 1.0 之間,超出範圍回傳 NaN;atan(x) 回傳 -PI/2 到 PI/2 之間的弧度;atan2(y, x) 回傳從 X 軸正向逆時針旋轉到點 (x, y) 所經過的角度,範圍是 -PI 到 PI。

cos(x)、sin(x)、tan(x) 的參數是弧度而非角度,換算方式是「角度 × 0.017453293」(即 2π/360)。回傳值落在 -1.0 到 1.0 之間。

document.write(Math.cos(Math.PI));     // -1
document.write(Math.cos(Math.PI / 2)); // 6.123233995736766e-17
document.write(Math.cos(Math.PI / 3)); // 0.5000000000000001

為什麼 cos(π/2) 不是 0、cos(π/3) 不是恰好 0.5?這是浮點數表示的必然結果。π 本身無法被二進位浮點精確表示,寄存器也不可能存下所有實數,運算尾端出現極小誤差屬正常現象。若用於金額或需要精確比較的場合,應先取適當精度再比對,而不是直接用 === 判斷。

四、指數與對數

exp(x) 回傳 e 的 x 次方。

document.write(Math.exp(1)); // 2.718281828459045

log(x) 回傳自然對數,參數須大於 0;等於 0 時回傳 -Infinity,小於 0 時回傳 NaN。

document.write(Math.log(2.7183) + "<br>"); // 1.0000066849139877
document.write(Math.log(2)      + "<br>"); // 0.6931471805599453
document.write(Math.log(1)      + "<br>"); // 0
document.write(Math.log(0)      + "<br>"); // -Infinity
document.write(Math.log(-1));              // NaN

pow(x, y) 回傳 x 的 y 次方。結果若為虛數或負數則回傳 NaN,指數過大造成浮點溢位則回傳 Infinity。

document.write(Math.pow()      + '<br>'); // NaN
document.write(Math.pow(2)     + '<br>'); // NaN
document.write(Math.pow(2, 2)  + '<br>'); // 4
document.write(Math.pow(2,2,2) + '<br>'); // 4,第三個參數被忽略
document.write(Math.pow('M', 2));        // NaN

sqrt(x) 回傳平方根,等同 Math.pow(x, 0.5);x 小於 0 時回傳 NaN。

五、最大值、最小值與隨機數

max 與 min 可接受多個參數(ES3 之前只支援兩個)。沒有參數時分別回傳 -Infinity 與 Infinity;任一參數為 NaN 或無法轉為數字時回傳 NaN。

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

random() 回傳 0 到 1 之間的偽隨機數。

document.write(Math.random()); // 例如 0.12645312909485157

之所以稱「偽」隨機,是因為它由確定性演算法產生,給定相同種子便會得到相同序列,並非真正不可預測。實務上要取某個區間的整數,常見寫法是 Math.floor(Math.random() * (max - min + 1)) + min;若用於加密或/security 相關場景,則應改用 crypto.getRandomValues()。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved