---
description: 🚧 under construction
---

# Number.EPSILON

[JS](../../../..) ⟩ [value](../../..) ⟩ [primitive](../..) ⟩ [number](..) ⟩ [special]() ⟩ Number.EPSILON

{% hint style="success" %}
[Number.EPSILON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON) = 2¯⁵² ≈ 2.2 \* 10¯¹⁶ \
(就是在 2⁰ \~ 2¹ 這個區間內，每個<mark style="color:orange;">**可以表示的**</mark>數字之間的<mark style="color:yellow;">**間隔**</mark>)
{% endhint %}

{% tabs %}
{% tab title="0.1+0.2" %}
```javascript
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))
```
{% endtab %}

{% tab title="2.5+ε" %}
```javascript
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))
```
{% endtab %}

{% tab title="💾 程式" %}
* replit - [JS Number](https://replit.com/@pegasusroe/JS-Number#script.js)
* replit - [JS: Number.EPSILON](https://replit.com/@pegasusroe/JS-NumberEPSILON)
* replit - [2.5 < 2.5 + Number.EPSILON ?](https://replit.com/@pegasusroe/25-less-25-NumberEPSILON#index.js)
{% endtab %}

{% tab title="📘 手冊" %}
* [Number.EPSILON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON)
* number.[toString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString)(radix) - radix: 2 \~ 36
{% endtab %}

{% tab title="🗣 討論" %}
* [Why is "(2.5 < 2.5 + Number.EPSILON)" false in JavaScript?](https://stackoverflow.com/questions/56437482/why-is-2-5-2-5-number-epsilon-false-in-javascript)
{% endtab %}

{% tab title="👥 相關" %}
* [n.isequal](../number+ext/n.isequal "mention")
{% endtab %}
{% endtabs %}
