Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | math
(tuple page style constexpr is less embarrassing)
(Applied LWG 3834.)
Line 2: Line 2:
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cstdlib}}
+
{{dcl header|cstdlib}}
{{dcl header | cmath}}
+
{{dcl header|cmath}}
{{dcl | num=1 | notes={{mark|constexpr since C++23}} |
+
{{dcl|num=1|notes={{mark|constexpr since C++23}}|
int      abs( int n );
+
int      abs( int num );
 
}}
 
}}
{{dcl | num=2 | notes={{mark|constexpr since C++23}} |
+
{{dcl|num=2|notes={{mark|constexpr since C++23}}|
long      abs( long n );
+
long      abs( long num );
 
}}
 
}}
{{dcl | num=3 | since=c++11 | notes={{mark|constexpr since C++23}} |
+
{{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 header | cstdlib}}
+
{{dcl header|cstdlib}}
{{dcl | num=4 | notes={{mark|constexpr since C++23}} |
+
{{dcl|num=4|notes={{mark|constexpr since C++23}}|
long      labs( long n );
+
long      labs( long num );
 
}}
 
}}
{{dcl | num=5 | since=c++11 | notes={{mark|constexpr since C++23}} |
+
{{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 | num=6 | 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 | num=7 | 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. The behavior is undefined if the result cannot be represented by the return type.
+
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|int}} by [[cpp/language/implicit conversion#Integral promotion|integral promotion]], the program is ill-formed.
+
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|
 
{{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 extended integer type.
+
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{{!}}}}), if it is representable.
+
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 {{c|int}}, {{lc|INT_MIN}} is {{c|-2147483648}}, but the would-be result {{c|2147483648}} is greater than {{lc|INT_MAX}}, which is {{c|2147483647}}.
+
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 {{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=
+
|code=
 +
#include <climits>
 +
#include <cstdlib>
 
#include <iostream>
 
#include <iostream>
#include <cstdlib>
 
#include <climits>
 
  
 
int main()
 
int main()
Line 60: Line 60:
 
               << "abs(+3) = " << std::abs(3) << '\n'
 
               << "abs(+3) = " << std::abs(3) << '\n'
 
               << "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
 
//  std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems
 
}
 
}
| output=
+
|output=
 
abs(+3) = +3
 
abs(+3) = +3
 
abs(-3) = +3
 
abs(-3) = +3
Line 70: Line 70:
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
{{dr list item|std=C++98|wg=lwg|dr=2192|before=overloads of {{tt|std::abs}} were inconsistently<br>declared in two headers|after=declared these overloads in both headers}}
+
{{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}}
 
{{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}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|tr|zh}}

Revision as of 19:11, 6 April 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

Parameters

num - integer value

Return value

The absolute value of num (i.e. |num|), if it is representable.

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.

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

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

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