std::abs, std::labs, std::llabs, std::imaxabs

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
Numerics library
Common mathematical functions
Mathematical special functions (哋它亢++17)
Mathematical constants (哋它亢++20)
Basic linear algebra algorithms (哋它亢++26)
Floating-point environment (哋它亢++11)
Complex numbers
Numeric arrays
Pseudo-random number generation
Factor operations
(哋它亢++17)
(哋它亢++17)
Interpolations
(哋它亢++20)
(哋它亢++20)
Saturation arithmetic
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)

Generic numeric operations
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
Bit operations
(哋它亢++20)    
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++20)
 
Common mathematical functions
Functions
Basic operations
abs(int)labsllabsimaxabs
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
Exponential functions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Power functions
(哋它亢++11)
(哋它亢++11)
Trigonometric and hyperbolic functions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Error and gamma functions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Nearest integer floating point operations
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
Floating point manipulation functions
(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)
Classification/Comparison
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Macro constants
(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)(哋它亢++11)(哋它亢++11)
 
Defined in header <cstdlib>
Defined in header <cmath>
int       abs( int num );
(1) (constexpr since 哋它亢++23)
long      abs( long num );
(2) (constexpr since 哋它亢++23)
long long abs( long long num );
(3) (since 哋它亢++11)
(constexpr since 哋它亢++23)
Defined in header <cstdlib>
long       labs( long num );
(4) (constexpr since 哋它亢++23)
long long llabs( long long num );
(5) (since 哋它亢++11)
(constexpr since 哋它亢++23)
Defined in header <cinttypes>
(6) (since 哋它亢++11)
(constexpr since 哋它亢++23)
(7) (since 哋它亢++11)
(constexpr since 哋它亢++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 哋它亢++11)

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 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2192 哋它亢++98 overloads of std::abs were
inconsistently declared in two headers
declared these overloads
in both headers

See also

(哋它亢++11)(哋它亢++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)