precision of float in a dict

Alex Martelli aleaxit at yahoo.com
Fri May 23 10:36:03 EDT 2003


Sven Brandt wrote:

> Hi Alex,
> 
> Thank you and the others for pointing me to the info.
> The limited precision of a float is clear now.
> 
> I'm still curious why the value stored in a dict is different from the
> one of the float.

It's not.  Exactly the same float value is STORED.  A different string
value is DISPLAYED when you call str on the float (as implicitly you
do by directly printing it) vs when you call repr on the float (as you
do when you print the dict, since the dict in turn used repr to get the
representation of its items).  Try:

x = 0.1
adict = {'kk':x}

print x
print str(x)
print repr(x)

print adict

print adict['kk']
print str(adict['kk'])
print repr(adict['kk'])


Is this enough to show you that just one value is STORED in all cases,
just DISPLAYED differently in the various cases...?


Alex





More information about the Python-list mailing list