std::abs(std::complex)(3)
π 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 anabs. 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 ourabsis secretly built on. Keep your friends close, keep yourhypotcloser π«Άabs(std::valarray)β applies the functionabsto each element ofvalarray(function template) π- C documentation for
cabsβ for when you wanna see how the C people do it π¦
lowkey thehypotentry 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms