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> | ||
(4) | ||
float fabs ( float arg ); | ||
float fabsf( float arg ); | (since C++11) | |
double fabs ( double arg ); | (5) | |
(6) | ||
long double fabs ( long double arg ); | ||
long double fabsl( long double arg ); | (since C++11) | |
double fabs ( IntegralType arg ); | (7) | (since C++11) |
arg
.double
). For integral arguments, the integral overloads of | (since C++17) |
arg | - | Value of a floating-point or Integral type |
If successful, returns the absolute value of arg
(|arg|
). The value returned is exact and does not depend on any rounding modes.
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),
#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
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2735 | C++11 | std::abs was erroneously required to have overloads for integer types returning double | removed the requirement |
(C++11) | computes absolute value of an integral value (|x|) (function) |
(C++11)(C++11)(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) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/math/fabs