|
| 1 | +# Behold, A New Operator `====` |
| 2 | + |
| 3 | +## About |
| 4 | + |
| 5 | +When `0.1 + 0.2` in JavaScript yields `0.30000000000000004`, it highlights a **common aspect of computer arithmetic**, |
| 6 | +not a bug. This occurs because JavaScript, like most languages, uses the **IEEE 754 standard** for floating-point numbers, |
| 7 | +which relies on **binary (base-2) representation**. |
| 8 | + |
| 9 | +**Decimal fractions** like 0.1 and 0.2 **cannot be perfectly represented as finite binary fractions**; they become infinitely repeating. |
| 10 | +When these are stored in a **finite number of bits**, a **tiny truncation error** is introduced. This **slight imprecision** |
| 11 | +in each number **accumulates during addition**, resulting in a sum that's marginally off from the **exact mathematical total**. |
| 12 | + |
| 13 | +### Solutions |
| 14 | + |
| 15 | +For scenarios requiring **precise decimal arithmetic** (e.g., financial applications), direct floating-point calculations |
| 16 | +can be problematic. Consider these approaches: |
| 17 | + |
| 18 | +1. **Rounding:** Use `toFixed()` to round results to a desired decimal precision. Remember to convert the string output |
| 19 | +back to a number if needed. |
| 20 | + ```javascript |
| 21 | + parseFloat((0.1 + 0.2).toFixed(1)); // 0.3 |
| 22 | + ``` |
| 23 | +2. **Integer Arithmetic:** **Scale numbers to integers** before calculations and then scale the final result back down. |
| 24 | + ```javascript |
| 25 | + (0.1 * 10 + 0.2 * 10) / 10; // 0.3 |
| 26 | + ``` |
| 27 | +3. **Specialized Libraries:** For **advanced precision**, utilize libraries like `Big.js` or `Decimal.js`. |
| 28 | + |
| 29 | +This behavior is a **fundamental consequence of binary representation in computing**, not a flaw in JavaScript, |
| 30 | +and **understanding it is key** to handling numerical precision effectively. |
| 31 | + |
| 32 | +## Introducing the `====` Operator: For When `===` Just Isn't Enough |
| 33 | + |
| 34 | +Sometimes, **strict equality** (`===`) feels like it's trying *too hard* to be precise, yet still falls short of our |
| 35 | +deepest desires for perfect, unyielding truth. For those moments, when you need to compare not just value and type, |
| 36 | +but also the very **essence** of existence, I propose the **Quadruple Equals Operator (`====`)**! |
| 37 | + |
| 38 | +What does `====` do? Well, it's simple: |
| 39 | + |
| 40 | +* `0.1 + 0.2 ==== 0.3` would _(theoretically)_ return `true`. Because in a world where `====` exists, numbers just *know* what they're supposed to be. |
| 41 | +* `"hello" ==== "hello"` would, naturally, be `true`. |
| 42 | +* `[] ==== []` might still be `false`, because even `====` respects the **existential uniqueness** of array instances. But I am working on it. ¯\\\_(ツ)\_/¯ |
| 43 | +* The `====` operator is so powerful, it can detect **deep existential equality**, ensuring that not only values and types match, |
| 44 | +but also their **historical context**, their **developer's intent**, and their **cosmic vibrational frequency**. |
| 45 | + |
| 46 | +Alas, `====` is a mere dream, a **mythical beast** in the JavaScript ecosystem, born from the frustration of floating-point arithmetic. |
| 47 | +For now, we'll have to stick to our practical solutions. But one can dream of a world where `0.1 + 0.2 ==== 0.3` just *makes sense*. |
0 commit comments