by philh · 11y ago · 0 comments
I now think the behiviour I ascribed to print, is actually what repr does. I'm not sure about print, but I guess it is just rounding to a certain number of places.
>>> from decimal import Decimal
>>> Decimal(0.1+0.2)
Decimal('0.3000000000000000444089209850062616169452667236328125')
>>> Decimal(0.3)
Decimal('0.299999999999999988897769753748434595763683319091796875')
>>> Decimal(0.300000000000000000000000000001)
Decimal('0.299999999999999988897769753748434595763683319091796875')
>>> print 0.300000000000000000000000000001
0.3
>>> repr(0.300000000000000000000000000001)
'0.3'
>>> D(0.30000000000000004)
Decimal('0.3000000000000000444089209850062616169452667236328125')
>>> repr(0.299999999999999988897769753748434595763683319091796875)
'0.3'
>>> repr(0.30000000000000004)
'0.30000000000000004'
>>> print 0.30000000000000004
0.3 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 >>> print(0.1 + 0.2)
0.30000000000000004