C 标准库 - <math.h>

C 语言 fabs() 函数用于计算浮点数的绝对值

原型

double fabs(double x);

参数为 double 值,返回它的绝对值(非负 double)。整数绝对值请用 stdlib.h 的 abs()。

示例

#include <stdio.h>
#include <math.h>

int main() {
    double x = -5.7;
    printf("fabs(%.1lf) = %.1lf\n", x, fabs(x));
    return 0;
}

输出:

fabs(-5.7) = 5.7

注意事项

使用 math.h 的函数时,多数编译器需要链接数学库(编译选项 -lm,如 gcc test.c -lm -o test)。所有三角函数使用弧度制。