[math] Formula for scaling numbers?

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
User avatar
Project Shadowcat
Posts: 9369
Joined: Thu Jul 14, 2005 8:33 pm
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Blacksburg, SC USA
Contact:

Re: [math] Formula for scaling numbers?

Post by Project Shadowcat »

Nash wrote: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...
Yeah, I wasn't expecting it to be a 100% fool-proof solution, only a stepping stone. :?
User avatar
Zippy
Posts: 3302
Joined: Wed Mar 23, 2005 5:31 pm
Location: New Jersey

Re: [math] Formula for scaling numbers?

Post by Zippy »

The equation HotWax gave way back when works fine, which just a minor modification to account for the non-zero based offset.

Code: Select all

NewVal = NewMax * (OldVal - OldMin) / (OldMax - OldMin)
For example:
xample: I want to scale [350 - 800] to [0 - 100].
So, let's take this example. Say the user has 400 exp total. The min of the range is 350 and the max is 800. You want the value between 0 - 100 that the player is at, right? So...

Code: Select all

NewVal = 100 * (400 - 350) / (800 - 350)
NewVal = 100 * 50/450
NewVal = 100 * 0.1111111...
NewVal = 11.111111...
NewVal = 11  // If rounding
User avatar
Nash
 
 
Posts: 17433
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

Post by Nash »

Thanks Zippy. I appreciate your help. However there's one small problem... the script bombs out with a divide by zero error when the player is at level 1, because he has 0 experience. How do I get around this?

Here's the expression I use (printed first for testing purposes... once I get the expression right, I will know what to do with the result).

Code: Select all


	print(d: 100 * (CheckInventory("Experience") - player_expmin[PlayerNumber()]) / (player_expnext[PlayerNumber()] - player_expmin[PlayerNumber()]));

Player's current experience is recorded through an inventory class, "Experience".

player_expmin[] records what the current level's minimum experience is.

player_expnext[] records the experience required for the next level - the maximum.

EDIT:

Turns out I was calculating player_expmin[] incorrectly.

Thanks so much Zippy. I would have never been able to figure it out on my own even if I read this thread a million times.

Now to get some sleep and hopefully not show up late for work tomorrow... @_@
User avatar
Unknown_Assassin
Posts: 2468
Joined: Wed Apr 12, 2006 5:17 pm
Location: Where dead carcasses lie
Contact:

Re: [math] Formula for scaling numbers?

Post by Unknown_Assassin »

Nash wrote:the script bombs out with a divide by zero error when the player is at level 1, because he has 0 experience. How do I get around this?
You could make the player have 1 experience at level 1. That way, you won't have the divide by zero error.
User avatar
Nash
 
 
Posts: 17433
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: [math] Formula for scaling numbers?

Post by Nash »

No, UA, that wasn't the problem. I was just calculcating the player's min exp incorrectly, which just screws up the math formula Zippy provided.

(I found out because I did try making the player start with 1 exp but the script still bombed out)
Locked

Return to “Editing (Archive)”