[Python-Dev] Caching float(0.0)

"Martin v. Löwis" martin at v.loewis.de
Tue Oct 3 19:55:43 CEST 2006


skip at pobox.com schrieb:
> If C90 doesn't distinguish -0.0 and +0.0, how can Python?  Can you give a
> simple example where the difference between the two is apparent to the
> Python programmer?

Sure:

py> x=-0.0
py> y=0.0
py> x,y
(-0.0, 0.0)
py> hash(x),hash(y)
(0, 0)
py> x==y
True
py> str(x)==str(y)
False
py> str(x),str(y)
('-0.0', '0.0')
py> float(str(x)),float(str(y))
(-0.0, 0.0)

Imagine an application that reads floats from a text file, manipulates
some of them, and then writes back the complete list of floats. Further
assume that somehow, -0.0 got into the file. Currently, the sign
"round-trips"; under the proposed change, it would stop doing so.
Of course, there likely wouldn't be any "real" change to value, as
the sign of 0 is likely of no significance to the application.

Regards,
Martin


More information about the Python-Dev mailing list