# Scale value by range

I have a range of some values, lets say from 1 to 10. With this range I want to set values from other ranges. For example:

• MainRange(1;10)
• RangeA(5;80)
• RangeB(13;140)

• 1 = 5 = 13

• 2 = ... = ...
• 10 = 80 = 140

How to calculate this?

Suppose we want to convert values in range A to values in range B. Range A goes from $p_a$ to $q_a$, and range B goes from $p_b$ to $q_b$, where in both cases, the first number is included but the last isn't. In range A, there are $q_a - p_a$ numbers, but in range B, there are $q_b - p_b$ numbers; thus a change of 1 in range A should be a change of $\frac{q_b - p_b}{q_a - p_a}$ in range B. (This means that a change of $q_a - p_a$ in range A (i.e. the full range) will produce a change of $q_b - p_b$ in range B.)
In addition, range A starts at $p_a$ while range B starts at $p_b$, so we must add $p_b - p_a$ to have them 'start' at the same number. The final transformation is $$a \in A \rightarrow \big(a \cdot \frac{q_b - p_b}{q_a - p_a} + p_b - p_a\big) \in B$$