need clarification on -0

Discussion in 'Python' started by moijes12, Nov 28, 2009.

  1. moijes12

    moijes12 Guest

    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, Nov 28, 2009
    #1
    1. Advertising

  2. moijes12

    moijes12 Guest

    On Nov 28, 12:55 pm, Erik Max Francis <> 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 && &&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
    moijes12, Nov 28, 2009
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Nov 28, 2009
    #3
  4. On Nov 28, 8:39 pm, Tim Roberts <> 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

    >
    > 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, Nov 28, 2009
    #4
  5. On Nov 28, 11:14 pm, Mark Dickinson <> 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
    Mark Dickinson, Nov 28, 2009
    #5
  6. 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
    Steven D'Aprano, Nov 29, 2009
    #6
  7. moijes12

    Ned Deily Guest

    In article <009b4bab$0$26925$>,
    Steven D'Aprano <> 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,
    Ned Deily, Nov 29, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem
    Replies:
    1
    Views:
    1,597
    Shankara Narayanan
    Nov 3, 2004
  2. HDSkiFreak
    Replies:
    2
    Views:
    3,518
    HDSkiFreak
    May 11, 2004
  3. Krishna
    Replies:
    5
    Views:
    1,777
    Howard
    Apr 1, 2005
  4. Tool69
    Replies:
    2
    Views:
    283
    Tool69
    Dec 15, 2006
  5. wizwx
    Replies:
    9
    Views:
    300
    David T. Ashley
    Feb 13, 2007
Loading...

Share This Page