Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102054

Re: How to simulate C style integer division?

From Grobu <snailcoder@retrosite.invalid>
Newsgroups comp.lang.python
Subject Re: How to simulate C style integer division?
Date 2016-01-23 17:44 +0100
Organization A noiseless patient Spider
Message-ID <n80aem$7d1$1@dont-email.me> (permalink)
References (1 earlier) <n7s28p$7ub$1@dont-email.me> <56a1a698$0$1612$c3e8da3$5496439d@news.astraweb.com> <n7vq24$a71$1@dont-email.me> <n7vsta$kph$1@dont-email.me> <lf5mvrwz651.fsf@ling.helsinki.fi>

Show all headers | View raw


On 23/01/16 16:07, Jussi Piitulainen wrote:
> Grobu writes:
>
>>> def intdiv(a, b):
>>>       return (a - (a % (-b if a < 0 else b))) / b
>>>
>>>
>>
>> Duh ... Got confused with modulos (again).
>>
>> def intdiv(a, b):
>>      return (a - (a % (-abs(b) if a < 0 else abs(b)))) / b
>
> You should use // here to get an exact integer result.
>

You're totally right, thanks! It isn't an issue for Python 2.x's 
"classic division", but it becomes one with Python 3's "True division", 
and '//' allows to play it safe all along.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to simulate C style integer division? Shiyao Ma <i@introo.me> - 2016-01-21 16:39 +0800
  Re: How to simulate C style integer division? "Peter Heitzer" <peter.heitzer@rz.uni-regensburg.de> - 2016-01-21 08:44 +0000
  Re: How to simulate C style integer division? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-21 10:59 +0200
    Re: How to simulate C style integer division? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-21 12:13 +0200
  Re: How to simulate C style integer division? Grobu <snailcoder@retrosite.invalid> - 2016-01-22 02:59 +0100
    Re: How to simulate C style integer division? Steven D'Aprano <steve@pearwood.info> - 2016-01-22 14:48 +1100
      Re: How to simulate C style integer division? Grobu <snailcoder@retrosite.invalid> - 2016-01-23 13:04 +0100
        Re: How to simulate C style integer division? Grobu <snailcoder@retrosite.invalid> - 2016-01-23 13:52 +0100
          Re: How to simulate C style integer division? Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-23 17:07 +0200
            Re: How to simulate C style integer division? Grobu <snailcoder@retrosite.invalid> - 2016-01-23 17:44 +0100

csiph-web