Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/abs"

From cppreference.com
< cpp‎ | numeric‎ | math
(+see c)
m (Notes: +link to 2's complement)
 
(21 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{cpp/title|abs{{small|(int)}}|labs|llabs|imaxabs}}
+
{{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 |
+
{{dcl header|cmath}}
int      abs( int n );
+
{{dcl|num=1|notes={{mark|constexpr since C++23}}|
 +
int      abs( int num );
 
}}
 
}}
{{dcl |
+
{{dcl|num=2|notes={{mark|constexpr since C++23}}|
long      abs( long n );
+
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 n );
+
long long abs( long long num );
 
}}
 
}}
{{dcl |
+
{{dcl header|cstdlib}}
long      labs( long n );
+
{{dcl|num=4|notes={{mark|constexpr since C++23}}|
 +
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 n );
+
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 n );
+
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 n );
+
std::intmax_t imaxabs( std::intmax_t num );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Computes the absolute value of an integer number.
+
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 | n | integer value}}
+
{{par|num|integer value}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
The absolute value of {{tt|n}} (i.e. {{tt|{{!}}n{{!}}}}).
+
The absolute value of {{c|num}} (i.e. {{tt|{{!}}num{{!}}}}), if it is representable.
 +
 
 +
===Notes===
 +
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|
 +
|code=
 +
#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
 +
}}
 +
 
 +
===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}}
  
[[de:cpp/numeric/math/abs]]
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|tr|zh}}
[[es:cpp/numeric/math/abs]]
+
[[fr:cpp/numeric/math/abs]]
+
[[it:cpp/numeric/math/abs]]
+
[[ja:cpp/numeric/math/abs]]
+
[[pl:cpp/numeric/math/abs]]
+
[[pt:cpp/numeric/math/abs]]
+
[[ru:cpp/numeric/math/abs]]
+
[[tr:cpp/numeric/math/abs]]
+
[[zh:cpp/numeric/math/abs]]
+

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>
(6) (since C++11)
(constexpr since C++23)
(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 std::abs for std::intmax_t is provided in <cinttypes> if and only if std::intmax_t is an extended integer type.

(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 were
inconsistently declared in two headers
declared these overloads
in both headers

[edit] See also

absolute value of a floating point value (|x|)
(function) [edit]
returns the magnitude of a complex number
(function template) [edit]
applies the function abs to each element of valarray
(function template) [edit]
C documentation for abs, labs, llabs