This is caused by a deficient implementation of pow
. Although the correct result, 625, is representable, it is returning 624.9999999999998863131622783839702606201171875.
pow
is a difficult function to implement well, and some implementations do not return correct results even when the exact real-number-arithmetic result is representable in the double
format. The function is implemented using approximations, and sometimes those approximations produce inaccurate results.
If you change the code to:
double power = pow(base, exponent);
printf("%g\n", power);
printf("%.99g\n", power);
you will see output like:
625
624.9999999999998863131622783839702606201171875
The first line shows the result rounded to six significant decimal digits (the default for a g
conversion) and then trimmed of trailing zeros. The second line shows the exact value. (Some C implementations may also fail to show the exact value in this line, but they should show you enough to distinguish it from 625.)
When 624.9999999999998863131622783839702606201171875 is converted to int
, the fraction part is discarded, leaving 624. This explains the output you observed.