Nostr'a Katılın
2025-12-31 03:00:30 UTC

Brandon Rohrer on Nostr: It is surprisingly hard to get computers to do addition with decimals the way a ...

It is surprisingly hard to get computers to do addition with decimals the way a third-grader can.

adding floats gives things like

0.1 + 0.2 = 0.30000000000000004

https://graffitiwall.nexus/graffiti_adder.html

The mpmath library in Python is capable of arbitrary precision, but still gives things like

-1.2 + 1.21 = 0.009999999999...

https://mpmath.org

The answer I was looking for was Python's decimal library. It gives

0.1 + 0.2 = 0.3
and
-1.2 + 1.21 = 0.01

https://docs.python.org/3/library/decimal.html