Help me with some equations, for i am retarded

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

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

Help me with some equations, for i am retarded

Postby Gunfingers » Fri Apr 25, 2008 10:38 pm UTC

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

User avatar
Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

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

Postby Xanthir » Fri Apr 25, 2008 10:49 pm UTC

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

Postby Tac-Tics » Fri Apr 25, 2008 10:50 pm UTC

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

Postby Micron » Fri Apr 25, 2008 10:51 pm UTC

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.

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

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

Postby NathanielJ » Fri Apr 25, 2008 10:54 pm UTC

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

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

Postby crazyjimbo » Fri Apr 25, 2008 10:55 pm UTC

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 [math]\left( \frac{1}{1 . \sqrt{2 \pi}} \right)^{- \frac{(x - 7) ^ 2}{2 \times 1 ^ 2}}[/math]

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.

User avatar
Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

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

Postby Xanthir » Fri Apr 25, 2008 10:56 pm UTC

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

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

Postby Torn Apart By Dingos » Fri Apr 25, 2008 11:00 pm UTC

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

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

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

Postby Gunfingers » Fri Apr 25, 2008 11:21 pm UTC

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-006
f(1)0.00013383
f(2)0.00443185
f(3)0.053991
f(4)0.241971
f(5)0.398942
f(6)0.241971
f(7)0.053991
f(8)0.00443185
f(9)0.00013383
f(10)1.48672e-006
Press 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

Postby Tac-Tics » Fri Apr 25, 2008 11:34 pm UTC

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.

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

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

Postby NathanielJ » Fri Apr 25, 2008 11:37 pm UTC

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

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

Postby Torn Apart By Dingos » Sat Apr 26, 2008 9:28 am UTC

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


Return to “Mathematics”

Who is online

Users browsing this forum: No registered users and 10 guests