std::abs,st...td::imaxabs(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::abs, std::labs, std::llabs, std::imaxabs πŸ’–

Okay bestie, welcome to this C++ Standard Library page (man page section 3, iykyk) β€” it’s literally the absolute-value squad: std::abs, std::labs, std::llabs, and std::imaxabs. ✨

🌸 NAME 🌸

std::abs,std::labs,std::llabs,std::imaxabs β€” std::abs,std::labs,std::llabs,std::imaxabs


✨ Synopsis ✨

Here’s the whole lineup, with headers and overload numbers, exactly as they are:

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)

So what’s the vibe? It computes the absolute value of the integer number num. Heads up, bestie: the behavior is undefined if the result cannot be represented by the return type. That’s not a β€œmaybe it’s fine” situation β€” it’s undefined behavior, periodt.

Also, if std::abs is called with an unsigned integral argument that cannot be converted to int by integral promotion, the program is ill-formed. <em>Not cute, not allowed.</em>

And the very specific lore: overload (6) of std::abs for std::intmax_t is provided in <cinttypes> (since C++11) if and only if std::intmax_t is an extended integer type. 🫢


πŸŽ€ 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.

That’s the classic trap: INT_MIN is not about to hand you a cozy positive twin. The math says β€œout of range,” and the standard says β€œundefined behavior” when the result can’t be represented. πŸ’€

πŸ“ Example πŸ“

// Run this 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 🧚

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 2192 β€” Applied to: C++98. Behavior as published: overloads of std::abs were inconsistently declared in two headers. Correct behavior: declared these overloads in both headers.

🌈 See also 🌈

  • abs(float), fabs, fabsf, fabsl β€” absolute value of a floating point value (function); the math vibe is (\(\small{|x|}\)|x|); also (C++11) and (C++11) are in the mix.
  • abs(std::complex) β€” returns the magnitude of a complex number (function template)
  • abs(std::valarray) β€” applies the function abs to each element of valarray (function template)
  • C documentation for abs, labs, llabs

This iconic page is serving source/version http://cppreference.com, date 2024.06.10, and title std::abs,st...td::imaxabs(3) β€” bye bestie! πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from