"normalizing" a value

random832 at fastmail.us random832 at fastmail.us
Thu Jul 2 03:27:36 CEST 2015


On Wed, Jul 1, 2015, at 20: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

And this gives you what you want? With e.g. 13=1, 14=2, 22=10, 23=11,
24=0, 25 = 1, etc. Seems unusual that that's what you would want.

Also note this gives an 0 to 11 range for the results, not 0 to 12.

Anyway, x %= 12 will give the same results.


More information about the Python-list mailing list