Division help in python

Hans Mulder hansmu at xs4all.nl
Sat Sep 8 04:09:23 EDT 2012


On 8/09/12 09:03:12, garabik-news-2005-05 at kassiopeia.juls.savba.sk wrote:
> Chris Angelico <rosuav at gmail.com> wrote:
>> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <ramyasri20 at gmail.com> 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



More information about the Python-list mailing list