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 // 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 fabs 函式 (p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.4.2 fabs 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.7.2 fabs 函式 (p: 181)
- 7.25 型別通用數學 <tgmath.h> (p: 272-273)
- F.10.4.2 fabs 函式 (p: 382)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.7.2 fabs 函式 (p: 248)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.4.2 fabs 函式 (p: 524)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.7.2 fabs 函式 (p: 228-229)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.4.2 fabs 函式 (p: 460)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.5.6.2 fabs 函式
[編輯] 參閱
(C99) |
計算整數值的絕對值 (|x|) (函式) |
(C99)(C99)(C99) |
生成一個具有給定值的幅度並具有另一個給定值的符號的值 (函式) |
(C99) |
檢查給定數字是否為負數 (函式宏) |
(C99)(C99)(C99) |
計算複數的模 (函式) |
C++ 文件,關於 fabs
|