# how to use built-in pow() function correctly

we all know that the pow() function returns a floating point value.Sometimes it may cause a problem if i take it as integer.How can i fix the problem?Or do i always need a floating point for the return value?

I suppose youâ€™re talking about both base and exponent being integers. In that case it is safer to write your own integer power function, e.g. tutorial on fast modulo multiplication.

Still, in most cases you should be fine using

``````int result = int(pow(2, 3) + 0.5);
``````

Usually problem gets solved using typecasting, and try using your own functions instead of power for the calculation of anything like a^b. The code goes this manner:

``````template <typename T>
T calcpow(T base, T exp)
{
T temp;
if(exp==0)
return 1;
if(exp==1)
return base;
temp= calcpow(base, exp/2);
if(exp%2==0)
return temp*temp;
else
return base*temp*temp;
}
``````

Usually calculating powers using this avoids any overflow or exception kind of problems associated with the in-built ones.