Why the result of pow(10,2) 99 instead of 100? [duplicate]
I am making a new project in c and the function pow "does not work".What can I do to fix?
It functions actually 2 to the power of 10 but 10 to the power of 1 comes out 99
#include <stdio.h>
#include <math.h>
int main(){
int res=2;
int base=10;
int exp=2;
res= pow(base,exp);
printf("%d",res);
return 0;
}
i expect the output of to be 100 but the output is 99
pow(10, 2)
yields a value slightly under 100 because it is a low-quality implementation of pow
that does not return a good result.
pow
is a difficult function to implement, and all commercial implementations produce many results that are inaccurate to some degree. However, good implementations make an effort to get certain cases exactly correct, including cases where the result is exactly an integer (or, for that matter, is rational).
In pow(base, exp)
, if exp
is known to be 2 at compile time, base*base
should be used instead—it is fast and accurate and avoids the pow
problem. If exp
is not known to be two at compile time, the code must be designed to tolerate inaccuracies in pow
or other adjustments must be made. (Further advice could be given to questions that are more specific about the circumstances or requirements.)