sqrt() 函数在 math.h 头文件中定义。它有助于返回给定参数的平方根。
double sqrt(double x); #where x should be in double
此外,还使用了两个函数 sqrtf() 和 sqrtl(),分别用于 float 和 long double 类型。
float sqrtf(float x);
long double sqrtl(long double x);
sqrt() 函数接受一个双精度浮点型的参数。我们可以使用强制类型转换运算符将类型显式转换为双精度浮点型,以查找 int、float 或 long double 类型的平方根。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 双精度浮点数值 | 需要计算平方根的值 | 必需 |
sqrt() 函数的返回值为双精度浮点数。
| 输入 | 返回值 |
|---|---|
| 双精度浮点数值 | 平方根(双精度浮点型) |
#include <stdio.h>
#include <math.h>
int main()
{
double N = 25, squareR;
squareR = sqrt(N);
printf("The Square root of given %lf is %lf", N, squareR);
return 0;
}
输出
The Square root of given 25.000000 is 5.000000
#include <stdio.h>
#include <math.h>
int main () {
printf("The Square root of %lf is %lf\n", 4.0, sqrt(4.0) );
printf("The Square root of %lf is %lf\n", 5.0, sqrt(5.0) );
return(0);
}
输出
The Square root of 4.000000 is 2.000000 The Square root of 5.000000 is 2.236068