[Python-Dev] Decimal data type issues (original) (raw)

Batista, Facundo FBatista at uniFON.com.ar
Tue Apr 13 16:20:00 EDT 2004


[Shane Hathaway]

#- Should we expect this to work? #- #- d = {} #- d[Decimal('1.1')] = 1 #- print d[float('1.1')] #-

No, because Decimal('1.1') is not the same number than float('1.1')!

d = {} d[Decimal('1.1')] = 1 d {Decimal( (0, (1, 1), -1) ): 1} d[float('1.1')] Traceback (most recent call last): File "<pyshell#10>", line 1, in ? d[float('1.1')] KeyError: 1.1000000000000001

In part, this is why you can not compare Decimal with float, and why you can not create a Decimal directly from float. You can use the alternative method, but you should know what you're doing:

Decimal.fromfloat(1.1) Decimal( (0, (1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 1, 7, 8, 4, 1, 9, 7, 0, 0, 1, 2, 5, 2, 3, 2, 3, 3, 8, 9, 0, 5, 3, 3, 4, 4, 7, 2, 6, 5, 6, 2, 5), -51L) ) Decimal.Decimal.fromfloat(1.1, 1) Decimal( (0, (1, 1), -1L) )

. Facundo



More information about the Python-Dev mailing list