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
>>> -0 and True
0 ----------> (Why is this 0 and not say True or False)
>>> -0 and false
0
>>> -0 or True
True
Could someone please provide me some resources on how these operations
take place.I'd wanna find it out myself
Thanks
moijes
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 && m...@alcyone.com && 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
> 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 and True
> 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.
--
Steven
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. Having spent
10 years with Control Data (the 6000 and Cyber 70/170 mainframes were all
one's complement), I am particularly sensitive to this. Processors are
usually architected so that you don't normally see the -0, but it leads you
to think about arithmetic a bit differently.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
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
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:
>>> -1 & 1
0
but the same operation on longs would give a different
result:
>>> -1L & 1L
1L
Mark
>> 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.
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).
--
Steven
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.
--
Ned Deily,
n...@acm.org