I have a problem with pow function in c. The variable Qb will give wrong output - it gives 10000 instead of 177827.941004 resulting in final output of 2007 instead of 2009
Compile command is by.
gcc -ggdb -std=c99 -Wall -Werror -o test_pow02 test_pow02.c -lm
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int my_rating = 2000;
int opponent_rating = 2100;
int coeff = 15;
int score = 1;
int new_rating;
double Qa = pow(10, my_rating / 400); // 100000
double Qb = pow(10, opponent_rating / 400); // 177827.941004
double Ea = Qa / (Qa + Qb);
new_rating = my_rating + ( (score - Ea) * coeff );
printf("Qa is %g\n", Qa);
printf("Qb is %g\n", Qb);
printf("New Rating is %d\n", new_rating);
return 0;
}
But if I hardcoded 2100/400 into 5.25, it will work correctly. I already have -lm at the end. How do I fix this?
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int my_rating = 2000;
//int opponent_rating = 2100;
int coeff = 15;
int score = 1;
int new_rating;
double Qa = pow(10, my_rating / 400); // 100000
double Qb = pow(10, 5.25); // 177827.941004
double Ea = Qa / (Qa + Qb);
new_rating = my_rating + ( (score - Ea) * coeff );
printf("Qa is %g\n", Qa);
printf("Qb is %g\n", Qb);
printf("New Rating is %d\n", new_rating);
return 0;
}
The operation pow(10, opponent_rating / 400);
does not equal to 177827.941004
.
opponent_rating / 400 == 5
Operations involving two integers will produce an integer,
in this case the result 5.25
gets clipped to 5
You should divide with a double to get a double result opponent_rating / 400.0
that is used in the pow function.