table of contents
        
      
      
    | std::abs(float),std::fabs,std::fabsf,std::fabsl(3) | C++ Standard Libary | std::abs(float),std::fabs,std::fabsf,std::fabsl(3) | 
NAME¶
std::abs(float),std::fabs,std::fabsf,std::fabsl - std::abs(float),std::fabs,std::fabsf,std::fabsl
Synopsis¶
 Defined in header <cmath>
  
   Defined in header <cstdlib>
  
   float abs( float arg ); (1) (constexpr since C++23)
  
   double abs( double arg ); (2) (constexpr since C++23)
  
   long double abs( long double arg ); (3) (constexpr since C++23)
  
   Defined in header <cmath>
  
   float fabs ( float arg ); (4) (constexpr since C++23)
  
   float fabsf( float arg ); (5) (since C++11)
  
   (constexpr since C++23)
  
   double fabs ( double arg ); (6) (constexpr since C++23)
  
   long double fabs ( long double arg ); (7) (constexpr since C++23)
  
   long double fabsl( long double arg ); (8) (since C++11)
  
   (constexpr since C++23)
  
   double fabs ( IntegralType arg ); (9) (since C++11)
  
   (constexpr since C++23)
  
   1-8) Computes the absolute value of a floating point value arg.
  
   9) A set of overloads or a function template accepting an argument of any
    integral
  
   type. Equivalent to (6) (the argument is cast to double).
  
   For integral arguments, the integral overloads of std::abs are likely better
  
   matches. If std::abs is called with an unsigned integral argument that cannot
    be
  
   converted to int by integral promotion, the program is ill-formed.
Parameters¶
arg - Value of a floating-point or integral type
Return value¶
 If successful, returns the absolute value of arg (|arg|). The
    value returned is
  
   exact and does not depend on any rounding modes.
Error handling¶
 This function is not subject to any of the error conditions
    specified in
  
   math_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
Example¶
// Run this code
  
   #include <iostream>
  
   #include <cmath>
  
   int main()
  
   {
  
   std::cout << "abs(+3.0) = " << std::abs(+3.0) <<
    '\n'
  
   << "abs(-3.0) = " << std::abs(-3.0) << '\n';
  
   // special values
  
   std::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) = 3
  
   abs(-3.0) = 3
  
   abs(-0.0) = 0
  
   abs(-Inf) = inf
  
   abs(-NaN) = nan
  
   Defect reports
  
   The following behavior-changing defect reports were applied retroactively to
  
   previously published C++ standards.
  
   DR Applied to Behavior as published Correct behavior
  
   overloads of std::abs were declared these overloads in
  
   LWG 2192 C++98 inconsistently both headers
  
   declared in two headers
  
   overloads of std::abs for integer
  
   LWG 2735 C++11 types removed the requirement
  
   returning double was erroneously
  
   required
See also¶
 abs(int)
  
   labs computes absolute value of an integral value (\(\small{|x|}\)|x|)
  
   llabs (function)
  
   (C++11)
  
   copysign
  
   copysignf
  
   copysignl copies the sign of a floating point value
  
   (C++11) (function)
  
   (C++11)
  
   (C++11)
  
   signbit checks if the given number is negative
  
   (C++11) (function)
  
   abs(std::complex) returns the magnitude of a complex number
  
   (function template)
  
   abs(std::valarray) applies the function abs to each element of valarray
  
   (function template)
| 2022.07.31 | http://cppreference.com |