Gratuitous Mathematics

Monday, August 23 2004 @ 08:46 PM BST

Contributed by: robin

Here's a good one for all you maths fiends...

Map the five values `1..5` to the range `10..50`. Easy, huh? (Just in case you're not clear the answer I'm looking for is `10, 20, 30, 40, 50`!)

What if the range starts at zero? i.e. map `0..5` to `10..50`? Hmmm, a little more tricky but do-able. Answer: `10, 18, 26, 34, 42, 50`.

Now, how about mapping the values `0..40` to the range `0..1`? Erm...

Actually, it's just a case of finding a generic formula to calculate the new values from the old values.

To map a series of consecutive integer values Imin .. Imax to a range of continous real values Rmin .. Rmax, I came up with the following formula:

```     ( Rmax - Rmin )( Ii - Imin )
Ri = ----------------------------
( Imax - Imin )
```
where:

• `Ri` is the ith value in the range
• `Rmax` is the maximum value of the range
• `Rmin` is the minimum value of the range
• `Ii` is the ith integer in the series
• `Imin` is the 1st integer in the series
• `Imax` is the last integer in the series
So, back to the original question about mapping the values `0..40` to the range `0..1`.

In this example:

• Rmax = 1
• Rmin = 0
• Imax = 40
• Imin = 0
So, plugging these numbers into the formula gives the following values:

```R1  = (1 - 0)( 0 - 0) / (40 - 0)         = 0
R2  = (1 - 0)( 1 - 0) / (40 - 0) = 1/40  = 0.025
..
R40 = (1 - 0)(39 - 0) / (40 - 0) = 39/40 = 0.975
R41 = (1 - 0)(40 - 0) / (40 - 0) = 40/40 = 1
```
So, now you know.