pow() 函数在 math.h 头文件中定义。它有助于返回第一个参数 (x) 乘以第二个参数 (y) 的值,即 xy。
double pow(double x, double y); #where x & y should be in double
pow() 函数接受一个 double 类型的参数。其中第一个参数是底数,第二个参数是幂值。使用强制转换运算符,我们可以将类型显式转换为 double,以查找 int、float 或 long double 类型的幂。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| x | 底数 | 必需 |
| y | 幂值 | 必需 |
pow() 函数的返回值是浮点数。
| 输入 | 返回值 |
|---|---|
| x & y | 返回 xy |
#include <stdio.h>
#include <math.h>
int main()
{
double B, P, output;
printf("Enter the base number: ");
scanf("%lf", &B);
printf("Enter the power raised: ");
scanf("%lf",&P);
output = pow(B,P);
printf("%.1lf^%.1lf = %.2lf", B, P, output);
return 0;
}
输出
Enter the base number: 4.00 Enter the power raised: 2.00 4.00^2.00 = 16.00
#include <stdio.h>
#include <math.h>
int main () {
printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
return(0);
}
输出
Value 8.0 ^ 3 = 512.000000 Value 3.05 ^ 1.98 = 9.097324