[ python-Bugs-1729014 ] 0.0 and -0.0 end up referring to the same object

SourceForge.net noreply at sourceforge.net
Tue Jun 5 12:52:26 CEST 2007


Bugs item #1729014, was opened at 2007-05-31 15:27
Message generated for change (Comment added) made by swfiua
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1729014&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Johnnyg (swfiua)
Assigned to: Nobody/Anonymous (nobody)
Summary: 0.0 and -0.0 end up referring to the same object

Initial Comment:
I am not really sure whether this is a bug or a feature.

The attached code attempts to demonstrate the problem.

I had some code that was trying to change -0.0 to 0.0 so that the accountants I work with don't panic.

The code was something like this:

if n == -0.0:
    n = 0.0

Regardless of whether n is -0.0 or 0.0 the test passes (which is good).

However after the assignment n is actually -0.0

It looks like python is creating a single object for both -0.0 and 0.0.

Whichever appears first within the local scope seems to be the value that actually gets stored.

Eg changing the code to 

if n == 0.0:
    n = 0.0

gets me the behaviour I wanted.

----------------------------------------------------------------------

>Comment By: Johnnyg (swfiua)
Date: 2007-06-05 10:52

Message:
Logged In: YES 
user_id=1529783
Originator: YES

I'm happy to flag this as undefined behaviour.

I have worked around it in my code, the only issue is that the code is
brittle, since I think it relies on the scope of constants -- I'm guessing
that is what has changed between 2.4 and 2.5 and could well change in the
future.

John

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2007-06-03 06:50

Message:
Logged In: YES 
user_id=80475
Originator: NO

I don't see an easy way to make this a defined behavior.

FWIW, the OP's code suggests that it makes a more specific test than it
does (since -0.0 == 0.0) so the test succeed when n is either -0.0 or 0.0. 
A quick fix in his code would be to eliminate the -0.0 from the code.  

def r(n):
    if n == 0.0:
        return 0.0
    return n

or more succinctly:

def r(n):
    return n or 0.0


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2007-06-01 05:49

Message:
Logged In: YES 
user_id=33168
Originator: NO

This is a regression from 2.4.  This seems to always have been undefined
behaviour.  It looks like it was the result of the compiler changes (code
is the same in both versions, but co_consts is diff):

Python 2.4.4c1 (#2, Oct 11 2006, 20:00:03) 
>>> def r(n):
...   if n == -0.0: n = 0.0
...   return n
... 
>>> r.func_code.co_consts
(None, 0.0)

Python 2.6a0 (trunk, May 30 2007, 21:02:18) 
>>> def r(n):
...  if n == -0.0: n = 0.0
...  return n
... 
>>> r.func_code.co_consts
(None, -0.0)


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1729014&group_id=5470


More information about the Python-bugs-list mailing list