C
Compiler support
Language
Headers
Type support
Program utilities
Variadic function support
Error handling
Dynamic memory management
Strings library
Algorithms
Numerics
Date and time utilities
Input/output support
Localization support
Concurrency support
(C11)
Technical Specifications
Symbol index
Numerics
Common mathematical functions
Floating-point environment
(C99)
Pseudo-random number generation
Complex number arithmetic
(C99)
Type-generic math
(C99)
Bit manipulation
(C23)
Checked integer arithmetic
(C23)
Common mathematical functions
Functions
Basic operations
abs
labs
llabs
imaxabs
(C99)
(C99)
fabs
div
ldiv
lldiv
imaxdiv
(C99)
(C99)
fmod
remainder
(C99)
remquo
(C99)
fma
(C99)
fdim
(C99)
nan
nanf
nanl
nand
N
(C99)
(C99)
(C99)
(C23)
Maximum/minimum operations
fmax
(C99)
fmin
(C99)
fmaximum
(C23)
fminimum
(C23)
fmaximum_mag
(C23)
fmaximum_num
(C23)
fminimum_mag
(C23)
fminimum_num
(C23)
fmaximum_mag_num
(C23)
fminimum_mag_num
(C23)
Exponential functions
exp
exp10
(C23)
exp2
(C99)
expm1
(C99)
exp10m1
(C23)
exp2m1
(C23)
log
log10
log2
(C99)
log1p
logp1
(C99)
(C23)
log10p1
(C23)
log2p1
(C23)
Power functions
sqrt
cbrt
(C99)
rootn
(C23)
rsqrt
(C23)
hypot
(C99)
compound
(C23)
pow
pown
(C23)
powr
(C23)
Trigonometric and hyperbolic functions
sin
cos
tan
asin
acos
atan
atan2
sinpi
(C23)
cospi
(C23)
tanpi
(C23)
asinpi
(C23)
acospi
(C23)
atanpi
(C23)
atan2pi
(C23)
sinh
cosh
tanh
asinh
(C99)
acosh
(C99)
atanh
(C99)
Nearest integer floating-point
ceil
floor
round
lround
llround
(C99)
(C99)
(C99)
roundeven
(C23)
trunc
(C99)
nearbyint
(C99)
rint
lrint
llrint
(C99)
(C99)
(C99)
fromfp
fromfpx
ufromfp
ufromfpx
(C23)
(C23)
(C23)
(C23)
Floating-point manipulation
ldexp
frexp
scalbn
scalbln
(C99)
(C99)
ilogb
llogb
(C99)
(C23)
logb
(C99)
modf
nextafter
nexttoward
(C99)
(C99)
nextup
nextdown
(C23)
(C23)
copysign
(C99)
canonicalize
(C23)
Narrowing operations
fadd
(C23)
fsub
(C23)
fmul
(C23)
fdiv
(C23)
ffma
(C23)
fsqrt
(C23)
Quantum and quantum exponent
quantized
N
(C23)
quantumd
N
(C23)
samequantumd
N
(C23)
llquantexpd
N
(C23)
Decimal re-encoding functions
encodedecd
N
(C23)
decodedecd
N
(C23)
encodebind
N
(C23)
decodebind
N
(C23)
Total order and payload functions
totalorder
(C23)
getpayload
(C23)
setpayload
(C23)
setpayloadsig
(C23)
Classification
fpclassify
(C99)
iscanonical
(C23)
isfinite
(C99)
isinf
(C99)
isnan
(C99)
isnormal
(C99)
signbit
(C99)
issubnormal
(C23)
iszero
(C23)
isgreater
(C99)
isgreaterequal
(C99)
isless
(C99)
islessequal
(C99)
islessgreater
(C99)
isunordered
(C99)
issignaling
(C23)
iseqsig
(C23)
Error and gamma functions
erf
(C99)
erfc
(C99)
lgamma
(C99)
tgamma
(C99)
Types
div_t
ldiv_t
lldiv_t
imaxdiv_t
(C99)
(C99)
float_t
double_t
(C99)
(C99)
_Decimal32_t
_Decimal64_t
(C23)
(C23)
Macro constants
Special floating-point values
HUGE_VAL
HUGE_VALF
HUGE_VALL
HUGE_VALD
N
(C99)
(C99)
(C23)
INFINITY
DEC_INFINITY
(C99)
(C23)
NAN
DEC_NAN
(C99)
(C23)
Arguments and return values
FP_ILOGB0
FP_ILOGBNAN
(C99)
(C99)
FP_NORMAL
FP_SUBNORMAL
FP_ZERO
FP_INFINITE
FP_NAN
(C99)
(C99)
(C99)
(C99)
(C99)
FP_LLOGB0
FP_LLOGBNAN
(C23)
(C23)
FP_INT_UPWARD
FP_INT_DOWNWARD
FP_INT_TOWARDZERO
FP_INT_TONEARESTFROMZERO
FP_INT_TONEAREST
(C23)
(C23)
(C23)
(C23)
(C23)
Error handling
MATH_ERRNO
MATH_ERRNOEXCEPT
(C99)
(C99)
math_errhandling
(C99)
Fast operation indicators
FP_FAST_FMAF
FP_FAST_FMA
(C99)
(C99)
FP_FAST_FADD
FP_FAST_FADDL
FP_FAST_DADDL
FP_FAST_D
M
ADDD
N
(C23)
(C23)
(C23)
(C23)
FP_FAST_FMUL
FP_FAST_FMULL
FP_FAST_DMULL
FP_FAST_D
M
MULD
N
(C23)
(C23)
(C23)
(C23)
FP_FAST_FFMA
FP_FAST_FFMAL
FP_FAST_DFMAL
FP_FAST_D
M
FMAD
N
(C23)
(C23)
(C23)
(C23)
FP_FAST_FMAL
FP_FAST_FMAD
N
(C99)
(C23)
FP_FAST_FSUB
FP_FAST_FSUBL
FP_FAST_DSUBL
FP_FAST_D
M
SUBD
N
(C23)
(C23)
(C23)
(C23)
FP_FAST_FDIV
FP_FAST_FDIVL
FP_FAST_DDIVL
FP_FAST_D
M
DIVD
N
(C23)
(C23)
(C23)
(C23)
FP_FAST_FSQRT
FP_FAST_FSQRTL
FP_FAST_DSQRTL
FP_FAST_D
M
SQRTD
N
(C23)
(C23)
(C23)
(C23)
定义于头文件
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 起)
定义于头文件
#define fabs( arith )
(7)
(C99 起)
1-6)
计算浮点值
arg
的绝对值。
当且仅当实现预定义了
__STDC_IEC_60559_DFP__
(即实现支持十进制浮点数)时,才会声明具有十进制浮点参数的函数。
(since C23)
7)
类型泛型宏:若参数类型为
_Decimal128
,
_Decimal64
,
_Decimal32
,
(C23起)
long
double
,
double
或
float
,则分别调用
fabsd128
,
fabsd64
,
fabsd32
,
(C23起)
fabsl
,
fabs
或
fabsf
。否则,若参数为整数类型,则调用
fabs
。否则,若参数为复数,则宏调用对应的复数函数(
cabsf
,
cabs
,
cabsl
)。否则行为未定义。
目录
1
参数
2
返回值
3
错误处理
4
示例
5
参考资料
6
参见
参数
arg
-
浮点数值
arith
-
浮点数或整数值
返回值
如果成功,返回
arg
的绝对值 (
\(\small |arg| \)
|arg|
)。返回值是精确的,且不依赖于任何舍入模式。
错误处理
此函数不受
math_errhandling
中指定的任何错误条件约束。
如果实现支持 IEEE 浮点算术 (IEC 60559),
若参数为 ±0,则返回 +0。
若参数为 ±∞,则返回 +∞。
若参数为 NaN,则返回 NaN。
示例
运行此代码
#include
#include
#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 fabs 函数集 (p: TBD)
7.25 泛型数学
F.10.4.2 fabs 函数集 (p: TBD)
C17 标准 (ISO/IEC 9899:2018):
7.12.7.2 fabs 函数集 (p: 181)
7.25 泛型数学
F.10.4.2 fabs 函数集 (p: 382)
C11 标准 (ISO/IEC 9899:2011):
7.12.7.2 fabs 函数集 (p: 248)
7.25 泛型数学
F.10.4.2 fabs 函数集 (p: 524)
C99标准(ISO/IEC 9899:1999):
7.12.7.2 fabs函数(页码:228-229)
7.22 泛型数学
F.9.4.2 fabs函数(页码:460)
C89/C90 标准 (ISO/IEC 9899:1990):
4.5.6.2 fabs 函数
参见
abs
labs
llabs
(C99)
计算整型数值的绝对值 (
\(\small{|x|}\)
|x|
)
(函数)
copysign
copysignf
copysignl
(C99)
(C99)
(C99)
生成一个具有给定值大小和另一个给定值符号的数值
(函数)
signbit
(C99)
检查给定数值是否为负
(函数宏)
cabs
cabsf
cabsl
(C99)
(C99)
(C99)
计算复数的模
(函数)
C++ 文档
for
fabs