> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/num/number+ext/n.isequal.md).

# n.isEqual()

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ [number](/web/js/val/prim/num.md) ⟩ [Number+ext](/web/js/val/prim/num/number+ext.md) ⟩ num.isEqual()

{% hint style="success" %}
test if <mark style="color:yellow;">**two numbers**</mark> are "<mark style="color:red;">**essentially**</mark>" <mark style="color:yellow;">**equal**</mark>.
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit - [JS Number](https://replit.com/@pegasusroe/JS-Number#script.js)

```javascript
// ⭐️ areEqualNumbers(x, y {threshold})
function areEqualNumbers(x, y, {threshold = Number.EPSILON}={}) { 
    return Math.abs(x - y) < threshold; 
}

// ⭐️ a.isEqual(b, {threshold})
Number.prototype.isEqualTo = function(n, {threshold = Number.EPSILON}={}){
    return areEqualNumbers(this, n, {threshold});
};
```

💈範例：

```javascript
const eps = Number.EPSILON;         // 2^(-52) (very small)
const a = Math.pow(10, 300);        // 10^300  (very big)

[
    Number.EPSILON,                 //  2.220446049250313e-16 (2^(-52))
    
    (0.1 + 0.2) - 0.3,              //  5.551115123125783e-17
    0.3 - (0.1 + 0.2),              // -5.551115123125783e-17
    0.1 + 0.2 === 0.3,              // ❗️ false (should be true)

    1.5 + eps > 1.5,                // true
    2.5 + eps === 2.5,              // ❗️ true  (should be false)

    areEqualNumbers(0.1+0.2, 0.3),  // true
    (0.1 + 0.2).isEqualTo(0.3),     // true

    // big numbers
    (a + 1).isEqualTo(a),           // ❗️ true  (should be false)
    (a + 1) - a,                    // ❗️ 0️     (should be 1)   

    // test threshold
    (0.1).isEqualTo(0.11, {threshold: 0.05}),    // true

].forEach(x => log(x));
```

{% endtab %}

{% tab title="⬇️ 應用" %}

* [Vector](/web/appendix/custom/class/vector.md) - u.isEqualTo(v)：test equality of two vectors.
  {% endtab %}

{% tab title="📗 參考" %}

* JavaScript Data Structures and Algorithms (p.21)
  {% endtab %}

{% tab title="👥 相關" %}

* [Number.EPSILON](/web/js/val/prim/num/special/number.epsilon.md)
* [sloppy equality (==)](/web/js/grammar/op/relational/equal/sloppy-equality.md)
* [strict equality (===)](/web/js/grammar/op/relational/equal/strict-equality.md)
  {% endtab %}
  {% endtabs %}
