Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'works.': 0.07; 'modulo': 0.09; 'operator,': 0.09; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'wrote:': 0.16; '(or': 0.21; 'header:In-Reply-To:1': 0.24; '(this': 0.24; 'wondering': 0.25; 'header:User-Agent:1': 0.26; 'this.': 0.28; "i'm": 0.29; 'convert': 0.29; 'values': 0.30; 'to:addr:python-list': 0.35; 'there': 0.36; '(and': 0.36; 'subject:" ': 0.36; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'sure': 0.40; 'where': 0.40; 'called': 0.40; 'places': 0.64; 'music': 0.78; '12:': 0.84; 'do:': 0.91; 'ranging': 0.91; 'subject:value': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JOrGyJ+b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=AGkejYDSTmIA:10 a=IkcTkHD0fZMA:10 a=P9NBYvFgXSPQULFFcecA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Thu, 02 Jul 2015 01:30:45 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: "normalizing" a value References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: , Newsgroups: comp.lang.python Message-ID: Lines: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435797056 news.xs4all.nl 2826 [2001:888:2000:d::a6]:46558 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93395 On 2015-07-02 01:12, bvdp wrote: > Not sure what this is called (and I'm sure it's not normalize). Perhaps "scaling"? > > Anyway, I need to convert various values ranging from around -50 to 50 to an 0 to 12 range (this is part of a MIDI music program). I have a number of places where I do: > > while x < 0: x += 12 > while x >= 12: x -= 12 > > Okay, that works. Just wondering if there is an easier (or faster) way to accomplish this. > That's called "modular arithmetic". Use the modulo operator, '%': x = x % 12 or just: x %= 12