Dict handling of floats

Donn Cave donn at drizzle.com
Fri Apr 25 06:02:52 CEST 2003


Quoth Jim Richardson <warlock at eskimo.com>:

| I have come across a weirdness. I have a number of floats, (lat long
| pairs) which I stuff into a dict, but when I take them out, my nice %2f4
| number, (say, 47.5894) is something long and *almost* right, like
| 47.589333333333333339. What gives? I am not doing anything special as
| far as I can tell, just stuffing the numbers into a dict, and recalling
| it later. This is with Python 2.2 on Linux, and no, it's not an original
| Pentium :)

They are floats, they were born weird.

You're looking at the repr() output, which for reasons we won't
go into here renders the value to its ultimate precision.  If
you run the same numbers through str(), they'll look different.

Maybe the big question here is whether your notion of "right"
has any computational significance, that is, does it matter to
your application which of these renditions more accurately
describes the value.  Because in that respect, repr is right
and str is almost right.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list