fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128
来自 cppreference.cn
定义于头文件 <math.h> |
||
float fabsf( float arg ); |
(1) | (自 C99) |
double fabs( double arg ); |
(2) | |
long double fabsl( long double arg ); |
(3) | (自 C99) |
_Decimal32 fabsd32( _Decimal32 arg ); |
(4) | (自 C23) |
_Decimal64 fabsd64( _Decimal64 arg ); |
(5) | (自 C23) |
_Decimal128 fabsd128( _Decimal128 arg ); |
(6) | (自 C23) |
定义于头文件 <tgmath.h> |
||
#define fabs( arith ) |
(7) | (自 C99) |
1-6) 计算浮点数值 arg 的绝对值。
如果实现预定义了 |
(自 C23) |
7) 类型泛型宏:若实参拥有类型 _Decimal128、_Decimal64、_Decimal32、(自 C23)long double、 double 或 float,则分别调用
fabsd128
、fabsd64
、fabsd32
、(自 C23)fabsl
、 fabs
或 fabsf
。 否则,若实参拥有整数类型,则调用 fabs
。 否则,若实参为复数,则此宏调用对应的复数函数(cabsf、 cabs、 cabsl)。 否则,行为未定义。内容 |
[编辑] 参数
arg | - | 浮点数值 |
arith | - | 浮点数或整数值 |
[编辑] 返回值
若成功,则返回 arg 的绝对值(|arg|)。 返回值是精确的,且不依赖于任何舍入模式。
[编辑] 错误处理
此函数不受到 math_errhandling 中指定的任何错误条件约束。
若实现支持 IEEE 浮点算术 (IEC 60559),
- 若参数是 ±0,则返回 +0。
- 若参数是 ±∞,则返回 +∞。
- 若参数是 NaN,则返回 NaN。
[编辑] 示例
运行此代码
#include <math.h> #include <stdio.h> #define PI 3.14159 // This numerical integration assumes all area is positive. double integrate(double f(double), double a, double b, // assume a < b unsigned steps) // assume steps > 0 { const double dx = (b - a) / steps; double sum = 0.0; for (double x = a; x < b; x += dx) sum += fabs(f(x)); return dx * sum; } int main(void) { printf("fabs(+3) = %f\n", fabs(+3.0)); printf("fabs(-3) = %f\n", fabs(-3.0)); // special values printf("fabs(-0) = %f\n", fabs(-0.0)); printf("fabs(-Inf) = %f\n", fabs(-INFINITY)); printf("Area under sin(x) in [-PI, PI] = %f\n", integrate(sin, -PI, PI, 5101)); }
输出
fabs(+3) = 3.000000 fabs(-3) = 3.000000 fabs(-0) = 0.000000 fabs(-Inf) = inf Area under sin(x) in [-PI, PI] = 4.000000
[编辑] 引用
- C23 标准 (ISO/IEC 9899:2024)
- 7.12.7.2 The fabs functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.4.2 The fabs functions (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.7.2 The fabs functions (p: 181)
- 7.25 Type-generic math <tgmath.h> (p: 272-273)
- F.10.4.2 The fabs functions (p: 382)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.7.2 The fabs functions (p: 248)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.4.2 The fabs functions (p: 524)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.7.2 The fabs functions (p: 228-229)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.4.2 The fabs functions (p: 460)
- C89/C90 标准 (ISO/IEC 9899:1990)
- 4.5.6.2 The fabs function
[编辑] 参见
(C99) |
计算整数值的绝对值 (|x|) (函数) |
(C99)(C99)(C99) |
产生一个大小为给定值,符号为另一给定值的数值 (函数) |
(C99) |
检查给定数字是否为负数 (函数宏) |
(C99)(C99)(C99) |
计算复数的模 (函数) |
C++ 文档 关于 fabs
|