紀錄工作經驗、相關知識,解決技術相關問題。

Javascript

JS Math 物件:常數、常用方法與弧度轉換

JS Math 物件:常數、常用方法與弧度轉換 特色圖片

JS Math 指的是 JavaScript 內建的 Math 物件:它是一個命名空間物件,裡面放著數學常數與數學函式,例如圓周率 Math.PI、絕對值 Math.abs()、開根號 Math.sqrt()。

白話一點,Math 就像一把附在語言裡的計算機:不用另外建立,直接拿來按。

Math 的基本特性

MDN 說明,Math 與大多數全域物件不同,它不是建構函式:不能用 new,也不能當函式呼叫,所有屬性與方法都是靜態的。

console.log(typeof Math);                         // object
console.log(Object.prototype.toString.call(Math)); // [object Math]

try {
  new Math();
} catch (e) {
  console.log(e.name); // TypeError
}

try {
  Math();
} catch (e) {
  console.log(e.name); // TypeError
}

Math 只處理 Number 型別,不能用在 BigInt 上:

try {
  Math.abs(10n);
} catch (e) {
  console.log(e.name); // TypeError
}

常數

常數說明近似值
Math.PI圓周率3.14159
Math.E歐拉數,自然對數的底2.718
Math.SQRT22 的平方根1.414
Math.SQRT1_21/2 的平方根0.707
Math.LN22 的自然對數0.693
Math.LN1010 的自然對數2.303
Math.LOG2E以 2 為底的 E 的對數1.443
Math.LOG10E以 10 為底的 E 的對數0.434
console.log(Math.PI);    // 3.141592653589793
console.log(Math.E);     // 2.718281828459045
console.log(Math.SQRT2); // 1.4142135623730951

常用方法

MDN 列出的方法很多,依用途分類如下:

用途方法
取整數Math.round()、Math.floor()、Math.ceil()、Math.trunc()
大小與符號Math.max()、Math.min()、Math.abs()、Math.sign()
次方與根號Math.pow()、Math.sqrt()、Math.cbrt()、Math.hypot()
對數與指數Math.log()、Math.log10()、Math.log2()、Math.exp()
三角函數Math.sin()、Math.cos()、Math.tan()、Math.asin()、Math.acos()、Math.atan()、Math.atan2()
雙曲函數Math.sinh()、Math.cosh()、Math.tanh()、Math.asinh()、Math.acosh()、Math.atanh()
隨機數Math.random()
console.log(Math.sqrt(16));      // 4
console.log(Math.pow(2, 10));    // 1024
console.log(Math.max(1, 3, 2));  // 3
console.log(Math.min(1, 3, 2));  // 1
console.log(Math.abs(-5));       // 5

console.log(Math.round(2.5));    // 3
console.log(Math.floor(-1.5));   // -2
console.log(Math.ceil(1.2));     // 2
console.log(Math.trunc(-1.9));   // -1
console.log(Math.sign(-3));      // -1

console.log(Math.hypot(3, 4));   // 5
console.log(Math.cbrt(27));      // 3
console.log(Math.log10(1000));   // 3
console.log(Math.log2(8));       // 3
console.log(Math.log(Math.E));   // 1

弧度與角度轉換

MDN 說明,sin()、cos()、tan()、asin()、acos()、atan()、atan2() 這些三角函數,輸入與輸出的角度都是弧度(radian)。人習慣用度(degree),所以常需要自己準備轉換函式:

function degToRad(degrees) {
  return degrees * (Math.PI / 180);
}

function radToDeg(rad) {
  return rad / (Math.PI / 180);
}

console.log(degToRad(180));      // 3.141592653589793
console.log(radToDeg(Math.PI));  // 180
console.log(Math.sin(Math.PI / 2)); // 1
console.log(Math.cos(0));           // 1

範例:正三角形的高

MDN 的例子:邊長 100 的正三角形,高等於底的一半乘以 60 度的正切。先把 60 度轉成弧度,再交給 Math.tan():

console.log(50 * Math.tan(degToRad(60))); // 86.60254037844383

範例:產生範圍內的隨機整數

用 Math.random() 與 Math.floor() 組合:

function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

const r = random(1, 10);
console.log(r >= 1 && r <= 10); // true

注意事項

  1. 精度依實作而異:MDN 提醒,許多 Math 函式的精度是 implementation-dependent,不同瀏覽器甚至同一個引擎在不同作業系統或架構上,結果都可能不同。拿浮點數比較時,不要假設最後幾位數字完全一致。
  2. 三角函數用弧度:直接把角度(度)傳進去是常見錯誤,先轉換。
  3. 不能用在 BigInt:需要 BigInt 的運算要另外處理。

補充說明:範例輸出為 Node.js v22.20.0 的執行結果。

參考資料與延伸閱讀

發佈留言