## As: Math - Exponential Functions

• 3,373 Views
• 25 Replies
Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
As: Math - Exponential Functions 2006-01-09 13:49:47

Math - Exponential Functions

/* This is my first tutorial, so I'll start with something simple */

What are Exponential Functions?

Most people can tell you that a gradient of a line is the difference vertically between two points on the line, divided by the difference horizontally between those two points, and can also be thought of as the rate of change of the line. However, this value is not always a constant number. In the case of curves, the rate of change of the curve is also changing, the gradient of the line also having a gradient of it's own. The Mathematician Euler found a number (approximately 2.718), which when raised to the power x has a self gradient, meaning the gradient of the curve is the same equation as the curve itself: e^x. An exponential function is any function that uses this magic constant to define it's structure.

What use are Exponential Functions?

Euler's number is found naturally in many things such as the half-life of radioactive atoms, and the current discharge from a capacitor. It can also be used to calculate a strong level-up calculator for a Role-Playing Game.

The AS Function

Math.exp(x)

This function raises Euler's Constant to a power x and returns the result. Understanding how the number works can make a smooth level-up curve without you having to do much work at all.

Implementing The Function

Let's try the RPG example.

Choose a step length, this will be your x value. For this purpose, I am choosing 1/2 as my step length. This will determine how far horizontally along the curve the next level will be to reach (as an addition to what has already been attained). In a real example, experience points will probably be in the hundreds or thousands, so the required experience to get to a certain level from a previous one we will assume to be 1000*Math.exp((x-1)/2). Why x-1? So that the first level will be e^0, which will yield 1000. A nice round number to scale every other level.

We can create an array to hold the values of experience needed to level up, using iterative code. Remember these values are the extra experience needed to reach the next level, not the total. Let's generate 20:

A_ExpNeeded = new Array();
for (i=1;i<20;i++) {
A_ExpNeeded[i] = Math.round(1000*Math.exp((i-1)/2));
}

Believe it or not, this is the most efficient way to create a level curve which is in a perfect balance of fairness to the user. Using different step lengths and different multipliers you can scale it to however you wish. If you want more, easier levels then divide x by an even larger number. Through experimentation you can easily find a system people will love, and everybody can enjoy the magic of Euler's number.

/* I may follow this up with another tutorial on more complex examples of exponential curves, depending on how this is appreciated/*

~Sekky~

authorblues
authorblues
• Member since: Jun. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:01:44

and you closed your comment tag wrong. its /* comment */

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:04:17

At 1/9/06 02:01 PM, authorblues wrote: and you closed your comment tag wrong. its /* comment */

I realised that after I posted

Because it's an important part of Mathematics. If that's how people treat this I may as well not fucking bother trying.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:05:22

Nice one, heres a visual aid for people wanting to create surface through the use of an exponential function, typical curves here

y = x²
bucket shaped, can be flipped by y = -x²

y = x³
curves upward to a point of inflection, then up again

y = 1/x
cuves towards the axis at asymtotes, and heads on towards infinity, never touches axes.

y = x^4 and x^5 are just more stretched versions of the basic x squared and cubed.

curves can be shifted by adding values onto the end of the equation, after affecting x

they can be stretched vertically by a scale factor or R by
y = R*f(x)

they can be stretched horizontally by a scale factor or 1/R by:

y = f(R*x)

f(x) being the means to which you are affecting the X value (so squared, cubed etc)

Might be useful to those using complex API terrain, I just needed the maths revision :D

Denvish
Denvish
• Member since: Apr. 25, 2003
• Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:07:05

Maths fries my brain. Maybe one day I'll be able to make use of this, but I doubt it...

Needs a linkback to AS: Main

- - Flash - Music - Images - -

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:07:12

At 1/9/06 02:05 PM, TimHeasman wrote: Nice one, heres a visual aid for people wanting to create surface through the use of an exponential function, typical curves here

Thanks for the good word, but those aren't exponential functions, they're polynomials.

authorblues
authorblues
• Member since: Jun. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:10:38

