## [math] Formula for scaling numbers?

Discuss all aspects of editing for ZDoom.
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

### [math] Formula for scaling numbers?

Let's say I want to scale a range of numbers... say, from 0.0 to 1.0 and translate it into another range, say 0 to 320.

How would I do this?

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Well, to convert from one range to another, you'd typically normalize the first range, then multiply the normalized value by the max value of the second range. However, since your first range is 0.0-1.0, it's already normalized. Therefore, all you need to do in this case is multiply the number by 320.

Examples:

0.0 (x 320) = 0
0.5 (x 320) = 160
0.6921 (x 320) = ~221
1.0 (x 320) = 320

(In case you're wondering, normalization is the process of converting a range into a decimal number between 0.0 and 1.0 -- To normalize 0-320, you'd divide each number in the range by 320)

[EDIT]Here's a more robust formula for you:

x = NewMax * n/OldMax
Last edited by HotWax on Fri Apr 27, 2007 10:56 am, edited 1 time in total.

HotWax
Do what you must, and pay the price later.

Joined: 18 Jul 2003
Location: Idaho Falls, ID

Divide the new range by the old range and that should work. I'm assuming you want to know this for sethudscale. I'm probably wrong though.

Edit: beaten by a better explanation.

chopkinsca
"." is too short

Joined: 11 Dec 2003

Specifically, I'm trying to convert the player's angle to a range of numbers to position a mouse cursor.

But I just realized the player's angle doesn't really work the way I thought it should though... hmmm... turning clockwise (to the right) decreases the the value instead of increasing.

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Nash wrote:But I just realized the player's angle doesn't really work the way I thought it should though... hmmm... turning clockwise (to the right) decreases the the value instead of increasing.

That is correct. See here for assistance.

HotWax
Do what you must, and pay the price later.

Joined: 18 Jul 2003
Location: Idaho Falls, ID

Code: Select all
`x = NewMax * n/OldMax`

What would n be?

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

The value to convert. In sticking with the old example, converting from 0-320 to 0.0-1.0:
Code: Select all
`x = NewMax * n/OldMaxn = 160  //Convert 160 to 0.0-1.0 rangeNewMax = 1.0OldMax = 320x = 1.0 * 160/320x = 1.0 * 0.5x = 0.5`

While there are some obvious shortcuts which could be made in this particular scenario, the point of HotWax's formula is to be general purpose; you should be able to convert from any one range to any other with it.
Code: Select all
`x = NewMax * n/OldMaxn = 20  //0-32 range, going to new 0-10 rangeNewMax = 10OldMax = 32x = 10 * 20/32x = 10 * 0.625x = 6.25`

EDIT: Zero based ranges, that is. If you wanted an arbitrary base, just shift the result value by the minimum (e.g. to convert to a range of 8-18, just convert to range 0-10, then add 8.)

Zippy
Scathing political commentary!

Joined: 23 Mar 2005
Location: Rochester

Nash wrote:
Code: Select all
`x = NewMax * n/OldMax`

What would n be?

I guess I could have been more consistent.

Here you go:

NewVal = NewMax * OldVal/OldMax

HotWax
Do what you must, and pay the price later.

Joined: 18 Jul 2003
Location: Idaho Falls, ID

### Re: [math] Formula for scaling numbers?

Sorry for the bump, but I need help. I've been trying to pull something off for the last 3 hours but I just can't figure out the math.

I know the value of the player's current EXP, the max EXP, and the EXP required to the next level. Example...

Code: Select all
`EXP: 1/300EXP to next level: 299 // found by using EXP To Next Level - Current EXP`

Now using SBARINFO, I'm trying to draw an EXP progress bar similar to Diablo 2. It's basically a percentage bar, from 0 to 100, that shows your experience progress for the current level.

The math that I'm trying to figure out now is, for each new level, how to properly draw this bar. So if the player levels up...

Code: Select all
`EXP: 300/600EXP to next level: 300`

... I want to convert the bar's range (0 to 100) to the range of the EXP to next level (300 in the example's case).

Help. :(

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

### Re: [math] Formula for scaling numbers?

I have a "hack". Use a new ammo bin or a new inventory, and dynamically set the capacity. The joy of the DrawBar is that it updates when it does receive a new capacity.
For each level, empty the bin, set a new capacity, based on ONLY from one level to the next. There is a slight disadvantage, and that is that it will cap off, but a little ACS to correct it shouldn't be much of a problem, right?

Project Dark Fox
Married Furry Anxiety Bucket

Joined: 14 Jul 2005
Location: Louisville, KY USA

### Re: [math] Formula for scaling numbers?

I... still don't get it. Formula, please?

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

### Re: [math] Formula for scaling numbers?

(Total accumulated XP for all previous levels) = Bottom End

(Distance from that point to next) = Top End

It's hard to print a formula from here for me, I've really lost my math edge over the years. However I hope that helps slightly.

Project Dark Fox
Married Furry Anxiety Bucket

Joined: 14 Jul 2005
Location: Louisville, KY USA

### Re: [math] Formula for scaling numbers?

Nash wrote:I... still don't get it. Formula, please?
HotWax's equation should work fine. I'll try to explain it better --

x = y * n/z

y, or "NewMax" is the new maximum value that you want to range to be. In your case 320.
z, or "OldMax" is the old maximum value of the range. In your case, 1.
n is the value you want to convert. Lets use 0.5 in this example:

x = 320 * 0.5/1

x would equal 160 in this case.

MasterOFDeath
professional lurking witch

Joined: 03 Apr 2004

### Re: [math] Formula for scaling numbers?

Project Dark Fox wrote:(Total accumulated XP for all previous levels) = Bottom End

(Distance from that point to next) = Top End

Okay, I know how to retrieve the bottom end, but what I'm having trouble is, how to convert that bottom end to become the new 0? (Because the new range is from 0 to 100 for the percentage bar)

EDIT for clarity: In my current situation, I have a new minimum and maximum so I don't know how to apply the formula Hotwax gave me.

Example: I want to scale [350 - 800] to [0 - 100]. I want an expression that I can use to do that. Hope my question is clear now...

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

### Re: [math] Formula for scaling numbers?

Anyone? I'm getting really desperate here because I believe this is something really simple to do and I don't need to lose 2 nights of sleep trying to figure this out. :(

There has to be a single expression I can use for this, instead of doing it like how PDF suggested because his way is just too messy and requires a lot of questionable steps...

Nash
Nash Muhandes

Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia

Next