Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: How to simulate C style integer division? Date: Thu, 21 Jan 2016 14:52:08 -0500 Lines: 8 Message-ID: References: <56a0d24f$0$1614$c3e8da3$5496439d@news.astraweb.com> <87mvrzc8bx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de uaavshCEEaeJL6SpVESoEAzEAO3NXwmgoN8kAlXtpA9Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '21,': 0.07; 'subject:How': 0.09; 'received:internal': 0.09; 'jan': 0.11; 'def': 0.13; 'thu,': 0.15; 'b):': 0.16; 'division.': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'opposite': 0.16; 'personally,': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.44': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:66.111.4.27': 0.16; 'received:compute4.internal': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:out3-smtp.messagingengine.com': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'done': 0.35; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'received:66': 0.38; 'to:addr:python.org': 0.40; 'header:Message-Id:1': 0.61 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=Cu9x5zEz5sRJCo1RkPpLaaGvyEU=; b=Yo8iIP 3P0NTK/nvPiW/cOib+Jq9SuweGLZ9NG0a2/VHYnB4SpG1EzxD/EtHq4QDf7W20Ri tpiynpMpVkebdA9UXrihANtdt/dgU/heNyLZT+LRd1h3UT953LdcJfBoy3fex7sX VhPmdVSbwmsMHosXOG32elIeuV6mzuBKFhUOk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=Cu9x5zEz5sRJCo1 RkPpLaaGvyEU=; b=ubKqNwM+bUMopdX6zvI/jSNgLT7WXXBn6mlKbJqhbUjuivL L+WOYUtNc14fV+WUwOFaqHu1Daoo6VYSF1NahFBpAZcE7E1IP3GSL6tvh6iVYhzH eLHr/ZBDRm1JNOFtO8mMjrtGUeS06YA5NNgCa78fzLN/mmXgCpxF7ierUhlI= X-Sasl-Enc: 5+JYxKoHiLq/0raRVd7liQ8bVVUTfs8NQ08BbzBOokzg 1453405928 X-Mailer: MessagingEngine.com Webmail Interface - ajax-6cda141f In-Reply-To: <87mvrzc8bx.fsf@elektro.pacujo.net> 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:101985 On Thu, Jan 21, 2016, at 09:31, Marko Rauhamaa wrote: > Maybe: > > def intdiv(a, b): > return a // b if (a < 0) == (b < 0) else -(-a // b) Personally, I like a // b + (a % b and a ^ b < 0) - I've done the opposite in C to get python-style division.