Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::abs(float),std::fabs,std::fabsf,std::fabsl

      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<cmath>
      Defined in header<cstdlib>
      (1)
      float       abs(float num);

      double      abs(double num);

      longdouble abs(longdouble num);
      (until C++23)
      constexpr/* floating-point-type */
                  abs(/* floating-point-type */ num);
      (since C++23)
      Defined in header<cmath>
      (2)
      float       fabs(float num);

      double      fabs(double num);

      longdouble fabs(longdouble num);
      (until C++23)
      constexpr/* floating-point-type */
                  fabs(/* floating-point-type */ num);
      (since C++23)
      float       fabsf(float num);
      (3)(since C++11)
      (constexpr since C++23)
      longdouble fabsl(longdouble num);
      (4)(since C++11)
      (constexpr since C++23)
      Defined in header<cmath>
      template<class Integer>
      double      fabs( Integer num);
      (A)(since C++11)
      (constexpr since C++23)
      1-4) Computes the absolute value of the floating-point valuenum. The library provides overloads ofstd::abs andstd::fabs for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      A) Additional overloads are provided for all integer types, which are treated asdouble.
      (since C++11)

      For integral arguments,the integral overloads ofstd::abs are likely better matches. Ifstd::abs is called with an unsigned integral argument that cannot be converted toint byintegral promotion, the program is ill-formed.

      Contents

      [edit]Parameters

      num - floating-point or integer value

      [edit]Return value

      If successful, returns the absolute value ofarg (|arg|). The value returned is exact and does not depend on any rounding modes.

      [edit]Error handling

      This function is not subject to any of the error conditions specified inmath_errhandling.

      If the implementation supports IEEE floating-point arithmetic (IEC 60559),

      • If the argument is ±0, +0 is returned.
      • If the argument is ±∞, +∞ is returned.
      • If the argument is NaN, NaN is returned.

      [edit]Notes

      The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their argumentnum of integer type,std::fabs(num) has the same effect asstd::fabs(static_cast<double>(num)).

      [edit]Example

      Run this code
      #include <cmath>#include <iostream> int main(){std::cout<<"abs(+3.0) = "<< std::abs(+3.0)<<'\n'<<"abs(-3.0) = "<< std::abs(-3.0)<<'\n'; // special valuesstd::cout<<"abs(-0.0) = "<< std::abs(-0.0)<<'\n'<<"abs(-Inf) = "<< std::abs(-INFINITY)<<'\n'<<"abs(-NaN) = "<< std::abs(-NAN)<<'\n';}

      Possible output:

      abs(+3.0) = 3abs(-3.0) = 3abs(-0.0) = 0abs(-Inf) = infabs(-NaN) = nan

      [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
      LWG 2735C++11overloads ofstd::abs for integer types
      returningdouble was erroneously required
      removed the requirement

      [edit]See also

      computes absolute value of an integral value (\(\small{|x|}\)|x|)
      (function)[edit]
      (C++11)(C++11)(C++11)
      copies the sign of a floating point value
      (function)[edit]
      (C++11)
      checks if the given number is negative
      (function)[edit]
      returns the magnitude of a complex number
      (function template)[edit]
      applies the functionabs to each element of valarray
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/fabs&oldid=181911"

      [8]ページ先頭

      ©2009-2025 Movatter.jp