Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Functions | ||||||||||||||||
Basic operations | ||||||||||||||||
| ||||||||||||||||
Exponential functions | ||||||||||||||||
Power functions | ||||||||||||||||
Trigonometric and hyperbolic functions | ||||||||||||||||
Error and gamma functions | ||||||||||||||||
Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||||||||||||
Macro constants | |||||||||||||||||||||||||||||||||||||||||
|
|
Defined in header <cmath> | ||
Defined in header <cstdlib> | ||
(1) | ||
float abs(float num); double abs(double 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); | (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) |
Additional overloads(since C++11) | ||
Defined in header <cmath> | ||
template<class Integer> double fabs( Integer num); | (A) | (since C++11) (constexpr since C++23) |
std::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 |
num | - | floating-point or integer value |
If successful, returns the absolute value ofarg (|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 inmath_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
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)).
Possible output:
abs(+3.0) = 3abs(-3.0) = 3abs(-0.0) = 0abs(-Inf) = infabs(-NaN) = nan
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2192 | C++98 | overloads ofstd::abs wereinconsistently declared in two headers | declared these overloads in both headers |
LWG 2735 | C++11 | overloads ofstd::abs for integer typesreturningdouble was erroneously required | removed the requirement |
(C++11) | 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] | |
C documentation forfabs |