Defined in header <cmath> | ||
---|---|---|
float abs( float arg ); | (1) | |
double abs( double arg ); | (2) | |
long double abs( long double arg ); | (3) | |
double abs( Integral arg ); | (4) | (since C++11) |
float fabs( float arg ); | (5) | |
double fabs( double arg ); | (6) | |
long double fabs( long double arg ); | (7) | |
double fabs( Integral arg ); | (8) | (since C++11) |
1-3,5-7) Computes the absolute value of a floating point value
arg
. 4,8) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by (1-3,5-7). 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
.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
Notes
The overloads of std::abs
for integer types (4) disagree in return types with the integer overloads of std::abs
defined in <cstdlib>
, and they also introduce the dangerous overloads of std::abs
for unsigned integer types (Defect reports LWG 2192 and LWG 2294).
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
See also
(C++11) | computes absolute value of an integral value (|x|) (function) |
(C++11) | copies the sign of a floating point value (function) |
(C++11) | checks if the given number is negative (function) |
returns the magnitude of a complex number (function template) | |
applies the function std::abs to each element of valarray (function template) | |
C documentation for fabs |
Please login to continue.