Velocity Reviews > Re: Division help in python

Re: Division help in python

Jean-Michel Pichavant
Guest
Posts: n/a

 09-07-2012
Ramyasri Dodla wrote:
> Hi All,
>
> 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
>

Why should it yield 'zero' ?
The definition of the euclidean division :
(http://en.wikipedia.org/wiki/Euclidean_division)

a = b*q +r with 0≤ r < |b|

With the constraint of r being a positive integer, the couple (q, r) is
unique:

with a=-5, b=10

-5 = 10*-1 + 5 (q=-1, r=+5)

Note that for the strict Euclidean division, I mean the one allowing r
to be negative, then

-5 = 10*0 - 5 (q=0, r=-5) is also valid, but I there's still no reason
to state that it SHOULD be prefered over the other solution.

The uniqueness of the solution for the 1st definition is probably what
makes python yield -1 instead of 0.

Cheers,

JM

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Chris Angelico Python 2 09-08-2012 08:09 AM Serhiy Storchaka Python 0 09-07-2012 06:17 PM krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM kermit@polaris.net Python 2 12-07-2006 02:35 AM seb.haase@gmail.com Python 18 02-20-2006 07:36 PM

Advertisments