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