fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128
来自cppreference.com
| 在标头 <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 // 此数值积分假定所有面积均为正。 double integrate(double f(double), double a, double b, // 假定 a < b unsigned steps) // 假定 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)); // 特殊值 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 (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.4.2 The fabs functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.7.2 The fabs functions (第 181 页)
- 7.25 Type-generic math <tgmath.h> (第 272-273 页)
- F.10.4.2 The fabs functions (第 382 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.7.2 The fabs functions (第 248 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.4.2 The fabs functions (第 524 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.7.2 The fabs functions (第 228-229 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.4.2 The fabs functions (第 460 页)
- C89/C90 标准(ISO/IEC 9899:1990):
- 4.5.6.2 The fabs function
参阅
| (C99) |
计算整数值的绝对值(|x|) (函数) |
| (C99)(C99)(C99) |
从一个给定值的绝对值和另一个给定值的符号产生值 (函数) |
| (C99) |
检查给定数是不是负数 (宏函数) |
| (C99)(C99)(C99) |
计算复数的模(绝对值) (函数) |