一、sqrt()函数
此功能用于双精度数据。因此,这将返回double类型的平方根。
语法如下:
double sqrt(double argument)
例:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
double x = 144.0;
double y = 180.0;
cout << fixed << setprecision(12) << sqrt(x) << endl;
cout << fixed << setprecision(12) << sqrt(y) << endl;
}
输出:
12.000000000000
13.416407864999
二、sqrtf()函数
该功能用于浮点型数据。因此,这将返回float类型的平方根。
语法如下:
float sqrtf(float argument)
例:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
float x = 144.0;
float y = 180.0;
cout << fixed << setprecision(6) << sqrtf(x) << endl;
cout << fixed << setprecision(6) << sqrtf(y) << endl;
}
输出:
12.000000
13.416408
三、sqrtl()函数
此功能用于长双精度型数据。因此,这将返回long double类型的平方根。这是精度更高的两倍。当我们使用长整数时,此函数很有用。
语法如下:
long double sqrtl(long double argument)
例:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
long long int x = 5000000000000000000;
long long int y = 999999999999999999;
cout << fixed << setprecision(12) << sqrtl(x) << endl;
cout << fixed << setprecision(12) << sqrtl(y) << endl;
}
输出:
2236067977.499789696420
999999999.999999999476