Dict handling of floats

Jim Richardson warlock at eskimo.com
Fri Apr 25 03:23:32 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 25 Apr 2003 04:02:52 -0000,
 Donn Cave <donn at drizzle.com> wrote:
> 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.
> 


It doesn't really matter to me in the application case, since one degree
to 4 or 6 points of precision, is plenty for most navigational purposes :) 


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+qOJ0d90bcYOAWPYRAj51AJ4rd84tO69/oK2ww2fsokBglVP5TwCgl2WW
KavjvS2/38mv+xPBX0h0MBg=
=GBMJ
-----END PGP SIGNATURE-----

-- 
Jim Richardson         http://www.eskimo.com/~warlock

Linux, because eventually, you grow up enough to be trusted with a fork()




More information about the Python-list mailing list