PDA

View Full Version : any math genius out there.


Rappa
07-31-2006, 07:17 PM
Ive been assigned to put this foruma into an application and get a result.

__________________________1
r1 + K * [ w - ------------------------------ ]
_________________1 + 10 ^ ((r2-r1)/400)

thats the forumla, its a rating system for the world chess club or ICC.

the code ive written so far in c# is,

int rating1 = int.Parse(yourrating.Text);
int rating2 = int.Parse(opponentsrating.Text);
int w = 1;
int k = 32;
int problemone = rating1 + k;
int top = 1;
int bottomleft = 1 + 10;
int ratingdiff = rating2 - rating1;
int a = 400;
int bottomright = ((ratingdiff) / a);
int bottom = bottomleft ^ bottomright;
int fraction = w - ((top) / (bottom));
int result = problemone * (fraction);
int total = result - rating1;

//print result
total1.Text = result.ToString();
//print gained
gained.Text = total.ToString();

now this code should print me a result from 0-32, 32 being the most. but all it does is add 32 to the input, which is totally wrong. i dont know if its one of my parenthesis or somthing but ive just about given up.

heres the link to the rating page:

http://www.chessclub.com/help/ratings

im not asking u to do my work, just if you have any spare time and want a challenge to try and help me with this.

thanks~


ps: the ^ character is 'to the power of' right?

Aradon
07-31-2006, 10:57 PM
Well, your formula is misleading. The actual formula is..

r1 + k*[ w- 1 / (1 + 10 ^ ((r2-r1)/400)) ]

So it looks like you have an order of operations problem first off.


You should be doing it in this order:

r2-r1
divide that by 400
take that number and make it 10 to the power of it.
add one to it.
Put it over one.
Take w and subtract that number
Muliply that result by k
and add ranking one.


looks like you're almost doing it backwards in the code.

Hope that helps!

Rappa
08-01-2006, 04:26 AM
wow man i appreciate it so much. im going to try that. thanks again!

Rappa
08-01-2006, 04:43 AM
o well still gives me the same result but thanks for trying anyway dude.

heres my new code:

int rating1 = int.Parse(yourrating.Text);
int rating2 = int.Parse(opponentsrating.Text);
int ratingdifference = rating2 - rating1;
int a = 400;
int b = 10;
int c = 1;
int w = 1;
int k = 32;
int ratingdividedby400 = ratingdifference / a;
int tothepowerof10 = b ^ ratingdividedby400;
int add1toit = c + tothepowerof10;
int putitover1 = c / add1toit;
int subtracttow = w - putitover1;
int multiplybyk = subtracttow * k;
int result = multiplybyk + rating1;
int total = result - rating1;

paulq
08-01-2006, 03:39 PM
What language are you using? The ^ in C++\C# is the XOR operator NOT "to the power of". You'll need to use a function to raise it to the power of something like the function pow() in C++.

Aradon
08-01-2006, 03:42 PM
What language are you using? The ^ in C++ is the XOR operator NOT "to the power of". You'll need to use a function to raise it to the power of something like the function pow() in C++.


Doh, that's right. Too much calculator work instead of programming work with math.

Rappa
08-01-2006, 05:02 PM
ooooh so whats the to the power of symbol or function in c#?

Brandoe85
08-01-2006, 05:09 PM
There isn't one ;) Like mentiond, use Math.Pow():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemmathmemberstopic.asp
Good luck;

rpgfan3233
08-01-2006, 09:26 PM
AFAIK, the only common language in which the ^ operator is the exponentiation operator is BASIC (and its various derivatives), though I'm not sure about certain languages such as Befunge or LISP.

Languages such as C, C++, C# or Java use some form of a pow() function:

C/C++ - include the <math.h>/<cmath> header and then use the function pow(base, exponent)
Java - Math.pow(base, exponent)
C# - same as Java except that "pow" is "Pow" (everything in C# seems to be title case)

Rappa
08-02-2006, 04:04 AM
ok so i did that now im getting errors that im fixing but wont go away.

double tothepowerof10 = Math.Pow(b, ratingdividedby400);
int add1toit = c + int.Parse(tothepowerof10);

the underlined part gives me 'can not implicitly convert type double to string' well im trying to parse it to an interger so it can go back into an equation.

nothing im trying works.

i need the double to turn back to a normal interger. i thought thats what int.parse was.

heres my whole code again:

int rating1 = int.Parse(yourrating.Text);
int rating2 = int.Parse(opponentsrating.Text);
int ratingdifference = rating2 - rating1;
int a = 400;
int b = 10;
int c = 1;
int w = 1;
int k = 32;
int ratingdividedby400 = ratingdifference / a;
double tothepowerof10 = Math.Pow(b, ratingdividedby400);
int add1toit = c + int.Parse(tothepowerof10);
int putitover1 = c / add1toit;
int subtracttow = w - putitover1;
int multiplybyk = k * subtracttow;
int result = rating1 + multiplybyk;
int total = result - rating1;

//print result
total1.Text = result.ToString();
//print gained
gained.Text = total.ToString();

thanks again guys

rpgfan3233
08-02-2006, 04:21 AM
I believe you need to use tothepowerof10.ToString() because Parse requires a string variable.

Rappa
08-02-2006, 04:33 AM
ok i got a new error,

