Velocity Reviews > need clarification on -0

# need clarification on -0

moijes12
Guest
Posts: n/a

 11-28-2009
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

>>> -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

moijes12
Guest
Posts: n/a

 11-28-2009
On Nov 28, 12:55*pm, Erik Max Francis <(E-Mail Removed)> wrote:
> moijes12 wrote:
> > 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

>
> 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 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

Steven D'Aprano
Guest
Posts: n/a

 11-28-2009
On Fri, 27 Nov 2009 23:09:06 -0800, moijes12 wrote:

> 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

Mark Dickinson
Guest
Posts: n/a

 11-28-2009
On Nov 28, 8:39*pm, Tim Roberts <(E-Mail Removed)> wrote:
> moijes12 <(E-Mail Removed)> wrote:
>
> >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

>
> 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

Mark Dickinson
Guest
Posts: n/a

 11-28-2009
On Nov 28, 11:14*pm, Mark Dickinson <(E-Mail Removed)> wrote:
> 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/tr....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

Steven D'Aprano
Guest
Posts: n/a

 11-29-2009
On Sat, 28 Nov 2009 15:14:31 -0800, Mark Dickinson wrote:

>> 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

Ned Deily
Guest
Posts: n/a

 11-29-2009
In article <009b4bab\$0\$26925\$(E-Mail Removed)>,
Steven D'Aprano <(E-Mail Removed)> wrote:
> 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.

--
Ned Deily,
http://www.velocityreviews.com/forums/(E-Mail Removed)