Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      abs, labs, llabs, imaxabs

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      abslabsllabsimaxabs
      (C99)(C99)
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      (C99)(C23)
      (C99)
      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
      (C99)(C23)
      Arguments and return values
      Error handling
      Fast operation indicators
       
      Defined in header<stdlib.h>
      int        abs(int n);
      long       labs(long n);
      longlong llabs(longlong n);
      (since C99)
      Defined in header<inttypes.h>
      intmax_t imaxabs(intmax_t n);
      (since C99)

      Computes the absolute value of an integer number. The behavior is undefined if the result cannot be represented by the return type.

      Contents

      [edit]Parameters

      n - integer value

      [edit]Return value

      The absolute value ofn (i.e.|n|), if it is representable.

      [edit]Notes

      In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement typeint,INT_MIN is-2147483648, but the would-be result2147483648 is greater thanINT_MAX, which is2147483647.

      [edit]Example

      Run this code
      #include <limits.h>#include <stdio.h>#include <stdlib.h> int main(void){printf("abs(+3) = %d\n", abs(+3));printf("abs(-3) = %d\n", abs(-3)); //  printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems}

      Output:

      abs(+3) = 3abs(-3) = 3

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.8.2.1 The imaxabs function (p: TBD)
      • 7.22.6.1 The abs, labs and llabs functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.8.2.1 The imaxabs function (p: 159)
      • 7.22.6.1 The abs, labs and llabs functions (p: 259)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.8.2.1 The imaxabs function (p: 218)
      • 7.22.6.1 The abs, labs and llabs functions (p: 356)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.8.2.1 The imaxabs function (p: 199-200)
      • 7.20.6.1 The abs, labs and llabs functions (p: 320)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.10.6.1 The abs function
      • 4.10.6.3 The labs function

      [edit]See also

      (C99)(C99)
      computes absolute value of a floating-point value (\(\small{|x|}\)|x|)
      (function)[edit]
      (C99)(C99)(C99)
      computes the magnitude of a complex number
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/abs&oldid=172096"

      [8]ページ先頭

      ©2009-2025 Movatter.jp