Reputation: 489
I am trying to convert a value, comprised in the range of two integers, to another integer comprised in an other given range. I want the output value to be the corresponding value of my input integer (respecting the proportions). To be more clear, here is an example of the problem I'd like to solve:
How is it possible to achieve such 'rescaling' in python ? I tried the usual calculation (value/max of first range)*max of second range
but it gives wrong values except in rare cases.
At first I had problems with the division with int
in python, but after correcting this, I still have wrong values. For instance, using the values given in the example, int((float(10)/28)*20)
will give me 7 as result, where it should return 5 because it's the minimum possible value in the first range.
I feel like it is a bit obvious (in terms of logic and math) and I am missing something.
Upvotes: 3
Views: 10447
Reputation: 110156
If you are getting wrong results, you are likely using Python2 - where a division always yield an integer - (and therefore you will get lots of rounding errors and "zeros" when it comes to scaling factors.
Python3 corrected this so that divisions return floats - in Python2 the workaround is either to put a from __future__ import division
on the first line of your code (preferred), or to explicitly convert at least one of the division operands to float - on every division.
from __future__ import division
def renormalize(n, range1, range2):
delta1 = range1[1] - range1[0]
delta2 = range2[1] - range2[0]
return (delta2 * (n - range1[0]) / delta1) + range2[0]
Upvotes: 7