std::abs(fl...f,std::fabsl(3)
std::abs(fl...f,std::fabsl(3) π
okay bestie, welcome to the C++ Standard Libary manual (yes, thatβs the spelling in this house π). This page is the whole vibe for std::abs(float), std::fabs, std::fabsf, and std::fabsl, and weβre gonna make it make sense without losing a single detail. π
πΈ NAME πΈ
The NAME line is literally std::abs(float),std::fabs,std::fabsf,std::fabsl β then a dash β then the exact same thing again: std::abs(float),std::fabs,std::fabsf,std::fabsl. No cap, thatβs the whole name section. π
π¦ Synopsis π¦
First thing: headers. std::abs is defined in header <cmath> and also in header <cstdlib>. The std::fabs crew is defined in header <cmath>. β¨
std::absoverloadsfloat abs( float num );double abs( double num );(until C++23)long double abs( long double num );constexpr /* floating-point-type */ abs( /* floating-point-type */ num );(since C++23)
std::fabsoverloads β defined in header<cmath>float fabs ( float num );double fabs ( double num );(until C++23)long double fabs ( long double num );(1)constexpr /* floating-point-type */ fabs ( /* floating-point-type */ num );(since C++23)float fabsf( float num );(2) (3)(since C++11)(constexpr since C++23)long double fabsl( long double num );(4)(since C++11)(constexpr since C++23)
- Additional overloads
(since C++11)- Defined in header
<cmath> template< class Integer >(since C++11)double fabs ( Integer num );(A)(constexpr since C++23)
- Defined in header
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)
A) Additional overloads are provided for all integer types, which are treated as double. (since C++11)
For integral arguments, the integral overloads of std::abs are likely better matches. If std::abs is called with an unsigned integral argument that cannot be converted to int by integral promotion, the program is ill-formed. Thatβs a hard βcompiler says nopeβ situation, bestie β not a vibe. π
π 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. π«Ά
π 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), then:
- If the argument is Β±0, +0 is returned.
- If the argument is Β±β, +β is returned.
- If the argument is NaN, NaN is returned.
β¨ 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<double>(num)). πΈ
π§ Example π§
// Run this code
#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';
}
π Possible output π
abs(+3.0) = 3
abs(-3.0) = 3
abs(-0.0) = 0
abs(-Inf) = inf
abs(-NaN) = nan
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2192β applied toC++98- Behavior as published: overloads of
std::abswere inconsistently declared in two headers. - Correct behavior: declared these overloads in both headers.
- Behavior as published: overloads of
LWG 2735β applied toC++11- Behavior as published: returning
doublewas erroneously required. - Correct behavior: removed the requirement.
- Behavior as published: returning
π See also π
abs(int)β theabs(int)entry, listed here with no extra description attached.labsβ computes absolute value of an integral value(\(\small{|x|}\)|x|)(function).llabsβ(function)(C++11).copysign,copysignf,copysignlβ copies the sign of a floating point value(function); these carry(C++11)markers.signbitβ checks if the given number is negative(function)(C++11).abs(std::complex)β returns the magnitude of a complex number(function template).abs(std::valarray)β applies the functionabsto each element ofvalarray(function template).- C documentation for
fabsβ listed in SEE ALSO.
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::abs(fl...f,std::fabsl(3) πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms