Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Matt Wheeler Newsgroups: comp.lang.python Subject: Re: How to simulate C style integer division? Date: Fri, 22 Jan 2016 00:51:21 +0000 Lines: 27 Message-ID: References: <56a0d24f$0$1614$c3e8da3$5496439d@news.astraweb.com> <87mvrzc8bx.fsf@elektro.pacujo.net> <877fj263aj.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de k0m4TxAkMKSizWfSKA0/cAJ501mnjtii+jSnIPfNSONQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'def': 0.13; '2016': 0.16; '21:17,': 0.16; 'b):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:1 40': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'header :In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'skip:2 30': 0.29; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'depends': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'january': 0.38; 'from:addr:m': 0.84 X-Virus-Scanned: Debian amavisd-new at membrane.funkyhat.net X-Gm-Message-State: AG10YOQzhSxZBnbY7TWsEmUybiVfuauZGTNIxzVv8elNrnpLgwoel3sw8vYleF5Ab1AKZzMFr+YTl4tofKSBQw== X-Received: by 10.28.130.205 with SMTP id e196mr524606wmd.34.1453423900958; Thu, 21 Jan 2016 16:51:40 -0800 (PST) In-Reply-To: <877fj263aj.fsf@elektro.pacujo.net> X-Gmail-Original-Message-ID: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101987 On 21 January 2016 at 21:17, Marko Rauhamaa wrote: > Well, then there's: > > def intdiv(a, b): > return int(a / b) That depends on how accurate you need it to be >>> def intdiv(a, b): ... return a//b if (a < 0) == (b < 0) else -(-a//b) ... >>> num = 3**171 >>> num 3870210234510307998744588107535211184800325224934979257430349324033792477926791547 >>> num2 = 4**80 >>> num2 1461501637330902918203684832716283019655932542976 >>> intdiv(num, num2) 2648105301871818722187687529062555 >>> int(num/num2) 2648105301871818477801931416797184 -- Matt Wheeler http://funkyh.at