📜  C++ sqrt()

📅  最后修改于: 2020-09-25 08:08:17             🧑  作者: Mango

C++中的sqrt() 函数返回数字的平方根。

[Mathematics]  √x = sqrt(x) [In C Programming]

此函数在头文件中定义。

sqrt()原型[从C++ 11标准开始]

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double sqrt(T x); // For integral type

sqrt()参数

sqrt() 函数采用单个非负参数。

如果将负参数传递给sqrt() 函数,则会发生域错误。

sqrt()返回值

sqrt() 函数返回给定参数的平方根。

示例1:sqrt()如何在C++中工作?

#include 
#include 
using namespace std;

int main()
{
    double x = 10.25, result;
    result = sqrt(x);
    cout << "Square root of " << x << " is " << result << endl;

    return 0;
}

运行该程序时,输出为:

Square root of 10.25 is 3.20156

示例2:带整数参数的sqrt() 函数

#include 
#include 
using namespace std;

int main()
{
    long x = 464453422;
    double result = sqrt(x);
    cout << "Square root of " << x << " is " << result << endl;
    
    return 0;
}

运行该程序时,输出为:

Square root of 464453422 is 21551.2