c语言幂函数

C ++幂函数 (C++ power functions)

Power functions are used to calculate the powers (like, raise to power, square root, cubic root, etc). There are following power functions which are the library functions of cmath header.

幂函数用于计算幂(例如,提高到幂,平方根,立方根等)。 有以下幂函数 ,它们是cmath标头的库函数。

  1. pow() function

    pow()函数

  2. sqrt() function

    sqrt()函数

  3. cbrt() function

    cbrt()函数

  4. hypot() function

    hypot()函数

1)pow()函数 (1) pow() function)

pow() function is a library function of cmath header (<math.h> in earlier versions), it is used to find the raise to the power, it accepts two arguments and returns the first argument to the power of the second argument.

pow()函数cmath标头的库函数(在早期版本中为<math.h>),用于查找幂的加数,它接受两个参数并将第一个参数返回为第二个参数的幂。

Syntax of pow() function:

pow()函数的语法:

    pow(x, y);

2)sqrt()函数 (2) sqrt() function)

sqrt() function is a library function of cmath header (<math.h> in earlier versions), it is used to find the square root of a given number, it accepts a number and returns the square root.

sqrt()函数cmath标头(在早期版本中为<math.h> )的库函数,用于查找给定数字的平方根,它接受数字并返回平方根。

Note: If we provide a negative value, sqrt() function returns a domain error. (-nan).

注意:如果我们提供负值,则sqrt()函数将返回域错误。 ( -nan )。

Syntax of sqrt() function:

sqrt()函数的语法:

    sqrt(x);

3)cbrt()函数 (3) cbrt() function)

cbrt() function is a library function of cmath header, it is used to find the cubic root of a given number, it accepts a number and returns the cubic root.

cbrt()函数cmath标头的库函数,用于查找给定数字立方根 ,它接受数字并返回立方根。

Syntax of cbrt() function:

cbrt()函数的语法:

    cbrt(x);

4)hypot()函数 (4) hypot() function)

hypot() function is a library function of cmath header, it is used to find the hypotenuse of the given numbers, it accepts two numbers and returns the calculated result of hypotenuse i.e. sqrt(x*x + y*y).

hypot()函数cmath标头的库函数,用于查找给定数字的斜边,接受两个数字并返回斜边的计算结果,即sqrt(x * x + y * y) 。

Syntax of hypot() function:

hypot()函数的语法:

    hypot(x, y);

C ++程序演示幂函数示例 (C++ program to demonstrate example of power functions)

// C++ program to demonstrate example of 
// power functions

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x, y;
    float result;
    
    //pow() function
    x = 12;
    y = 4;
    result = pow(x,y);
    cout<<x<<" to the power of "<<y<<" is : "<<result;
    cout<<endl;
    
    //sqrt() function
    x = 2;
    result = sqrt(x);
    cout<<"square root of "<<x<<" is : "<<result;
    cout<<endl;

    //cbrt() function
    x = 2;
    result = cbrt(x);
    cout<<"cubic root of "<<x<<" is : "<<result;
    cout<<endl; 

    //hypot() function
    x = 2;
    y = 3;
    result = hypot(x,y);
    cout<<"hypotenuse is : "<<result;
    cout<<endl;     
    
    return 0;
}

Output

输出量

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


翻译自: https://www.includehelp.com/cpp-tutorial/power-functions.aspx

c语言幂函数

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