Namespaces
Variants

fabs, fabsf, fabsl, fabsd32, fabsd64, fabsd128

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 的绝对值。

当且仅当实现预定义了 __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 )。否则行为未定义。

目录

参数

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 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函数(页码:228-229)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.4.2 fabs函数(页码:460)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.6.2 fabs 函数

参见

计算整型数值的绝对值 ( |x| )
(函数)
生成一个具有给定值大小和另一个给定值符号的数值
(函数)
(C99)
检查给定数值是否为负
(函数宏)
(C99) (C99) (C99)
计算复数的模
(函数)
C++ 文档 for fabs