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')
FalseIn 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.
There's some discussion here: https://docs.python.org/3.1/tutorial/floatingpoint.html. Basically: eval(repr(x)) == x is guaranteed, and some attempt is made to give the shortest such string representation for Python 3.1+ (and 2.7?), but it's not guaranteed on all platforms.
Re:0.30000000000000004
by indosauros on Friday November 13, 2015 @08:06AM
(#10559588)
It also matters whether you're using Python 2 or 3. This is one of the things they changed in 3:
>>> print(0.1 + 0.2)
0.30000000000000004I believe this is relevant:
https://bugs.python.org/issue1580
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.