Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
abs(float)fabs
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
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)
(C++11)(C++11)
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 
Defined in header <cmath>
Defined in header <cstdlib>
(since C++17)
float       abs( float arg );
(1)
double      abs( double arg );
(2)
long double abs( long double arg );
(3)
Defined in header <cmath>
float       fabs( float arg );
(4)
double      fabs( double arg );
(5)
long double fabs( long double arg );
(6)
double      fabs( Integral arg );
(7) (since C++11)
1-6) Computes the absolute value of a floating point value arg.
7) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by (4-6). If any argument has integral type, it is cast to double. If any other argument is long double, then the return type is long double, otherwise it is double.
For integral arguments, the integral overloads of std::abs are likely better matches. If std::abs is called with an argument of type X such that std::is_unsigned<X>::value is true and X cannot be converted to int by integral promotion, the program is ill-formed. (since C++17)

Contents

[edit] Parameters

arg - Value of a floating-point or Integral type

[edit] Return value

If successful, returns the absolute value of arg (|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 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

[edit] Notes

Between C++11 and C++14, the standard erroneously required std::abs to have overloads for integer types returning double. This requirement was removed in C++17 by defect report 2735.

[edit] Example

#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';
}

Possible output:

abs(+3.0) = 3
abs(-3.0) = 3
abs(-0.0) = 0
abs(-Inf) = inf

[edit] See also

computes absolute value of an integral value (|x|)
(function) [edit]
(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 function std::abs to each element of valarray
(function template) [edit]