## Help me with some equations, for i am retarded

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

Gunfingers
Posts: 2401
Joined: Wed May 30, 2007 7:15 pm UTC

### Help me with some equations, for i am retarded

I was trying to work out the damage calculation in a game i play and hit a road block. See, i'm pretty sure they're using a normal distribution to determine where in your damage range your are most likely to fall.

For example, if your "balance" stat is 65% and your damage range is 10-20, then you are most likely to do 17 damage (65% of the way between 10 and 20).

The roadblock is that i don't know how to calculate a normal distribution curve. I found the algorithm on wikipedia.

but was forced to remember that despite being a CS major i don't know shit about maths. To date the most advanced math course i've taken was basic statistics. At present, my equation (with mu hardcoded as 7 and sigma hardcoded as 1) looks like this.

Code: Select all

double equation (int x){    int exponent = -1 * (((x - 7) ^ 2) / 2 * (1 ^ 2));    int blah = 1 / (1 * sqrt(2 * 3.14));    return blah ^ exponent;}

But it doesn't work. Is it because i oversimplified pi?

Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Help me with some equations, for i am retarded

Um, is this in the C family? If so, the ^ operator doesn't do what you think it does.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Tac-Tics
Posts: 536
Joined: Thu Sep 13, 2007 7:58 pm UTC

### Re: Help me with some equations, for i am retarded

Just glancing at the formula, you lost your e in translation. It *should* be.

Code: Select all

double equation (int x){    int exponent = -1 * (((x - 7) ^ 2) / 2 * (1 ^ 2));    int blah = 1 / (1 * sqrt(2 * 3.14));    return blah * 2.71 ^ exponent;}

Micron
Posts: 319
Joined: Sat Feb 16, 2008 1:03 am UTC

### Re: Help me with some equations, for i am retarded

No, that doesn't work because you're calculating a decimal value <1 and then casting it to an int ("blah"). Use an appropriate numeric type.

NathanielJ
Posts: 882
Joined: Sun Jan 13, 2008 9:04 pm UTC

### Re: Help me with some equations, for i am retarded

In addition to what others have said, watch your order of operations. You have:

Code: Select all

-1 * (((x - 7) ^ 2) / 2 * (1 ^ 2))

Put brackets around the (2 * (1 ^ 2)) part, because as it is now it will divide by 2 and then multiply that result by (1^2) (recall BEDMAS). It doesn't matter when sigma = 1, but for other sigma it will make a huge difference.

Also, there should be a built-in constant for E and Pi no matter what language you're using. Look around on Google for what they are.
Homepage: http://www.njohnston.ca
Conway's Game of Life: http://www.conwaylife.com

crazyjimbo
Posts: 887
Joined: Fri Apr 20, 2007 11:45 pm UTC
Location: Durham, England
Contact:

### Re: Help me with some equations, for i am retarded

Edit: Wow, Ninja'd 4 times. If the result looks nothing like a normal distribution then Xanthir looks like he has the answer.

Gunfingers wrote:

Code: Select all

double equation (int x){    int exponent = -1 * (((x - 7) ^ 2) / 2 * (1 ^ 2));    int blah = 1 / (1 * sqrt(2 * 3.14));    return blah ^ exponent;}

But it doesn't work. Is it because i oversimplified pi?

You are currently calculating $\left( \frac{1}{1 . \sqrt{2 \pi}} \right)^{- \frac{(x - 7) ^ 2}{2 \times 1 ^ 2}}$

You have no exponential function (ex) in your calculation.
Last edited by crazyjimbo on Fri Apr 25, 2008 10:57 pm UTC, edited 1 time in total.

Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Help me with some equations, for i am retarded

Micron wrote:No, that doesn't work because you're calculating a decimal value <1 and then casting it to an int ("blah"). Use an appropriate numeric type.

For that matter, exponent is also generally a real number, and must be stored as a float or double.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

### Re: Help me with some equations, for i am retarded

Include <cmath>.

Code: Select all

double fun(double x, double mu, double sigma){   return exp(-pow(x-mu,2)/(2*pow(sigma,2)))/(sigma*sqrt(2*M_PI));}

Gunfingers
Posts: 2401
Joined: Wed May 30, 2007 7:15 pm UTC

### Re: Help me with some equations, for i am retarded

Okay, for one, you have no idea how annoyed i was when i was told i had tried to use the XOR operator for exponents. I'm pretty sure i would have never realized i'd done that, too. Is there even any language where that works? Because i can't think of one. Damn i'm stupid.

Anyway, i've implemented dingo's algorithm and got this output

Code: Select all

f(0)1.48672e-006f(1)0.00013383f(2)0.00443185f(3)0.053991f(4)0.241971f(5)0.398942f(6)0.241971f(7)0.053991f(8)0.00443185f(9)0.00013383f(10)1.48672e-006Press any key to continue . . .

Which, while oddly formatted, seems to follow a normal distribution. I'll just have to figure out how to best manipulate that to be meaningful to what i'm doing. I'm thinking i'll use it as a percentage.

Tac-Tics
Posts: 536
Joined: Thu Sep 13, 2007 7:58 pm UTC

### Re: Help me with some equations, for i am retarded

Gunfingers wrote:Is there even any language where that works? Because i can't think of one.

There are tons that do. Python and Haskell both have an exponentiation operator **. You just gotta be careful when translating those big math expressions because written math conveys so much in so few characters.

NathanielJ
Posts: 882
Joined: Sun Jan 13, 2008 9:04 pm UTC

### Re: Help me with some equations, for i am retarded

Gunfingers wrote:Is there even any language where that works? Because i can't think of one.

VBScript uses "^" as the exponentiation operator.
Homepage: http://www.njohnston.ca
Conway's Game of Life: http://www.conwaylife.com

Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

### Re: Help me with some equations, for i am retarded

That's scientific notation. 1.48672e-006 means 1.48672*10^-6 (i.e. 0.00000148672 (you shift the digits to the right by 6 places (but I'm sure you know that (yay for nested parentheses)))).