At 1/9/06 02:04 PM, Sekky wrote: Because it's an important part of Mathematics. If that's how people treat this I may as well not fucking bother trying.

its just that AS threads are meant to cover broader topics. this is a bit too specific for a thread. had you covered it in the way that T-H just covered, that would have made up for it, but its one function that is very rarely used.

the Math.pow() function is incredibly inefficient. most programmers never need to do more than square or cube a number, which can be covered with num*num or num*num*num. that is more efficient because the Math.pow() function goes thru a complex algorithm to solve exponential equations.

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:12:05

At 1/9/06 02:10 PM, authorblues wrote: the Math.pow() function is incredibly inefficient. most programmers never need to do more than square or cube a number, which can be covered with num*num or num*num*num. that is more efficient because the Math.pow() function goes thru a complex algorithm to solve exponential equations.

As I just said, that is NOT an exponential function, it's a polynomial. There's a distinct difference.

Polynomials take the form X^C, where C is a constant and X is a variable. Exponentials take teh form e^X, where e is a self derivative and X is variable.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:15:18

At 1/9/06 02:07 PM, Sekky wrote: Thanks for the good word, but those aren't exponential functions, they're polynomials.

You'll have to excuse me, I've not done too much with natural logarithms and such, my current understanding is that an exponential function is simply a function that grows at an ever increasing rate (x²?)

Ill wikipedia it lol. brb

authorblues
authorblues
• Member since: Jun. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:24:35

At 1/9/06 02:12 PM, Sekky wrote: As I just said, that is NOT an exponential function, it's a polynomial. There's a distinct difference. Polynomials take the form X^C, where C is a constant and X is a variable. Exponentials take teh form e^X, where e is a self derivative and X is variable.

so you can do a recursive multiplication. if you thought for three seconds, you would realize that Math.exp(num) is just a modification as Math.pow(2.71828, num); that uses the Math.pow() function nonetheless. a better way would be to do it like this.

Math.prototype.intPow = function(num:Number, pwr:Number):Number{
var tempRtn:Number = num;
for (var i=1; i<pwr; i++){
tempRtn *= num;
}
return tempRtn;
}

surely a shorter algorithm that way...

Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Math - Exponential Functions 2006-01-09 14:26:50

wow the example was awesome

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:31:46

At 1/9/06 02:24 PM, authorblues wrote: so you can do a recursive multiplication

Is this topic titled "As: Numerical Methods: Recursion"? I was explaining the exponential constant to programmers with little mathematical knowledge. Fuck off.

Denvish
Denvish
• Member since: Apr. 25, 2003
• Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 14:53:09

At 1/9/06 02:31 PM, Sekky wrote:
At 1/9/06 02:24 PM, authorblues wrote: so you can do a recursive multiplication
Is this topic titled "As: Numerical Methods: Recursion"? I was explaining the exponential constant to programmers with little mathematical knowledge. Fuck off.

Haha, still a master of tact then... you and fwe should get together sometime.

- - Flash - Music - Images - -

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 16:11:05

At 1/9/06 02:53 PM, Denvish wrote: Haha, still a master of tact then... you and fwe should get together sometime.

fwe? another cynic is he?

Denvish
Denvish
• Member since: Apr. 25, 2003
• Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 16:13:40

At 1/9/06 04:11 PM, Sekky wrote:
At 1/9/06 02:53 PM, Denvish wrote: Haha, still a master of tact then... you and fwe should get together sometime.
fwe? another cynic is he?

Let's just say he has a talent for putting peoples' noses out of joint ;)

- - Flash - Music - Images - -

Phyruss
Phyruss
• Member since: Dec. 29, 2005
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 16:21:37

sounds like a nice bloke

authorblues
authorblues
• Member since: Jun. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 16:46:10

At 1/9/06 04:13 PM, Denvish wrote: Let's just say he has a talent for putting peoples' noses out of joint ;)

Xelius
Xelius
• Member since: Jan. 7, 2005
• Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to As: Math - Exponential Functions 2006-01-09 17:22:05

