This is due to the inexact nature of floating point. There are not enough significant digits in a double
to express that number exactly since numbers are stored internally in binary.
For example, if you run this:
printf("result=%.10e\n", pow(.1, -120));
printf("result=%.20e\n", pow(.1, -120));
printf("result=%f\n", pow(.1, -120));
printf("10^120=%.10e\n", 1e120);
printf("10^120=%.20e\n", 1e120);
printf("10^120=%f\n", 1e120);
You'll get this:
result=1.0000000000e+120
result=9.99999999999993386195e+119
result=999999999999993386194947375938605300558731199397053728064304453022541669059515488061520680536169238451435883569728192512.000000
10^120=1.0000000000e+120
10^120=9.99999999999999980003e+119
10^120=999999999999999980003468347394201181668805192897008518188648311830772414627428725464789434929992439754776075181077037056.000000
The reason pow(.1, -120)
is slightly different than 1e120
is that .1
cannot be represented exactly either, so the error magnifies with each iteration of the power.
If on the other hand you printed the result of pow(2, 350)
, you would get an exact answer.