I know that computing decimals is not an easy task for a computer, but is there any way I can get simple arithmetic get done in python? What is the best approach?
>>> 0.3 + 0.3 + 0.3 + 0.1 != 1
True
>>> from decimal import Decimal
>>> Decimal(0.3) + Decimal(0.3) + Decimal(0.3) + Decimal(0.1) != 1
True
>>> Decimal(0.3) + Decimal(0.3) + Decimal(0.3) + Decimal(0.1) != Decimal(1)
True
>>> Decimal(0.3) + Decimal(0.3) + Decimal(0.3) + Decimal(0.1)
Decimal('0.9999999999999999722444243843')
Update:
As proposed, the solution would be to use Decimal()
and strings instead of plain numbers. But I find this solution very unsatisfying (non pythonic and ugly). Really there is no other way? (using a decorator perhaps?)