Thanks for this one! Very well explained and good examples, keep e'm comming.

Glaiel-Gamer
Glaiel-Gamer
• Member since: Dec. 28, 2004
• Offline.
Forum Stats
Member
Level 28
Game Developer
Response to As: Math - Exponential Functions 2006-01-09 17:43:03

At 1/9/06 02:24 PM, authorblues wrote: so you can do a recursive multiplication. if you thought for three seconds, you would realize that Math.exp(num) is just a modification as Math.pow(2.71828, num); that uses the Math.pow() function nonetheless. a better way would be to do it like this.

so tell me usind your method what is 2^-23.4572

Sekky
Sekky
• Member since: Mar. 17, 2003
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 07:54:46

At 1/9/06 05:43 PM, Glaiel_Gamer wrote: so tell me usind your method what is 2^-23.4572

Nicely said.

It could be approximated via a few expansions I can think of, but it can't be done via recursion.

You = ownage.

Rantzien
Rantzien
• Member since: Jan. 27, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 08:16:53

As far as level up curves go, I'd rather use something like:

exp relation between this level and next level = exp relation between last level and this level * 1.15

That provides a satisfying result for me. But I can see some other uses for this. Still though, I'm not much of a mathematician, so I'll stick with methods I can understand better.

But thanks for showing it to me ;)

dELtaluca
dELtaluca
• Member since: Apr. 16, 2004
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 10:51:52

At 1/9/06 05:43 PM, Glaiel_Gamer wrote: so tell me usind your method what is 2^-23.4572

Math.pow(2,-23.4572) = 8.68318763334859*10^-8

dELtaluca
dELtaluca
• Member since: Apr. 16, 2004
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 10:53:05

At 1/10/06 10:51 AM, -dELta- wrote:
At 1/9/06 05:43 PM, Glaiel_Gamer wrote: so tell me usind your method what is 2^-23.4572
Math.pow(2,-23.4572) = 8.68318763334859*10^-8

oh wait nevermind, i forgot what he had posted :p i thought you were trying to say you cant do it using Math.pow :p discard everything ive said and carry on

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 11:08:24

At 1/9/06 05:43 PM, Glaiel_Gamer wrote:
At 1/9/06 02:24 PM, authorblues wrote: so you can do a recursive multiplication. if you thought for three seconds, you would realize that Math.exp(num) is just a modification as Math.pow(2.71828, num); that uses the Math.pow() function nonetheless. a better way would be to do it like this.
so tell me usind your method what is 2^-23.4572

you could break it into two, let's call authorblues's fullPow, you can calculate it to the 23rd power, and then use a none-recursive part to calculate the rest

2^-23.4572 is equal to :
(2^-1) ^ 34.4572 which is equal to
.5 ^ 34 * .5^.4572
let's call .5 ^ 34 T since it's calculabe through authorblues's algorithem
so you're left with .5^.4572 * T
I don't have my calculator here, but as long as it's rational, you can break that into a fraction, and since a number less than one in the fraction field is a root, that can be calculated in a reccursive way. if it's irrational, you can break it into a root and the number in the power of an irrational number, which you do need a calculator for, but it's still a lot easier than the whole thing to calculate, you can also round it up to something rational and estimate, that's what the computer does anyway.

T-H
T-H
• Member since: Jan. 7, 2004
• Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Math - Exponential Functions 2006-01-10 11:27:21

At 1/9/06 05:43 PM, Glaiel_Gamer wrote: so tell me usind your method what is 2^-23.4572

was one of his points not - why the fuck would you want to know that in the first place?

no argument though.

Newsdee
Newsdee
• Member since: Jan. 21, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Math - Exponential Functions 2006-05-29 08:15:06

Great example, I'll definitely be using this.
I like nice round numbers for XP though, so here's a little rounding to get nice values:
function expForLevel(x:Number):Number {
var base:Number = Math.floor(1000*Math.exp((x-1)/2));
return Math.ceil(base / 500) * 500;
}
for (var i:Number = 1; i<11; i++) {
trace(expForLevel(i));
}