I'm trying to convert one range of numbers to another, maintaining ratio. Maths is not my strong point.
I have an image file where point values may range from -16000.00 to 16000.00 though the typical range may be much less. What I want to do is compress these values into the integer range 0-100, where 0 is the value of the smallest point, and 100 is the value of the largest. All points in between should keep a relative ratio even though some precision is being lost I'd like to do this in python but even a general algorithm should suffice. I'd prefer an algorithm where the min/max or either range can be adjusted (ie, the second range could be -50 to 800 instead of 0 to 100).
Here's some short Python functions for your copy and paste ease, including a function to scale an entire list.
Which can be used like so:
In my case I wanted to scale a logarithmic curve, like so:
I personally use the helper class which supports generics (Swift 3 compatible)
Short-cut/simplified proposal
wayne
Actually there are some cases that above answers would break. Such as wrongly input value, wrongly input range, negative input/output ranges.
PHP Port
Found PenguinTD's solution helpful so I ported it to PHP. Help yourself!
C++ Variant
I found PenguinTD's Solution usefull, so i ported it to C++ if anyone needs it: