# (\~\~) vs. Math.trunc()

x[JS](https://lochiwei.gitbook.io/web/js) ⟩ [statement](https://lochiwei.gitbook.io/web/js/grammar/statement) ⟩ [expression](https://lochiwei.gitbook.io/web/js/grammar/statement/expr) ⟩ [operator](https://lochiwei.gitbook.io/web/js/grammar/op) ⟩ double tilde (\~\~)

{% hint style="success" %} <mark style="color:orange;">**nearest**</mark>**&#x20;**<mark style="color:yellow;">**integer**</mark>**&#x20;**<mark style="color:orange;">**towards**</mark> <mark style="color:red;">**0**</mark><mark style="color:yellow;">**.**</mark>

```javascript
// ⭐️ ~~(x) : nearest integer towards 0
//    (almost) same as Math.trunc() = sign(x) * [|x|]
~~(7.8),            //  7
~~(-5.6),           // -5
Math.trunc(7.8),    //  7
Math.trunc(-5.6),   // -5

~~M,                // ❗ 0 (overflow: ~~(x+M) = ~~x)
Math.trunc(M),      // ⭐️ M (Math.trunc() has wider range)
```

:point\_right: [](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not "mention") <mark style="color:yellow;">**|**</mark> :star2: [2s-complement](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/2s-complement "mention")
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %} <mark style="color:purple;">**\~\~**</mark>**&#x20;**<mark style="color:yellow;">**vs.**</mark>**&#x20;**<mark style="color:purple;">**Math.trunc()**</mark>：the <mark style="color:red;">**difference**</mark>

* [Math.trunc(x)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc)&#x20;
  * will try to convert x with [**"prefer-number" conversion**](https://lochiwei.gitbook.io/web/js/val/obj/convert/obj-to-prim), if it's [nan](https://lochiwei.gitbook.io/web/js/val/prim/num/special/nan "mention"), the result <mark style="color:yellow;">**remains**</mark> <mark style="color:blue;">**NaN**</mark>.
  * has wider range. (see example above)
* <mark style="color:purple;">**bitwise not (\~)**</mark>\ <mark style="color:yellow;">**the following**</mark>**&#x20;**<mark style="color:red;">**all**</mark>**&#x20;**<mark style="color:yellow;">**converted to**</mark>**&#x20;**<mark style="color:red;">**0**</mark> before applying <mark style="color:purple;">**bitwise not (\~)**</mark> operator.
  * [null](https://lochiwei.gitbook.io/web/js/val/prim/null "mention") <mark style="color:yellow;">**/**</mark> [undefined](https://lochiwei.gitbook.io/web/js/val/prim/undefined "mention") <mark style="color:yellow;">**/**</mark> <mark style="color:red;">**false**</mark> <mark style="color:yellow;">**/**</mark> <mark style="color:orange;">**`""`**</mark> (<mark style="color:yellow;">**empty string**</mark>) <mark style="color:yellow;">**/**</mark> [nan](https://lochiwei.gitbook.io/web/js/val/prim/num/special/nan "mention") <mark style="color:yellow;">**/**</mark> [infinity](https://lochiwei.gitbook.io/web/js/val/prim/num/special/infinity "mention")
  * <mark style="color:yellow;">**non-numeric**</mark> [str](https://lochiwei.gitbook.io/web/js/val/prim/str "mention") <mark style="color:yellow;">**/**</mark> <mark style="color:yellow;">**non-numeric**</mark> [arr](https://lochiwei.gitbook.io/web/js/val/builtin/arr "mention")&#x20;
  * <mark style="color:yellow;">**other**</mark> [obj](https://lochiwei.gitbook.io/web/js/val/obj "mention") <mark style="color:yellow;">**(**</mark><mark style="color:red;">**except**</mark> [date](https://lochiwei.gitbook.io/web/js/val/builtin/date "mention")<mark style="color:yellow;">**)**</mark>
    {% endhint %}
    {% endtab %}

{% tab title="💈範例" %}
{% embed url="<https://codesandbox.io/embed/compare-math-trunc-bi2gh?fontsize=14&hidenavigation=1&theme=dark>" %}
"\~\~" vs. Math.trunc()
{% endembed %}

{% hint style="warning" %}
注意：使用 [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 將函數轉換成字串時，會轉成 "**undefined**"❗️
{% endhint %}
{% endtab %}

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

* [..](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise "mention") ⟩ [](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not "mention") - invert bits.
* [unary](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/unary "mention") ⟩ unary plus (+)
* [unary](https://lochiwei.gitbook.io/web/js/grammar/op/logical/unary "mention") ⟩ logical not (!)
  {% endtab %}

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

* [ ] CodeWars ⟩ [Adding Big Numbers](https://www.codewars.com/kata/525f4206b73515bffb000b21)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Bitwise NOT (\~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
* [Math.trunc()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc) - same as <mark style="color:purple;">**`~~`**</mark> (with wider range)
  {% endtab %}

{% tab title="🗣 討論" %}

* SOF ⟩ [What does \~\~ ("double tilde") do in Javascript?](https://stackoverflow.com/a/4055675)
  {% endtab %}
  {% endtabs %}
