Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Formula for scaling a number to fit a given range?

12,865 views
Skip to first unread message

mwse...@yahoo.com

unread,
Apr 22, 2009, 12:25:57 PM4/22/09
to
I need a formula for scaling a number from one linear range to fit in
another linear range.

For example, Iets say I have a number that can range from -20 --> 50.
I need to map it to a value between 0 --> 100. I get that -20 maps to
0, 50 maps to 100 (the easy stuff), but I'm at a loss as to how to
calculate all possible mappings in the full range from -20 -> 50.

Is there a general formula for this calculation?

I know I knew how to do this at one point in my life (many years ago),
but for the life of me I can't remember now. I guess it comes with
age. Google hasn't been much help -- maybe I'm not wording my search
correctly. Any help would be appreciated. Thanks!

alainv...@gmail.com

unread,
Apr 22, 2009, 1:43:52 PM4/22/09
to

Bonjour,

from -20 --> 50 interval = 70 (1)
to 0 -->100 " " = 100 (2)

firstly: a dilatation (100/70) (1) -->(2)
second: origine (x+20) 'cause -->-20-->0

Then we've got the formula x' =(100/70)*(x+20)
x' = 10/7*(x+20)
Verification:
Middle of first interval x = 15
middle of second x' =10/7*(15+20)=10/7*35=50

Best regards,
Alain

Greg Neill

unread,
Apr 22, 2009, 1:46:15 PM4/22/09
to

Let's say the first range goes from A to B and your number
in that range is n1. That is, A <= n1 <= B.

The size of the range is given by R1 = B - A. Note that the
signs of A and B should be preserved. In your example, A = -20
and B = 50, so the size of the range would be

R1 = B - A
= 50 - (-20)
= 70

Similarly, say the second range is between C and D, so that its
size is R2 = D - C.

Express your number n1 as a fraction of the size of the first
range:

F = (n1 - A)/R1

= (n1 - A)/(B - A)

You want the number n2 in the second range to be the equivalent
fraction of that range. So:

F = (n2 - C)/(D - C) = (n1 - A)/(B - A)

Solve for n2:

n2 = (B*C - A*D)/(B - A) + n1*(D - C)/(B - A)

= (B*C - A*D)/R1 + n1*(R2/R1)

Using your example:

A = -20
B = 50
C = 0
D = 100
and pick a number for n1: n1 = -5

n2 = (50*0 - (-20)*100)/(70) + (-5)*(100/70)

= 2000/70 - 500/70

= 150/7

= 21.429 (approx)


Mensanator

unread,
Apr 22, 2009, 1:53:54 PM4/22/09
to
On Apr 22, 11:25 am, mwsene...@yahoo.com wrote:
> I need a formula for scaling a number from one linear range to fit in
> another linear range.
>
> For example, Iets say I have a number that can range from -20 --> 50.
> I need to map it to a value between 0 --> 100. I get that -20 maps to
> 0, 50 maps to 100 (the easy stuff), but I'm at a loss as to how to
> calculate all possible mappings in the full range from -20 -> 50.
>
> Is there a general formula for this calculation?

Yes, the equation of a straight line: y = mx + b

where m is the slope and b is the y intercept.

The original range is 50 - -20 = 70. You want a new range
of 100, so the slope is 100/70 or 10/7.

slope = gmpy.mpq('100/70')

Which would give us

for i in xrange(-20,51):
print i,i*slope,

-20 -200/7
-19 -190/7 -18 -180/7 -17 -170/7 -16 -160/7 -15 -150/7 -14 -20
-13 -130/7 -12 -120/7 -11 -110/7 -10 -100/7 -9 -90/7 -8 -80/7
-7 -10 -6 -60/7 -5 -50/7 -4 -40/7 -3 -30/7 -2 -20/7 -1 -10/7
0 0 1 10/7 2 20/7 3 30/7 4 40/7 5 50/7 6 60/7 7 10 8 80/7 9 90/7
10 100/7 11 110/7 12 120/7 13 130/7 14 20 15 150/7 16 160/7 17 170/7
18 180/7 19 190/7 20 200/7 21 30 22 220/7 23 230/7 24 240/7 25 250/7
26 260/7 27 270/7 28 40 29 290/7 30 300/7 31 310/7 32 320/7 33 330/7
34 340/7 35 50 36 360/7 37 370/7 38 380/7 39 390/7 40 400/7 41 410/7
42 60 43 430/7 44 440/7 45 450/7 46 460/7 47 470/7 48 480/7 49 70
50 500/7

but that's not 0 to 100. We need to add the y intercept. We see that
when x is -20, y is -200/7, so if we add 200/7 to each number...

intercept = gmpy.mpq('200/7')

for i in xrange(-20,51):
print i,i*slope+intercept, # equivalent to y = mx + b

-20 0
-19 10/7 -18 20/7 -17 30/7 -16 40/7 -15 50/7 -14 60/7
-13 10 -12 80/7 -11 90/7 -10 100/7 -9 110/7 -8 120/7
-7 130/7 -6 20 -5 150/7 -4 160/7 -3 170/7 -2 180/7 -1 190/7
0 200/7 1 30 2 220/7 3 230/7 4 240/7 5 250/7 6 260/7 7 270/7
8 40 9 290/7 10 300/7 11 310/7 12 320/7 13 330/7 14 340/7
15 50 16 360/7 17 370/7 18 380/7 19 390/7 20 400/7 21 410/7
22 60 23 430/7 24 440/7 25 450/7 26 460/7 27 470/7 28 480/7
29 70 30 500/7 31 510/7 32 520/7 33 530/7 34 540/7 35 550/7
36 80 37 570/7 38 580/7 39 590/7 40 600/7 41 610/7 42 620/7
43 90 44 640/7 45 650/7 46 660/7 47 670/7 48 680/7 49 690/7
50 100

...then the -20 to 50 x range translates to the 0 to 100 y range.

Therefore, y = 10/7 x + 200/7

mwse...@yahoo.com

unread,
Apr 22, 2009, 3:28:10 PM4/22/09
to
Thank you, all, for the thoughtful answers! You've been most helpful.

---Matt

Jasen Betts

unread,
Apr 23, 2009, 5:25:18 AM4/23/09
to
On 2009-04-22, mwse...@yahoo.com <mwse...@yahoo.com> wrote:
> I need a formula for scaling a number from one linear range to fit in
> another linear range.
>
> For example, Iets say I have a number that can range from -20 --> 50.
> I need to map it to a value between 0 --> 100. I get that -20 maps to
> 0, 50 maps to 100 (the easy stuff), but I'm at a loss as to how to
> calculate all possible mappings in the full range from -20 -> 50.

what you need to is also translate the size of the ranges:

eg -20 to 50 is 50-(-20) = 70

0 to 100 is 100-0 = 100

spoiler follows:



> Is there a general formula for this calculation?

mapping x from (a,b) to (c,d)

x'=((x-a)(d-c)/(b-a))+c

alexpe...@gmail.com

unread,
Apr 25, 2015, 11:57:30 AM4/25/15
to
I found this information in AB Manual, when they use SCP in RS Logix500.
Linear relationship between the input and scaled values - Calculating the Linear Relationship.

Input Value : 19,277
Scaled Value: 4.80 pH
Input MIN : 6,242
Input MAX : 52,800
Scaled MIN : 2
Scaled MAX : 12

0.000214786 Rate = ( Scaled MAX - Scaled MIN ) / ( Input MAX - Input MIN )
0.659306671 Offset = Scaled MIN - ( Input MIN * Rate )
4.799733666 Scaled Value = (Input Value * Rate) + Offset
0 new messages