sqrt 是 "square root"(平方根)的缩写,它是C标准库中的一个数学函数,用于计算一个非负数的平方根。

(图片来源网络,侵删)
核心要点
在开始使用之前,请记住以下几个最核心的要点:
- 功能:计算一个数的平方根。
- 头文件:使用
sqrt函数前,必须包含<math.h>头文件。 - 返回值:返回一个
double类型的结果。 - 参数:接受一个
double类型的参数。 - 错误处理:如果传入的参数是负数,结果会是 "NaN" (Not a Number),并且会设置一个错误标志。
基本语法
#include <math.h> double sqrt(double x);
x:你想要计算平方根的数字,它可以是int,float或double类型,但函数会自动将其提升为double。- 返回值:
x的平方根,类型为double。
使用示例
下面是一个最简单的完整示例,展示了如何计算一个数字的平方根。
示例1:基本用法
#include <stdio.h>
#include <math.h> // 1. 必须包含这个头文件
int main() {
double number = 25.0;
double result;
// 2. 调用 sqrt 函数
result = sqrt(number);
// 3. 打印结果
printf("The square root of %.2f is %.2f\n", number, result);
return 0;
}
编译和运行:
重要提示:在使用数学库的C程序进行编译时,通常需要链接数学库,在Linux/macOS下,你需要在编译命令末尾加上
-lm。(图片来源网络,侵删)gcc your_program.c -o your_program -lm
gcc是你的编译器。-o your_program指定输出的可执行文件名。-lm就是链接数学库的选项。
输出结果:
The square root of 25.00 is 5.00
重要注意事项
注意事项1:包含头文件 <math.h>
忘记包含这个头文件是初学者最常犯的错误之一,如果没包含,编译器会提示 sqrt 未声明。
// 错误示例
#include <stdio.h>
int main() {
double result = sqrt(16.0); // 编译错误: 'sqrt' 未声明
printf("%f\n", result);
return 0;
}
注意事项2:参数不能为负数
sqrt 函数的定义域是 [0, +∞),如果你试图计算一个负数的平方根,在数学上这是无意义的(在实数范围内),C语言会返回一个特殊的值 NaN (Not a Number)。
#include <stdio.h>
#include <math.h>
#include <stdio.h> // 用于 isnan 函数
int main() {
double negative_number = -9.0;
double result = sqrt(negative_number);
printf("The square root of %.2f is %f\n", negative_number, result);
// 检查结果是否为 NaN
if (isnan(result)) {
printf("Error: Cannot calculate the square root of a negative number.\n");
}
return 0;
}
输出结果:
The square root of -9.00 is nan
Error: Cannot calculate the square root of a negative number.
isnan 函数(需要包含 <stdio.h> 或 <math.h>)可以帮助你检测一个值是否为 NaN。
注意事项3:编译时链接数学库 (-lm)
如前所述,这是在Linux和macOS环境下编译的关键步骤,在Windows上使用Visual Studio等IDE时,通常会自动处理链接,但如果你使用命令行(如MinGW),同样需要加上 -lm。
进阶用法
示例2:计算用户输入的数字的平方根
这个程序会提示用户输入一个数字,然后计算并显示其平方根。
#include <stdio.h>
#include <math.h>
int main() {
double input_num;
printf("Please enter a non-negative number: ");
scanf("%lf", &input_num);
// 检查输入是否为负数
if (input_num < 0) {
printf("Error: Invalid input. Number cannot be negative.\n");
} else {
double square_root = sqrt(input_num);
printf("The square root of %f is %f\n", input_num, square_root);
}
return 0;
}
运行示例1:
Please enter a non-negative number: 144
The square root of 144.000000 is 12.000000
运行示例2:
Please enter a non-negative number: -10
Error: Invalid input. Number cannot be negative.
示例3:结合 pow 函数进行验证
pow(x, y) 函数用于计算 x 的 y 次方,我们可以用它来验证 sqrt 的结果是否正确,因为 sqrt(x) 等同于 pow(x, 0.5)。
#include <stdio.h>
#include <math.h>
int main() {
double x = 81.0;
double root = sqrt(x);
double verification = pow(root, 2.0); // (sqrt(x))^2 应该等于 x
printf("Original number: %f\n", x);
printf("Square root: %f\n", root);
printf("Verification (root * root): %f\n", verification);
return 0;
}
输出结果:
Original number: 81.000000
Square root: 9.000000
Verification (root * root): 81.000000
| 要点 | 描述 |
|---|---|
| 头文件 | #include <math.h> |
| 函数原型 | double sqrt(double x); |
| 功能 | 计算 x 的算术平方根。 |
| 参数 | x 必须大于或等于0。 |
| 返回值 | x 的平方根(double 类型),若 x 为负,返回 NaN。 |
| 编译 | 在Linux/macOS下,使用 gcc -o program program.c -lm 链接数学库。 |
记住这几点,你就可以在C程序中正确、安全地使用 sqrt 函数了。

