Forum Topic: As: Math - Exponential Functions

(2,044 views • 25 replies)

This topic is 1 page long.

<< < > >>
None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 01:49 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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~


None

authorblues

Reply To Post Reply & Quote

Posted at: 1/9/06 02:01 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

why was this necessary?
and you closed your comment tag wrong. its /* comment */

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 02:04 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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

I realised that after I posted

why was this necessary?

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


None

T-H

Reply To Post Reply & Quote

Posted at: 1/9/06 02:05 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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

As: Math - Exponential Functions


None

Denvish

Reply To Post Reply & Quote

Posted at: 1/9/06 02:07 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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 - -

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 02:07 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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.


None

authorblues

Reply To Post Reply & Quote

Posted at: 1/9/06 02:10 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 02:12 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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.


None

T-H

Reply To Post Reply & Quote

Posted at: 1/9/06 02:15 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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


None

authorblues

Reply To Post Reply & Quote

Posted at: 1/9/06 02:24 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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...

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Rustygames

Reply To Post Reply & Quote

Posted at: 1/9/06 02:26 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

wow the example was awesome

- Matt, Rustyarcade.com


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 02:31 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 1/9/06 02:53 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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 - -

BBS Signature

None

Sekky

Reply To Post Reply & Quote

Posted at: 1/9/06 04:11 PM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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?


None

Denvish

Reply To Post Reply & Quote

Posted at: 1/9/06 04:13 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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 - -

BBS Signature

None

Phyruss

Reply To Post Reply & Quote

Posted at: 1/9/06 04:21 PM

Phyruss EVIL LEVEL 07

Sign-Up: 12/29/05

Posts: 286

sounds like a nice bloke


None

authorblues

Reply To Post Reply & Quote

Posted at: 1/9/06 04:46 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

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

dont say the hurty words about fwe. hes adorable...

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Xelius

Reply To Post Reply & Quote

Posted at: 1/9/06 05:22 PM

Xelius EVIL LEVEL 21

Sign-Up: 01/07/05

Posts: 381

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


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 1/9/06 05:43 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,053

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


None

Sekky

Reply To Post Reply & Quote

Posted at: 1/10/06 07:54 AM

Sekky EVIL LEVEL 24

Sign-Up: 03/17/03

Posts: 6,167

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.


None

Rantzien

Reply To Post Reply & Quote

Posted at: 1/10/06 08:16 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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 ;)

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 1/10/06 10:51 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,548

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

My social worker says im special!

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 1/10/06 10:53 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,548

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

My social worker says im special!

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 1/10/06 11:08 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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.


None

T-H

Reply To Post Reply & Quote

Posted at: 1/10/06 11:27 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

Newsdee

Reply To Post Reply & Quote

Posted at: 5/29/06 08:15 AM

Newsdee LIGHT LEVEL 18

Sign-Up: 01/21/05

Posts: 650

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));
}


All times are Eastern Standard Time (GMT -5) | Current Time: 04:58 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!