在我们写代码时,会经常要求幂运算。虽说不是很复杂,但是有又快又省事的库函数为啥不用呢?
函数原型:double pow(double x, double y)
函数功能:x的y次方
头文件:#include <cmath >或 #include <math.h>
示例:
#include <stdio.h>
#include <cmath> //或#include <math.h>
int main()
{
printf("%d",(int)pow(2,3));
return 0;
}
ps: 因为pow( )函数的返回值为double类型,故printf打印格式为%f或%lf,或者像示例一样先转为int类型,否则将输出0。
参考博客:https://blog.csdn.net/snowcatvia/article/details/96639110