Cube AlgosBlog

· 6 min read · Deutsch

How do you add money when 0.1 + 0.2 is not 0.3?

Python adds 0.1 and 0.2 and gets 0.3, fifteen zeros, and a four: 0.30000000000000004. JavaScript prints the same. So how do you add money without that four? This post traces the four back in three steps: a third on paper, a tenth in halves, and the value the computer actually stores. Then comes the fix from the reel, whole cents, and where it stops working. Every output shown comes from our own run with Python 3.13.5 and Node.js 22.20.0; the amounts are made up.

Not a bug in Python

The Python documentation is clear on this point: it is in the very nature of binary floating point. It is not a bug in Python, and not a bug in your code either. According to the documentation, you will see the same kind of thing in every language that uses your hardware's floating-point arithmetic, though some may not display the difference by default. We checked JavaScript:

python3   >>> 0.1 + 0.2          0.30000000000000004node      > 0.1 + 0.2          0.30000000000000004

To see where the four comes from, it helps to start on paper.

On paper, a third never ends

Write a third as a decimal: 0.3. Better, 0.33. Better still, 0.333. The Python documentation uses exactly this example: however many digits you write down, the result is never exactly 1/3, only a closer and closer approximation.

The reason is that decimals are built from tenths, hundredths and thousandths. A third cannot be put together exactly from those pieces. Something is always left over.

In a computer, a tenth never ends

A computer does not store floating-point numbers in tenths but in base 2. Its pieces are halves, quarters, eighths, sixteenths and so on. The documentation gives an example: the binary fraction 0.101 means 1/2 + 0/4 + 1/8, the same value as the decimal 0.625.

Now the tenth. Our run builds it out of halves, place by place:

1/10 in binary:  0.000110011001100110011001...1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + ...

The pattern 0011 repeats for ever. In the documentation's words, in base 2, 1/10 is the infinitely repeating fraction. A tenth cannot be written exactly in halves, just as a third cannot be written exactly in decimals.

0.1 is stored a hair too big

The computer has to stop somewhere. According to the documentation, almost all platforms map Python floats to IEEE 754 "double precision" values with 53 bits of precision. For 0.1 that gives the fraction 3602879701896397 / 2**55. Written out in full, it is exactly this value:

stored for 0.1:  0.1000000000000000055511151231257827021181583404541015625stored for 0.2:  0.200000000000000011102230246251565404236316680908203125

Rounding to 53 bits went up, and the documentation notes that the stored value is therefore a little larger than 1/10. The same holds for 0.2. When Python prints a float, it shows the shortest number that maps to the stored value, so you see 0.1. Only a calculation brings the leftover into view.

Add the two stored values, and the result lands on a different stored value from the number 0.3:

0.1 + 0.2 gives:  0.30000000000000004440892098500626161694526672363281250.3 is:           0.299999999999999988897769753748434595763683319091796875

Python prints the first as 0.30000000000000004 and the second as 0.3. That is why 0.1 + 0.2 == 0.3 is False. The documentation shows the same thing with three lots of 0.1. In our run, adding 0.1 ten times gives 0.9999999999999999, not 1.

The test: count in whole cents

Now change one thing: instead of 0.10 and 0.20, add 10 and 20, counting cents rather than fractions of a unit.

>>> 10 + 2030>>> 10 + 20 == 30True

Whole numbers have no digits after the point, so there is no leftover like the tenth's. In Python, the documentation adds, integers have unlimited precision. Only when the total is shown does 30 cents become 0.30 again.

The second case: Ethereum counts in wei

Ethereum works on the same idea. According to ethereum.org, wei is the smallest possible amount of ether, and one wei is 10^-18 ether. Every transaction has a value field, and that value is denominated in wei. Account balances are shown in wei too.

Our run does the reel's sum in wei:

0.1 ether  =  100000000000000000 wei0.2 ether  =  200000000000000000 weisum        =  300000000000000000 wei   exactly 0.3 ether

No four, because only whole numbers are added.

Check it yourself

from decimal import Decimalprint(0.1 + 0.2)                        # 0.30000000000000004print(Decimal(0.1))                     # the value stored for 0.1print(10 + 20)                          # 30, in centsprint(Decimal("0.1") + Decimal("0.2"))  # 0.3

The decimal module is what the Python documentation points to for work that needs exact decimal representation, such as accounting. Note the quotation marks: Decimal("0.1") is exactly one tenth, while Decimal(0.1) takes over the float's already rounded value.

What whole cents do not fix

  • Dividing is still dividing. Split 100 cents three ways and Python's divmod(100, 3) gives (33, 1). One cent is left over, and your code has to decide who gets it.
  • JavaScript has a limit on whole numbers. In our run, Node.js prints 9007199254740992 for 2**53 + 1, one short; Number.MAX_SAFE_INTEGER is 9007199254740991. One ether is 10^18 wei, far above that limit. Python gives 9007199254740993 for the same sum.
  • Rounding the display is cosmetic. The documentation calls the neatly rounded output an illusion: the stored value stays what it is.
  • "Too big" is true for 0.1, not for every number. The value stored for 0.3 sits just below it, as the code block above shows.
  • We checked Python and JavaScript. The statement about other languages comes from the Python documentation, not from our run.

Get the next explainer

Every Cube Algos reel gets a post like this one: the mechanism worked through, with sources and limits. If you would like the next one by email, sign up below. You will first get an email with a confirmation link; you are only on the list after you click it, and every email has a link to unsubscribe.

Sources

All sources last opened on 1 December 2026. Every output in the code blocks, the breakdown of 1/10 into halves, the wei sum and the JavaScript limit come from our own runs (Python 3.13.5, Node.js 22.20.0), not from the sources.