std::abs(float), std::fabs, std::fabsf, std::fabsl
Header: <cmath>
1-4) Computes the absolute value of the floating-point value num.The library provides overloads of std::abs and std::fabs for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)
# Declarations
float abs( float num );
double abs( double num );
long double abs( long double num );
(until C++23)
constexpr /* floating-point-type */
abs( /* floating-point-type */ num );
(since C++23)
float fabs ( float num );
double fabs ( double num );
long double fabs ( long double num );
(until C++23)
constexpr /* floating-point-type */
fabs ( /* floating-point-type */ num );
(since C++23)
float fabsf( float num );
(since C++11) (constexpr since C++23)
long double fabsl( long double num );
(since C++11) (constexpr since C++23)
Additional overloads (since C++11)
template< class Integer >
double fabs ( Integer num );
(since C++11) (constexpr since C++23)
# Parameters
num: floating-point or integer value
# Return value
If successful, returns the absolute value of arg (|arg|). The value returned is exact and does not depend on any rounding modes.
# Notes
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::fabs(num) has the same effect as std::fabs(static_cast
# Example
#include <cmath>
#include <iostream>
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';
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2192 | C++98 | overloads of std::abs wereinconsistently declared in two headers | declared these overloadsin both headers |
| LWG 2735 | C++11 | overloads of std::abs for integer typesreturning double was erroneously required | removed the requirement |