<style>
/* make code examples suck less */
pre code {
margin: 0 !important;
font-size: 1.25em !important;
}
pre {
padding: 0 !important;
text-align: left !important;
}
.align-left {
text-align: left !important;
}
.small-li li {
font-size: 0.85em !important;
}
</style>
# Decimal
## Stage 1 Update
### Caio Lima & Jesse Alama
#### Igalia, in collaboration with Bloomberg
---
### Problem statement
Humans think of numbers as based on powers of 10, and carry over those assumptions when writing code that does arithmetic.
The float64 semantics differ from that, causing math errors that cause bugs and leak to the users. We need to provide developers an easily reachable tool to use when these problems arise.
Furthermore, other languages already provide such solution, and JavaScript is not able to fully interop with them.
<!--
# Motivation
There ought to be a kind of number in JS that work like the numbers we learned in elementary school.
Doing even simple calculations can expose the ugly underbelly of binary floats. `0.1 + 0.2 !== 0.3` is just the tip of the iceberg.#
-->
---
### Real world Evidence
<figure>
<img src="https://notes.igalia.com/uploads/77e8cc93-a2cd-465b-afb6-4a3306d783c8.png" alt="StackOverflow - Is floating-point math broken?">
<figcaption><a href="https://stackoverflow.com/questions/588004/is-floating-point-math-broken">StackOverflow - Is floating-point math broken?</a></figcaption>
</figure>
---
### Real world Evidence
<figure>
<img src="https://notes.igalia.com/uploads/d2bb6dd7-58de-4f6e-9bd3-33b322835a86.png" alt="Floating-Point Questions Are Endless on stackoverflow.com">
<figcaption><a href="https://www.exploringbinary.com/floating-point-questions-are-endless-on-stackoverflow-com/">Floating-Point Questions Are Endless on stackoverflow.com</a></figcaption>
</figure>
---
### Real world Evidence
<figure>
<img src="https://notes.igalia.com/uploads/274e3f4d-494a-42bb-8538-b341587ea933.png" alt="Screenshot from Nicolò's account software">
<figcaption>Screenshot from Nicolò's account software</figcaption>
</figure>
---
## Prior art
Many languages have decimals alongside binary floating-point numbers: Python, Java, C#, Swift, Ruby
Even C and C++ have proposals to add decimals to the language.
---
## Design choices
* IEEE-754 Decimal128. Fixed bit-width decimals supporting up to 34 significant digits. (Used by several other languages.)
* API: New standard library object, `Decimal`, supporting basic arithmetic & comparison
* "Normalize on the way out": `.toString` and comparisons use the MV of a Decimal128
* Implementations may use unnormalized values
This is the result of many iterations through plenary!
<!--
---
## State of play
The data model and API were crafted after many iterations in and out of plenary. We contend:
i. the problem to be solved does affect JS programmers,
ii. it is reasonable to add decimals to the language,
iii. the current design addresses the use cases…
iv. …while remaining performant, optimizable, and open to further extension.
-->
---
## State of play, _elsewhere_
Decimal has allies & friends. There appears to be appetite for number stuff these days.
The [`Amount`](https://github.com/tc39/proposal-amount) and [`intl-keep-trailing-zeros`](https://github.com/tc39/proposal-intl-keep-trailing-zeros) proposals allow Decimal to sidestep a tricky canonicalization issue (`1.2` vs. `1.20`)
[`Math.fma`](https://github.com/waldemarhorwat/fma) and [`sum-precise`](https://github.com/tc39/proposal-math-sum) are philosophically related: They push JS to do better at math.
But the problem of correct decimal arithmetic remains unsolved.
---
## Do we want Decimal?
- Are the use cases compelling (enough)?
- Is the data model/API OK?
- Is this too hard to optimize?
- Does the Object-based API solve the problem statement?
---