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
// 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 함수

참고 항목

정수 값의 절댓값을 계산합니다 ( |x| )
(함수)
주어진 값의 크기와 다른 주어진 값의 부호를 갖는 값을 생성합니다
(함수)
(C99)
주어진 숫자가 음수인지 확인합니다
(함수 매크로)
(C99) (C99) (C99)
복소수의 크기를 계산합니다
(함수)