Re: Division help in python

Discussion in 'Python' started by Chris Angelico, Sep 7, 2012.

  1. On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <> wrote:
    > I am brand new to python. checking over basic stuff. I came across the
    > problem while doing so. If any body aware of the problem, kindly respond me.
    >
    >>>> 5/10

    > 0
    >>>> - 5/10

    > -1
    >
    > The second case also should yield a 'zero' but it is giving a -1


    You're clearly using Python 2, because in Python 3, the / operator
    will return a float instead (so these would return 0.5 and -0.5
    respectively). But it's helpful to mention what Python version you're
    using when you ask for help :)

    The reason for this is that / (or in Python 3, //) rounds toward
    negative infinity, not toward zero. This allows the modulo operator
    (%) to return a positive number, while still maintaining the normal
    expectation that:

    (x//y)*y + (x%y) == x

    for any two integers x and y.

    Hope that helps!

    ChrisA
     
    Chris Angelico, Sep 7, 2012
    #1
    1. Advertisements

  2. Chris Angelico

    Guest

    Chris Angelico <> wrote:
    > On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <> wrote:
    >> I am brand new to python. checking over basic stuff. I came across the
    >> problem while doing so. If any body aware of the problem, kindly respond me.
    >>
    >>>>> 5/10

    >> 0
    >>>>> - 5/10

    >> -1
    >>
    >> The second case also should yield a 'zero' but it is giving a -1

    >
    >


    ....

    > The reason for this is that / (or in Python 3, //) rounds toward
    > negative infinity, not toward zero. This allows the modulo operator


    I think he means the non-obvious unary minus precedence.

    --
    -----------------------------------------------------------
    | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
    | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
    -----------------------------------------------------------
    Antivirus alert: file .signature infected by signature virus.
    Hi! I'm a signature virus! Copy me into your signature file to help me spread!
     
    , Sep 8, 2012
    #2
    1. Advertisements

  3. Chris Angelico

    Hans Mulder Guest

    On 8/09/12 09:03:12, wrote:
    > Chris Angelico <> wrote:
    >> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <> wrote:
    >>> I am brand new to python. checking over basic stuff. I came across the
    >>> problem while doing so. If any body aware of the problem, kindly respond me.
    >>>
    >>>>>> 5/10
    >>> 0
    >>>>>> - 5/10
    >>> -1
    >>>
    >>> The second case also should yield a 'zero' but it is giving a -1

    >>

    > ...
    >
    >> The reason for this is that / (or in Python 3, //) rounds toward
    >> negative infinity, not toward zero. This allows the modulo operator

    >
    > I think he means the non-obvious unary minus precedence.


    That seems unlikely. Unary minus has lower precedence in
    Python than in most other programming languages, but its
    precedence is higher than division, so this example doesn't
    show the difference.

    For example, in C unary opeators have the highest precedence.
    Yet -5/10 returns 0, not because of precedence, but because C
    rounds towards zero.


    Hope this helps,

    -- HansM
     
    Hans Mulder, Sep 8, 2012
    #3
    1. Advertisements

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. Replies:
    1
    Views:
    979
    Thomas Matthews
    Jan 21, 2005
  2. Replies:
    18
    Views:
    603
    Magnus Lycka
    Feb 20, 2006
  3. True Division in Python

    , Dec 6, 2006, in forum: Python
    Replies:
    2
    Views:
    574
    Terry Reedy
    Dec 7, 2006
  4. jamestuck21

    Binary Division Problem Help

    jamestuck21, Nov 30, 2006, in forum: C Programming
    Replies:
    22
    Views:
    1,971
    Al Balmer
    Dec 1, 2006
  5. Replies:
    94
    Views:
    6,043
    ¬a\\/b
    Feb 9, 2007
  6. Ninereeds

    Python 3 and PEP238 division

    Ninereeds, Mar 17, 2008, in forum: Python
    Replies:
    5
    Views:
    347
    Terry Reedy
    Mar 19, 2008
  7. Hutch

    Python 3.0 new integer division

    Hutch, Apr 8, 2008, in forum: Python
    Replies:
    8
    Views:
    860
    Mark Dickinson
    Apr 9, 2008
  8. jackie

    Help,how does division truncate?

    jackie, Aug 18, 2008, in forum: C Programming
    Replies:
    5
    Views:
    604
    jackie
    Aug 19, 2008
Loading...