C ++幂函数
幂函数用于计算幂(例如,提高到幂,平方根,立方根等)。有以下幂函数,它们是cmath标头的库函数。pow()函数
sqrt()函数
cbrt()函数
hypot()函数
1)pow()函数
pow()函数是cmath标头(在早期版本中为 )的库函数,用于查找幂的加数,它接受两个参数并将第一个参数返回为第二个参数的幂。
pow()函数语法:pow(x, y);
2)sqrt()函数
sqrt()函数是cmath标头(在早期版本中为 )的库函数,用于查找给定数字的平方根,它接受数字并返回平方根。
注意:如果我们提供负值,则sqrt()函数将返回域错误。(-nan)。
sqrt()函数语法:sqrt(x);
3)cbrt()函数
cbrt()函数是cmath标头的库函数,用于查找给定数字的立方根,它接受数字并返回立方根。
cbrt()函数语法:cbrt(x);
4)hypot()函数
hypot()函数是cmath标头的库函数,用于查找给定数字的斜边,接受两个数字并返回斜边的计算结果,即sqrt(x * x + y * y)。
hypot()函数语法:hypot(x, y);
C ++程序演示幂函数示例//示例
//电源功能
#include
#include
using namespace std;
// main()部分
int main(){
float x, y;
float result;
//pow()函数
x = 12;
y = 4;
result = pow(x,y);
cout<
cout<
//sqrt()函数
x = 2;
result = sqrt(x);
cout<
cout<
//cbrt()函数
x = 2;
result = cbrt(x);
cout<
cout<
//hypot()函数
x = 2;
y = 3;
result = hypot(x,y);
cout<
cout<
return 0;
}
输出结果12 to the power of 4 is : 20736
square root of 2 is : 1.41421
cubic root of 2 is : 1.25992
hypotenuse is : 3.60555