int n;
n = 2;
int d;
d = pow(10,n);
cout<<d<<endl;
It displays 99 instead of 100. I have indcluded math.h
Quite possibly a rounding issue.
Some implementations of std::pow
have special cases for integral arguments. Others just rely on basic math. For instance, pow(x,y)
== pow(2, y*log2(x))
. The typical reason why an implementation would use this rule is it represents floating-point numbers internally in base 2. But log2(x)
may suffer from rounding issues.
In your case, log2(10)
would be 3.3219280948873623478703194294894
. That means that pow(10.0, 2.0)
can be calculated behind the scenes as pow2(6.6438561897747246957406388589788)
= 64 * pow2(0.6438561897747246957406388589788)
. It's quite possible that the latter term is rounded down, to a value just below the correct 1.5625
.