Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Re: need clarification on -0

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


steve at REMOVE-THIS-cybersource

Nov 28, 2009, 1:18 AM

Post #1 of 8 (267 views)
Permalink
Re: need clarification on -0

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
--
http://mail.python.org/mailman/listinfo/python-list


max at alcyone

Nov 27, 2009, 11:55 PM

Post #2 of 8 (204 views)
Permalink
Re: need clarification on -0 [In reply to]

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 && max [at] alcyone && 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
--
http://mail.python.org/mailman/listinfo/python-list


moijes12 at gmail

Nov 28, 2009, 12:45 AM

Post #3 of 8 (199 views)
Permalink
Re: need clarification on -0 [In reply to]

On Nov 28, 12:55 pm, Erik Max Francis <m...@alcyone.com> 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 && 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
--
http://mail.python.org/mailman/listinfo/python-list


timr at probo

Nov 28, 2009, 12:39 PM

Post #4 of 8 (230 views)
Permalink
Re: need clarification on -0 [In reply to]

moijes12 <moijes12 [at] gmail> 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. 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, timr [at] probo
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


dickinsm at gmail

Nov 28, 2009, 3:14 PM

Post #5 of 8 (230 views)
Permalink
Re: need clarification on -0 [In reply to]

On Nov 28, 8:39 pm, Tim Roberts <t...@probo.com> wrote:
> moijes12 <moije...@gmail.com> 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
--
http://mail.python.org/mailman/listinfo/python-list


dickinsm at gmail

Nov 28, 2009, 3:38 PM

Post #6 of 8 (230 views)
Permalink
Re: need clarification on -0 [In reply to]

On Nov 28, 11:14 pm, Mark Dickinson <dicki...@gmail.com> 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/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
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 28, 2009, 4:58 PM

Post #7 of 8 (230 views)
Permalink
Re: need clarification on -0 [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


nad at acm

Nov 28, 2009, 10:33 PM

Post #8 of 8 (226 views)
Permalink
Re: need clarification on -0 [In reply to]

In article <009b4bab$0$26925$c3e8da3 [at] news>,
Steven D'Aprano <steve [at] REMOVE-THIS-cybersource> 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,
nad [at] acm

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.