std::abs(std::complex)(3)

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

πŸ’… std::abs(std::complex) β€” the vibe

okay bestie, so this is a page from the C++ Standard Library man pages (that's the manual name, consider it folded in πŸŽ€), and we're talking about std::abs(std::complex), literally the same name as the thing it does. Iconic behavior, no notes.


✨ NAME ✨

std::abs(std::complex) β€” which is, and I cannot stress this enough, std::abs(std::complex). That's the whole name. We love a self-aware moment πŸ’–


🌸 SYNOPSIS 🌸

So first things first, this bad boy lives in the header <complex>:

Defined in header <complex>
template< class T >
T abs( const complex<T>& z );

And what does it do? It returns the magnitude of the complex number z. That's it. That's the whole job. She's focused, she's employed, she's slaying 🫢


πŸ¦‹ PARAMETERS πŸ¦‹

  • z β€” a complex value. Just... a complex number. Hand it over. That's the assignment πŸ’…

⭐ RETURN VALUE ⭐

If nothing goes wrong (and honestly, when does it ever), it returns the absolute value of z β€” also known as the norm, the modulus, or the magnitude, depending on which friend you ask. Same thing, four nicknames, main character energy πŸ‘‘

Now here's the important bit, ngl β€” errors and special cases are handled as if the function is implemented as:

std::hypot(std::real(z), std::imag(z))

That's not me being cute, that's literally the spec. If you need to know how weird edge cases behave, that's the recipe it follows, so go look at std::hypot for the drama 😭


🧚 EXAMPLE 🧚

// Run this code

#include <complex>
#include <iostream>

int main()
{
    std::complex<double> z(1, 1);
    std::cout << z << " cartesian is rho = " << std::abs(z)
              << " theta = " << std::arg(z) << " polar\n";
}

🌈 Output

(1,1) cartesian is rho = 1.41421 theta = 0.785398 polar

Look at her go πŸ₯Ή converting cartesian to polar in like four lines. rho is the magnitude (that's our function, hiiii πŸ’–) and theta is the phase angle. Bestie's out here doing actual math.


πŸ”₯ SEE ALSO πŸ”₯

Everything else you might want in the complex-number friend group:

  • arg β€” returns the phase angle (function template) 🌸
  • polar β€” constructs a complex number from magnitude and phase angle (function template) πŸŽ€
  • abs(int), labs, llabs (C++11) β€” computes absolute value of an integral value ((\small{|x|})|x|) (function). Yes, the integer squad also gets an abs. Everybody eats πŸ’…
  • abs(float), fabs, fabsf, fabsl (C++11), (C++11) β€” absolute value of a floating point value ((\small{|x|})|x|) (function). The float girlies needed their own versions, obviously ✨
  • hypot, hypotf, hypotl (C++11), (C++11), (C++11) β€” computes square root of the sum of the squares of two or three (since C++17) given numbers, i.e. ((\scriptsize{\sqrt{x^2+y^2}}) √ x2 +y2 ) and, (since C++17), ((\scriptsize{\sqrt{x^2+y^2+z^2}}) √ x2 +y2 +z2 ) (since C++17) (function). This is the one our abs is secretly built on. Keep your friends close, keep your hypot closer 🫢
  • abs(std::valarray) β€” applies the function abs to each element of valarray (function template) 🌟
  • C documentation for cabs β€” for when you wanna see how the C people do it πŸ¦‹
lowkey the hypot entry above is doing gymnastics with the √ symbols, but that's just how the page renders the formula β€” the math itself is √(xΒ²+yΒ²) and √(xΒ²+yΒ²+zΒ²), iykyk πŸ’€

πŸ’œ the sign-off πŸ’œ

this page came from cppreference.com, dated 2024.06.10, and it's the manual page for std::abs(std::complex) β€” section 3, in case you were collecting them πŸŽ€ study it, love it, go compute some magnitudes with it. periodt. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from