Sabobin
Sabobin

Reputation: 4276

How to scale a value down?

I have a variable x that accepts a range of values from 0 - 500.

I want to represdnt this variable's value in a new variable xScaled that accepts a range of 0 - 1.

Example: Given x = 292 what is the relative value of xScaled and how can this be calculated?

Thanks

Upvotes: 2

Views: 8080

Answers (5)

danatel
danatel

Reputation: 4982

No offence intended, but this is fairly simple arithmetics. Your programming skills will greatly improve if you spend just few minutes a day here: http://www.khanacademy.org/

Upvotes: 1

Blindy
Blindy

Reputation: 67380

Divide by the maximum value: (0-500) becomes (0/500-500/500)=(0-1).

So for 292, scaled it becomes 292/500.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881523

You seem to be asking for the formula:

xScaled = x / 500

For a more general solution, the following pseudo-code can map one range to another:

def mapRange (x, from_min, from_max, to_min, to_max):
    return (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min

Upvotes: 4

Paul R
Paul R

Reputation: 212979

In C and similar languages:

x = 292;
xScaled = x / 500.0;

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

Just divide by 500:

xScaled = 292/500;

Upvotes: 5

Related Questions