math - Why does this C computation involving exponents produce the wrong answer? -


this question has answer here:

i trying implement below formula on c

enter image description here

here code:

int function(int x){    return pow(10, (((x-1)/(253/3))-1)); }  int main(void){   int z = function(252);   printf("z: %d\n",z);    return 0; } 

it outputs 10. calculator outputs 94.6.

could explain me doing wrong?

note in line

(((x-1)/(253/3))-1)) 

you dividing integer value x - 1 integer value 253 / 3. truncate value int, meaning you'll raising integer power integer power.

to fix this, try changing expression to

(((x-1)/(253.0 / 3.0))-1)) 

this use doubles in expression, giving value want.

hope helps!


Comments