Skip to main content

Decimals

As we saw in the floating point article, they're fast but not accurate. This is due to binary division. Programming languages also have a Decimal data type.

How decimals work?

Programming languages fully control the Decimal data type. It first scales the numbers so both operands are at the same level. Then it does the arithmetic. Then it scales the result down.

This is why Decimal is accurate. It's used where accuracy matters, such as a balance. Still, it's slow.

10.5+0.1234VA=105,scale=1VB=1234,scale=4VA(new)=105ร—10ร—10ร—10VA(new)=105,000105,000+1,234=106,234Finalย Result=106,23410410.6234\begin{aligned} 10.5 + 0.1234 \\ V_A = 105, scale = 1 \\ V_B = 1234, scale = 4 \\ V_A (\text{new}) = 105 \times 10 \times 10 \times 10 \\ V_A (\text{new}) = 105,000 \\ 105,000 + 1,234 = 106,234 \\ \text{Final Result} = \frac{106,234}{10^4} \\ \mathbf{10.6234} \end{aligned}