Math.LN2 表示2 的自然對數,即 ln(2) 或 logₑ(2)。其近似值為 0.6931471805599453。
Math.LN2
ln(2) = logₑ(2)log₂(e) = 1 / ln(2) = 1 / Math.LN2 = Math.LOG2Ee^(ln 2) = 2 → Math.exp(Math.LN2) === 2console.log(Math.LN2); // 0.6931471805599453
console.log(Math.log(2)); // 0.6931471805599453 (相同)
console.log(Math.exp(Math.LN2)); // 2
console.log(1 / Math.LN2); // 1.4426950408889634 (=== Math.LOG2E)
console.log(Math.log2(Math.E)); // 1.4426950408889634 (ES2015 推薦)
放射性衰變、藥物代謝、RC 電路放電等指數衰減過程:
// 半衰期公式:N(t) = N₀ * (1/2)^(t / T_half)
// 等價:N(t) = N₀ * e^(-λt),其中 λ = ln(2) / T_half
function decayRemaining(initial, halfLife, time) {
const lambda = Math.LN2 / halfLife; // 衰變常數
return initial * Math.exp(-lambda * time);
}
// 碳-14 半衰期 5730 年
console.log(decayRemaining(100, 5730, 5730)); // 50 (一半衰期後剩 50%)
console.log(decayRemaining(100, 5730, 11460)); // 25 (兩個半衰期)
// 藥物半衰期:咖啡因 ~5 小時
console.log(decayRemaining(200, 5, 10)); // 50mg (10小時後)
// 1 bit = ln(2) nat ≈ 0.693 nat
function bitsToNats(bits) {
return bits * Math.LN2;
}
function natsToBits(nats) {
return nats / Math.LN2; // 或 nats * Math.LOG2E
}
console.log(bitsToNats(1)); // 0.693... nat
console.log(natsToBits(1)); // 1.442... bit
// n 個元素二分查找最多步數 = log₂(n)
// 用自然對數計算:log₂(n) = ln(n) / ln(2) = ln(n) * Math.LOG2E
function maxBinarySearchSteps(n) {
return Math.ceil(Math.log(n) * Math.LOG2E);
}
console.log(maxBinarySearchSteps(1000)); // 10
console.log(maxBinarySearchSteps(1e6)); // 20
| 常量 | 含義 | 值 | 倒數 |
|---|---|---|---|
Math.LN2 | ln(2) | 0.6931471805599453 | Math.LOG2E |
Math.LN10 | ln(10) | 2.302585092994046 | Math.LOG10E |
Math.LOG2E | log₂(e) | 1.4426950408889634 | Math.LN2 |
Math.LOG10E | log₁₀(e) | 0.4342944819032518 | Math.LN10 |
Math.log(2) 或 Math.LN2(語義更清晰)Math.log2(x)** (ES2015),而非 Math.log(x) * Math.LOG2EMath.LN2 語義最明確Math.LN2 連接了指數衰減物理模型、信息論單位轉換、算法複雜度分析。掌握其與 Math.LOG2E 的倒數關係,能在科學計算與工程實踐中靈活切換對數底數。