🔢Number.EPSILON
🚧 under construction
JS ⟩ value ⟩ primitive ⟩ number ⟩ special ⟩ Number.EPSILON
Number.EPSILON = 2¯⁵² ≈ 2.2 * 10¯¹⁶ (就是在 2⁰ ~ 2¹ 這個區間內,每個可以表示的數字之間的間隔)
const {log} = console;
/*
┌─────────────────────────────────────┐
│ Number.EPSILON = 2¯⁵² ≈ 2.2 * 10¯¹⁶ │
└─────────────────────────────────────┘
0.1 + 0.2 === 0.3 ?
2¯⁴↴ ┌┐ (11 進位?)
110011001100110011001100110011001100110011001100110011.... (math)
11001100110011001100110011001100110011001100110011010 = 0.1 (JS)
╰──────────────────── 52-bit ──────────────────────╯
2¯³↴ ┌┐ (11 進位?)
110011001100110011001100110011001100110011001100110011.... (math)
11001100110011001100110011001100110011001100110011010 = 0.2 (JS)
╰──────────────────── 52-bit ──────────────────────╯
2¯³↴11001100110011001100110011001100110011001100110011010 = 0.1 (JS)
+ 11001100110011001100110011001100110011001100110011010 = 0.2 (JS)
─────────────────────────────────────────────────────────────────────
┌┐ (11 進位?)
1001100110011001100110011001100110011001100110011001110
10011001100110011001100110011001100110011001100110100 = 0.1 + 0.2
─────────────────────────────────────────────────────────────────────
10011001100110011001100110011001100110011001100110011 = 0.3 (JS)
⇧╰──────────────────── 52-bit ──────────────────────╯
2¯² ↳ 2¯⁵⁴
(0.1 + 0.2) - 0.3 = 1 * 2¯⁵⁴ = 5.551115123125783e-17
*/
// isEqual(a,b)
function isEqual(a, b){
// |a-b| < ε
return Math.abs(a - b) < Number.EPSILON;
}
// number.isEqual(a)
Number.prototype.isEqual = function(n){
return isEqual(this, n);
}
// --------------- log ---------------
[
0.1 + 0.2 === 0.3, // false❗️
(0.1 + 0.2) - 0.3, // 5.551115123125783e-17
isEqual(0.1 + 0.2, 0.3), // true
(0.1 + 0.2).isEqual(0.3), // true
].forEach(x => log(x))
const {log} = console;
/*
┌─────────────────────────────────────┐
│ Number.EPSILON = 2¯⁵² ≈ 2.2 * 10¯¹⁶ │
└─────────────────────────────────────┘
2.5 < 2.5 + ε ?
2¯⁵¹
2¹↴ ⇩
10100000000000000000000000000000000000000000000000000 = 2.5 (JS)
+ 1 = ε
─────────────────────────────────────────────────────────────────────
┌┐ (01, 1 truncated )
101000000000000000000000000000000000000000000000000001
= 10100000000000000000000000000000000000000000000000000 = 2.5 (JS)
╰──────────────────── 52-bit ──────────────────────╯
⭐️ Conclusion ❌: 2.5 === 2.5 + ε
1.5 < 1.5 + ε ?
2¯⁵²
2⁰↴ ⇩
11000000000000000000000000000000000000000000000000000 = 1.5 (JS)
+ 1 = ε
─────────────────────────────────────────────────────────────────────
= 11000000000000000000000000000000000000000000000000001 = 1.5 + ε (JS)
╰──────────────────── 52-bit ──────────────────────╯
⭐️ Conclusion ✅: 1.5 < 1.5 + ε
*/
// --------------- log ---------------
const ε = Number.EPSILON;
[
2.5 < 2.5 + ε, // false❗️
1.5 < 1.5 + ε, // true
].forEach(x => log(x))
replit - JS Number
replit - JS: Number.EPSILON
replit - 2.5 < 2.5 + Number.EPSILON ?
number.toString(radix) - radix: 2 ~ 36
Last updated