Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::abs,std::labs,std::llabs,std::imaxabs

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Nearest integer floating point operations
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      (C++11)(C++11)(C++11)
      Floating point manipulation functions
      (C++11)(C++11)
      (C++11)
      (C++11)
      Classification and comparison
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Types
      (C++11)
      (C++11)
      (C++11)
      Macro constants
       
      Defined in header<cstdlib>
      Defined in header<cmath>
      int       abs(int num);
      (1)(constexpr since C++23)
      long      abs(long num);
      (2)(constexpr since C++23)
      longlong abs(longlong num);
      (3)(since C++11)
      (constexpr since C++23)
      Defined in header<cstdlib>
      long       labs(long num);
      (4)(constexpr since C++23)
      longlong llabs(longlong num);
      (5)(since C++11)
      (constexpr since C++23)
      Defined in header<cinttypes>
      (6)(since C++11)
      (constexpr since C++23)
      (7)(since C++11)
      (constexpr since C++23)

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

      Ifstd::abs is called with an unsigned integral argument that cannot be converted toint byintegral promotion, the program is ill-formed.

      Overload(6) ofstd::abs forstd::intmax_t is provided in<cinttypes> if and only ifstd::intmax_t is anextended integer type.

      (since C++11)

      Contents

      [edit]Parameters

      num - integer value

      [edit]Return value

      The absolute value ofnum (i.e.|num|), if it is representable.

      [edit]Notes

      In2'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 <climits>#include <cstdlib>#include <iostream> int main(){std::cout<<std::showpos<<"abs(+3) = "<< std::abs(3)<<'\n'<<"abs(-3) = "<< std::abs(-3)<<'\n'; //  std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems}

      Output:

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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2192C++98overloads ofstd::abs were
      inconsistently declared in two headers
      declared these overloads
      in both headers

      [edit]See also

      absolute value of a floating point value (\(\small{|x|}\)|x|)
      (function)[edit]
      returns the magnitude of a complex number
      (function template)[edit]
      applies the functionabs to each element of valarray
      (function template)[edit]
      C documentation forabs,labs,llabs
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/abs&oldid=181910"

      [8]ページ先頭

      ©2009-2025 Movatter.jp