Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Division help in python

Reply
Thread Tools

Re: Division help in python

 
 
Chris Angelico
Guest
Posts: n/a
 
      09-07-2012
On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
garabik-news-2005-05@kassiopeia.juls.savba.sk
Guest
Posts: n/a
 
      09-08-2012
Chris Angelico <(E-Mail Removed)> wrote:
> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      09-08-2012
On 8/09/12 09:03:12, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Chris Angelico <(E-Mail Removed)> wrote:
>> On Fri, Sep 7, 2012 at 10:53 PM, Ramyasri Dodla <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Division help in python Serhiy Storchaka Python 0 09-07-2012 06:17 PM
Re: Division help in python Jean-Michel Pichavant Python 0 09-07-2012 04:19 PM
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
True Division in Python kermit@polaris.net Python 2 12-07-2006 02:35 AM
Python 3000 deat !? Is true division ever coming ? seb.haase@gmail.com Python 18 02-20-2006 07:36 PM



Advertisments