pow(x,y)函數(shù)計算參數(shù)x 的 y 次冪。
pow()函數(shù)采用兩個參數(shù)(基值和冪值),然后將提高到基數(shù)的冪返回。例如
pow()函數(shù)在math.h頭文件中定義。
double pow(double x, double y)
x -- 代表基數(shù)的浮點值。y -- 代表指數(shù)的浮點值。
要查找int或float變量的冪,可以使用強(qiáng)制轉(zhuǎn)換運(yùn)算符將類型顯式轉(zhuǎn)換為double類型。
int base = 3; int power = 5; pow(double(base), double(power));
#include <stdio.h>
#include <math.h>
int main()
{
double base, power, result;
printf("輸入基數(shù): ");
scanf("%lf", &base);
printf("輸入指數(shù): ");
scanf("%lf",&power);
result = pow(base,power);
printf("%.1lf^%.1lf = %.2lf", base, power, result);
return 0;
}輸出結(jié)果
輸入基數(shù): 2.5 輸入指數(shù): 3.4 2.5^3.4 = 22.54