3 ms·
you are using Decimal "wrong". If you want it to handle exact values, you need to pass a string with the decimal interpretation Python 3.5.0 (v3.5.0:374f50
by _ZeD_ 11y ago
you are using Decimal "wrong". If you want it to handle exact values, you need to pass a string with the decimal interpretation
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.2') - Decimal('0.3')
Decimal('0.0')
>>> Decimal(0.1) + Decimal(0.2) - Decimal(0.3)
Decimal('2.775557561565156540423631668E-17')
>>> Decimal(0.1) == Decimal('0.1')
False
- philh 11y agoIn this case, that was deliberate. I was using it to show the exact decimal value of the floating point approximations to decimal numbers, not to do decimal arithmetic.