🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Brain fart. How can I reduce some number ranges to another range?

Started by
4 comments, last by SiCrane 15 years, 3 months ago
Hey all, Im working on a radar of sorts and am having a huge brain fart.. and google isnt helping much either. I have three number ranges.. 1 - 10,000, 1 - 30,000 & 1 - 55,000 I need to be able to convert each to a range from 1-64.. and for the life of me, the math just isn't working in my favor.. Any ideas?

Squiggly Frog - My little project place on the web. Updated as I see fit. =)

Advertisement
I suspect you're after linear interpolation.

Take the value, and subtract the lower bound of its current range (-1), then divide by the difference between the upper bound and lower bound of that range (/9999). This gives you a value between 0 and 1, where 0 corresponds to the lower bound of the old range and 1 corresponds to the upper bound of the old range. Now multiply by the difference between the upper bound and lower bound of the new range (*63) and add the lower bound of the new range (+1).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

You can always perform linear normalisation,

For each value x in the range ( 1, 10000 )
new_value = ( x - 1 ) / ( 10000 - 1 )

This would give you a range between 0-1, so,

For each value x,
new_value = 63*x+1

For example,

The value x=1 will become 1;
The value x=10000 will become 64;

The value 5000 will become ~0.5

Hope this helps!
new_value = (old_value - old_min) / old_range * new_range + new_min

For 1 to 10,000, old_min is 1 and old_range is 9,999. Your new range and minimum is 63 and 1, respectively.

EDIT: Whoah. It took me less than a minute to type that, but benryves' post hadn't shown up yet. Is there some update lag between the database clusters on the GD net forums or something?
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Thanks for the assistance all! Got it!

Squiggly Frog - My little project place on the web. Updated as I see fit. =)

In case it matters, those formulas are not integer friendly.

This topic is closed to new replies.

Advertisement