Shift numbers into a different range

The function $$f(t) = c + \left(\frac{d-c}{b-a}\right)(t-a)$$ will map the interval $[a,b]$ onto the interval $[c,d]$ in that order (meaning $f(a)=c$ and $f(b)=d$), assuming $a\neq b$.

Addendum: It's worth checking out how one can derive this instead of just memorizing it. It's the composition of the following maps.

(1) First, apply the map $t\mapsto t-a$ so the left endpoint shifts to the origin. The image interval is $[0,b-a]$.

(2) Next scale the interval to unit length by dividing by its length (this is the only place the requirement $a\neq b$ is needed). The map that does this is $t\mapsto \frac{1}{b-a}\cdot t$. The image interval is $[0,1]$.

Then reverse the process to get at the other interval as follows.

(3) Scale up by the desired length $d-c$ using the map $t\mapsto (d-c )\cdot t$, and the image is $[0,d-c]$.

(4) Finally shift the left endpoint to $c$ by the map. $t\mapsto c+t$. The image is $[c,d]$.

Composing these maps in order gives the map I indicated at the top.


You want to transform $x\in[0,125]$ to $y\in[-128,128]$.

Assuming scales are linear, you need to do the following:

(1) "Stretch" the $x$-range of $125$ to the desired $y$-range of $256$: $$y=\frac {256}{125}x$$

(2) Set the zero offset (i.e. value of $y$ when $x$ is zero) to $-128$: $$y=\frac {256}{125}x-128\qquad \blacksquare$$

Check:

$$x=0 \Rightarrow y=-128\\ x=125 \Rightarrow y=128$$

as required.

NB: the same method can be applied to convert Celsius to Fahrenheit.