Re:0.30000000000000004
by mkesper on Friday November 13, 2015 @05:31AM
(#10559042)
Additionally, for exact calculations you might want to use the decimal module. https://docs.python.org/3.5/library/decimal.html#module-deci...
[ Parent
| on HN ]
But be wary so you don't pass in floats when creating your decimals
In [10]: from decimal import Decimal
In [11]: Decimal(0.1) + Decimal(0.2) - Decimal(0.3)
Out[11]: Decimal('2.775557561565156540423631668E-17')
In [12]: Decimal("0.1") + Decimal("0.2") - Decimal("0.3")
Out[12]: Decimal('0.0')Try the usual decimally-unrepresentable numbers (like 1/3).
As illumen pointed out, Python has also got fractions.
For things like Pi however, there is no exact numerical representation and you'd probably want sympy or something similar.
Sure. But the inability to represent 1/3 in decimal is just the same as inability to represent 1/10 in binary. The binary case just seems different because we are using a foreign base (decimal) literal "0.1" to express the binary number.