Modul (%) in python not like in C?

mensanator at aol.com mensanator at aol.com
Mon Sep 10 01:57:24 EDT 2007


On Sep 9, 11:43?pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Mon, 10 Sep 2007 01:06:59 GMT, Bryan Olson <fakeaddr... at nowhere.org>
> declaimed the following in comp.lang.python:
>
> > They also disagree about integer division. C rounds toward zero;
> > Python rounds downward.
>
> > In C:       -111 / 10  evaluates to -11
> > In Python:  -111 / 10  evaluates to -12
>
> > > Is this a known gotcha? I tried to google the
> > > subject however one cannot google the symbol %.
>
> > I think it's a known gotcha. I cited the refs in another post.
>
>         The best answer is probably to be found from the definition of
> divmod()
>
>
>
> >>> divmod(70, 6)
> (11, 4)
> >>> 6*11 + 4
> 70
> >>> divmod(-70, 6)
> (-12, 2)
> >>> 6 * -12 + 2
> -70
>
> Or in general terms
>
> (a, b) = divmod(x, y)
> x = y * a + b

But, in general, doesn't (-11,-4) also satisfy
  x = y *   a +   b
-70 = 6 * -11 + (-4)?

Do you overpay your car loans and wait for a refund?

Or do you make your last payment the residue of the
debt after making 47 (or whatever) equal payments?


>
> IOWs, the definition of modulo, and the definition of integer division,
> are linked...
>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/





More information about the Python-list mailing list