cannot implicitly convert type double to int, an explicit conversion exists (are you missing a cast?)

i have no idea what that means, im a total novice.

int rating1 = int.Parse(yourrating.Text);
int rating2 = int.Parse(opponentsrating.Text);
int ratingdifference = rating2 - rating1;
int a = 400;
int b = 10;
int c = 1;
int w = 1;
int k = 32;
int ratingdividedby400 = ratingdifference / a;
double tothepowerof10 = Math.Pow(b, ratingdividedby400);
int add1toit = c + tothepowerof10;
int putitover1 = c / add1toit;
int subtracttow = w - putitover1;
int multiplybyk = k * subtracttow;
int result = rating1 + multiplybyk;
int total = result - rating1;

Brandoe85
08-02-2006, 04:44 AM
You're trying to assign a double value to an int. tothepowerof10 is a double value. Make sense?

Rappa
08-02-2006, 04:50 AM
right but how to i turn the double into an interger to be used in the rest of the equation?

Brandoe85
08-02-2006, 04:53 AM
I was too subtle, type Convert. And you should see some options in your IDE, preferably integers. (int16, int32 etc).

Convert.ToInt32(val)

Rappa
08-02-2006, 05:00 AM
im about to give up on this whole project

the out put for this equation should be a number between 1 and 32 depending on the respected inputs by the user.

with this code:

int rating1 = int.Parse(yourrating.Text);
int rating2 = int.Parse(opponentsrating.Text);
int ratingdifference = rating2 - rating1;
int a = 400;
int b = 10;
int c = 1;
int w = 1;
int k = 32;
int ratingdividedby400 = ratingdifference / a;
double tothepowerof10 = Math.Pow(b, ratingdividedby400);
int add1toit = c + Convert.ToInt32(tothepowerof10);
int putitover1 = c / add1toit;
int subtracttow = w - putitover1;
int multiplybyk = k * subtracttow;
int result = rating1 + multiplybyk;
int total = result - rating1;

//print result
total1.Text = result.ToString();
//print gained
gained.Text = total.ToString();


im either getting 0 or 32. frustrating

i unno what to do

paulq
08-03-2006, 04:45 PM
Ok, I don't know what you are doing to solve your probem but the following are things I do when trying to code an algorithm.

First, find very simple input that you know the answer to when inputted into the function. (A + B = C)

If possible, I calculate the sample using pencil and paper before writing any code. Your algorithm is simple enough that anyone who passed 6th grade should be able to do this on pencil and paper. Do it in steps so you know that after each step what the answer should be at that point. If you can't get a consistent and correct answer doing it the traditional way, you'll never be able to code it.

If A + B = C Good, start coding it!
If A + B = Q BAD recheck your math.)


Use debugging breakpoints when coding, trace each line of code to test each variable individually at that point in your code.


X + Y = A
R + S = B
A + B = C
//Good, works fine.

X + Y = A
R + T = D // Error, not what you expected, all wrong from here on
A + D = Q
// Results in incorrect output


This would have quickly caught the first problem you had when you were using the "exclusive or" operator instead of the pow() function because you would have known immediately that the line int tothepowerof10 = b ^ ratingdividedby400;was not giving the result you got when you did it out by hand.

Like I said, if you have done all this, don't think that I am insulting you. It is just that, short of writing the algorithm for you, this is the only advice I can give.

Rappa
08-03-2006, 10:13 PM
sorry ive only been doing C# for about a week and trying to make an application for a problem like this: r1 + k*[ w- 1 / (1 + 10 ^ ((r2-r1)/400)) ]
is completlet out of my league. so im just trying my hardest.

paulq
08-04-2006, 03:28 PM
No way dude, that's a simple formula. If you can do it the traditional way, then you can code it. Don't give up. The best thing you could do is exactly what you have done. You're using a forum to ask intelligent questions in order to accomplish your task. If you don't know how to use the built in debugger, let us know. It can be really simple to use and offers great advantages when coding. If you've just started with C# then it seems like you've already learned a lot. So once again, don't give up!

Aradon
08-04-2006, 05:58 PM
sorry ive only been doing C# for about a week and trying to make an application for a problem like this: r1 + k*[ w- 1 / (1 + 10 ^ ((r2-r1)/400)) ]
is completlet out of my league. so im just trying my hardest.

Whenever I face a problem that looks difficult the first thing I do is think about how I would do it without a computer. The steps that are involved. So for a math formula, how would you do it by hand? Or on paper?

Once you figure that out, it's just taking that procedure and putting it into a language that the computer can understand.

And then once THAT'S figured out, you can optimize it as needed.

You're not out of your league. The best part of programming is taking a problem, figuring out the best way to do it, and then doing it. Cause once it's done..it's about the best feeling ever.

Rappa
08-04-2006, 11:11 PM
i dont know how to use the built in debugger. would it help?

rpgfan3233
08-04-2006, 11:16 PM
The best part of programming is taking a problem, figuring out the best way to do it, and then doing it. Cause once it's done..it's about the best feeling ever.
I agree. Imagine doing a giant application that takes well over 10 years (we're talking Fedora Core size, which is approximately 4 discs (CD, not DVD)). Once you are finished and everything works, the feeling is wonderful.

The more difficult the road, the greater the achievement.