Difference between revisions of "cpp/numeric/math/abs"
(LWG 2192) |
m (→Notes: +link to 2's complement) |
||
(19 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title|abs | + | {{cpp/title|abs|labs|llabs|imaxabs}} |
{{cpp/numeric/math/navbar}} | {{cpp/numeric/math/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | cstdlib}} | + | {{dcl header|cstdlib}} |
− | {{dcl header | cmath | notes={{mark since | + | {{dcl header|cmath}} |
− | + | {{dcl|num=1|notes={{mark|constexpr since C++23}}| | |
− | int abs( int | + | int abs( int num ); |
}} | }} | ||
− | {{dcl | | + | {{dcl|num=2|notes={{mark|constexpr since C++23}}| |
− | long abs( long | + | long abs( long num ); |
}} | }} | ||
− | {{dcl | since=c++11 | | + | {{dcl|num=3|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long long abs( long long | + | long long abs( long long num ); |
}} | }} | ||
− | {{dcl header | cstdlib}} | + | {{dcl header|cstdlib}} |
− | {{dcl | | + | {{dcl|num=4|notes={{mark|constexpr since C++23}}| |
− | long labs( long | + | long labs( long num ); |
}} | }} | ||
− | {{dcl | since=c++11 | | + | {{dcl|num=5|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long long llabs( long long | + | long long llabs( long long num ); |
}} | }} | ||
− | {{dcl header | cinttypes}} | + | {{dcl header|cinttypes}} |
− | {{dcl | since=c++11 | | + | {{dcl|num=6|since=c++11|notes={{mark|constexpr since C++23}}| |
− | std::intmax_t abs( std::intmax_t | + | std::intmax_t abs( std::intmax_t num ); |
}} | }} | ||
− | {{dcl | since=c++11 | | + | {{dcl|num=7|since=c++11|notes={{mark|constexpr since C++23}}| |
− | std::intmax_t imaxabs( std::intmax_t | + | std::intmax_t imaxabs( std::intmax_t num ); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Computes the absolute value of | + | Computes the absolute value of the integer number {{c|num}}. The behavior is undefined if the result cannot be represented by the return type. |
+ | |||
+ | If {{tt|std::abs}} is called with an unsigned integral argument that cannot be converted to {{c/core|int}} by [[cpp/language/implicit conversion#Integral promotion|integral promotion]], the program is ill-formed. | ||
+ | |||
+ | {{rrev|since=c++11| | ||
+ | Overload {{v|6}} of {{tt|std::abs}} for {{lc|std::intmax_t}} is provided in {{header|cinttypes}} if and only if {{lc|std::intmax_t}} is an [[cpp/language/types#Extended integer types|extended integer type]]. | ||
+ | }} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | | + | {{par|num|integer value}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | The absolute value of {{ | + | The absolute value of {{c|num}} (i.e. {{tt|{{!}}num{{!}}}}), if it is representable. |
===Notes=== | ===Notes=== | ||
− | In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647. | + | In [[cpp/language/types#Range of values|2's complement]] systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement type {{c/core|int}}, {{lc|INT_MIN}} is {{c|-2147483648}}, but the would-be result {{c|2147483648}} is greater than {{lc|INT_MAX}}, which is {{c|2147483647}}. |
===Example=== | ===Example=== | ||
− | {{example | | + | {{example| |
− | + | |code= | |
− | + | ||
− | + | ||
#include <climits> | #include <climits> | ||
+ | #include <cstdlib> | ||
+ | #include <iostream> | ||
int main() | int main() | ||
{ | { | ||
− | std::cout << "abs(+3) = " << std::abs(3) << '\n' | + | std::cout << std::showpos |
+ | << "abs(+3) = " << std::abs(3) << '\n' | ||
<< "abs(-3) = " << std::abs(-3) << '\n'; | << "abs(-3) = " << std::abs(-3) << '\n'; | ||
− | + | ||
− | // std::cout << abs(INT_MIN | + | // std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems |
} | } | ||
− | + | |output= | |
− | abs(+3) = 3 | + | abs(+3) = +3 |
− | abs(-3) = 3 | + | abs(-3) = +3 |
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|std=C++98|wg=lwg|dr=2192|before=overloads of {{tt|std::abs}} were<br>inconsistently declared in two headers|after=declared these overloads<br>in both headers}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/math/dsc fabs}} | + | {{dsc inc|cpp/numeric/math/dsc fabs}} |
− | {{dsc inc | cpp/numeric/complex/dsc abs}} | + | {{dsc inc|cpp/numeric/complex/dsc abs}} |
− | {{dsc inc | cpp/numeric/valarray/dsc abs}} | + | {{dsc inc|cpp/numeric/valarray/dsc abs}} |
− | {{dsc see c | c/numeric/math/abs | abs | labs | llabs}} | + | {{dsc see c|c/numeric/math/abs|abs|labs|llabs}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pl|pt|ru|tr|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 14:25, 19 November 2023
Defined in header <cstdlib>
|
||
Defined in header <cmath>
|
||
int abs( int num ); |
(1) | (constexpr since C++23) |
long abs( long num ); |
(2) | (constexpr since C++23) |
long long abs( long long num ); |
(3) | (since C++11) (constexpr since C++23) |
Defined in header <cstdlib>
|
||
long labs( long num ); |
(4) | (constexpr since C++23) |
long long llabs( long long num ); |
(5) | (since C++11) (constexpr since C++23) |
Defined in header <cinttypes>
|
||
std::intmax_t abs( std::intmax_t num ); |
(6) | (since C++11) (constexpr since C++23) |
std::intmax_t imaxabs( std::intmax_t num ); |
(7) | (since C++11) (constexpr since C++23) |
Computes the absolute value of the integer number num. The behavior is undefined if the result cannot be represented by the return type.
If std::abs
is called with an unsigned integral argument that cannot be converted to int by integral promotion, the program is ill-formed.
Overload (6) of |
(since C++11) |
Contents |
[edit] Parameters
num | - | integer value |
[edit] Return value
The absolute value of num (i.e. |num|
), if it is representable.
[edit] Notes
In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647.
[edit] Example
#include <climits> #include <cstdlib> #include <iostream> int main() { std::cout << std::showpos << "abs(+3) = " << std::abs(3) << '\n' << "abs(-3) = " << std::abs(-3) << '\n'; // std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems }
Output:
abs(+3) = +3 abs(-3) = +3
[edit] Defect reports
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 of std::abs wereinconsistently declared in two headers |
declared these overloads in both headers |
[edit] See also
(C++11)(C++11) |
absolute value of a floating point value (|x|) (function) |
returns the magnitude of a complex number (function template) | |
applies the function abs to each element of valarray (function template) | |
C documentation for abs, labs, llabs
|