need clarification on -0

M

moijes12

Hi

I know the value -0 is quite meaningless and makes little sense.But I
was just fiddling.I am unable to figure out the below result

True

Could someone please provide me some resources on how these operations
take place.I'd wanna find it out myself

Thanks
moijes
 
M

moijes12

Your questions have nothing to do with -0, as it's no different from 0:

 >>> 0 == -0
True

Your examples work the same way with simply 0, which is considered a
false value:

 >>> bool(0)
False
 >>> 0 and True
0
 >>> 0 and False
0
 >>> 0 or True
True

What you're seeing is simply the short-circuiting behavior of the `and`
and `or` operators; they return the last (relevant) value they
encountered before making their determination of the value of the
overall expressions.  See python.org/doc for more information.

--
Erik Max Francis && (e-mail address removed) &&http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   You'll survive / A true Darwin star
    -- Des'ree

Thanks Erik
 
S

Steven D'Aprano

Hi

I know the value -0 is quite meaningless and makes little sense.

Actually, when it comes to floating point values, it is very useful to be
able to distinguish between -0 and +0.

But I
was just fiddling.I am unable to figure out the below result


0 ----------> (Why is this 0 and not say True or False)

You need to know two things about Python:

(1) All values can be interpreted in a boolean context:

if None:
print "this will never be printed"
else:
print "this is always printed"

False values include: None, 0, 0.0, "", [], {} and of course False.

True values include nearly everything else.


(2) `and` and `or` are short-cut operators. They return the first
argument which unambiguously defines the result:

X and Y => X if X is a false value, and Y if X is a true value.
X or Y => X if X is a true value, and Y if X is a false value.


Why do `and` and `or` return objects other than True and False? This is
especially useful when using `or` in situations like this:

process(main_list or fallback_list)

which will process the first list of the two which is not empty.
 
M

Mark Dickinson

Actually, there ARE computers where you might not see this result.
Virtually all of the processors on which Python runs use two's complement
arithmetic.  In two's complement, there is no separate value called -0.  0
and -0 have the same bit representation.

In one's complement, -0 and 0 have different representations.

While that's true, I think the implementation of Python is
such that the Python objects -0 and 0 should always be
indistinguishable even on machines where the underlying
architecture represents integers using ones' complement or
sign-magnitude.

At least that's certainly the intention: there are bits of
CPython's source code that are deliberately written in
convoluted ways in order to avoid the assumption of two's
complement. But I have a nasty suspicion that, were Python
ever unlucky enough to meet a ones' complement machine,
we'd quickly find that there were many *other* bits of the
source code that tacitly (and incorrectly) assumed a two's
complement representation.

Mark
 
M

Mark Dickinson

While that's true, I think the implementation of Python is
such that the Python objects -0 and 0 should always be
indistinguishable even on machines where the underlying
architecture represents integers using ones' complement or
sign-magnitude.

Hmm. I really should think before posting. A quick glance
at int_and, int_xor and int_or in Objects/intobject.c:

http://svn.python.org/view/python/trunk/Objects/intobject.c?view=markup

shows that Python clearly fails to be independent of the
hardware's choice of integer representation. E.g., on a
ones' complement machine, Python would give:
0

but the same operation on longs would give a different
result:
1L

Mark
 
S

Steven D'Aprano

While that's true, I think the implementation of Python is such that the
Python objects -0 and 0 should always be indistinguishable even on
machines where the underlying architecture represents integers using
ones' complement or sign-magnitude.

I don't think that really has any bearing on the Original Poster's
question -- presumably on such machines, Python should treat both -0 and
+0 as false in a boolean context and generate the same result.

When it comes to integers, I'm not aware of any mathematical or
programming system which treats -0 and +0 as distinct entities, even if
they have different internal representations. But the same doesn't apply
for floats, where the IEEE standard requires that -0.0 and +0.0 be
distinct and distinguishable (although it also requires that they compare
as equal).
 
N

Ned Deily

When it comes to integers, I'm not aware of any mathematical or
programming system which treats -0 and +0 as distinct entities, even if
they have different internal representations.

A documented feature of most FORTRAN run-time libraries on the Control
Data 6600/70/170 family (a one's-complement architecture cited earlier
by Tim Roberts) was to convert a blank numeric input field (i.e.
consisting of all space characters) to minus zero. Many programmers
took advantage of that, using a test for -0 as an easy, though not 100%
foolproof, test for a missing numeric value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top