std::arg(std::complex)(3)

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

πŸ’… NAME

std::arg(std::complex) β€” bestie, this is std::arg(std::complex), the function that tells you the phase angle of a complex number. It lives in πŸ“š C++ Standard Library (manual std::arg(std::complex)(3)), and no cap, it's lowkey one of the most useful little guys in <complex>.


✨ SYNOPSIS ✨

Okay so first things first: this whole thing lives in the header <complex> πŸŽ€

Form 1, the main character:

template< class T >                          (1)
T           arg( const std::complex<T>& z );

Then we've got the additional overloads (since C++11), also defined in header <complex>:

float       arg( float f );

double      arg( double f );                         (until C++23)

long double arg( long double f );
template< class FloatingPoint >              (A)

FloatingPoint                                        (since C++23)

            arg( FloatingPoint f );
template< class Integer >                        (B)
double      arg( Integer i );

Here's what they mean, in bestie terms 🫢:

  • 1) Calculates the phase angle (in radians) of the complex number z. That's it, that's the vibe.
  • A,B) Additional overloads are provided for all integer and floating-point types, which are treated as complex numbers with zero imaginary component (since C++11). Basically the plain numbers get to sit at the cool table too πŸ’….

🌸 PARAMETERS 🌸

  • z β€” complex value. The full complex-number experience πŸ¦‹
  • f β€” floating-point value. Just a regular ol' float living its life πŸ™ˆ
  • i β€” integer value. Whole numbers are welcome here ⭐

πŸ‘‘ RETURN VALUE πŸ‘‘

  • 1) std::atan2(std::imag(z), std::real(z)). If no errors occur, this is the phase angle of z in the interval [βˆ’Ο€; Ο€]. Iconic, bounded, well-behaved πŸ’–
  • A) Zero if f is positive or +0, Ο€ if f is negative or -0, NaN otherwise. Yes, that "otherwise" is doing a lot of heavy lifting 😭
  • B) Zero if i is non-negative, Ο€ if it is negative.

πŸ’œ NOTES πŸ’œ

So here's the tea, bestie β˜• β€” the additional overloads are not required to be provided exactly as (A,B). They only need to be sufficient to ensure that for their argument num:

  • If num has a standard (until C++23) floating-point type T, then std::arg(num) has the same effect as std::arg(std::complex<T>(num)). Same energy, different wrapper ✨
  • Otherwise, if num has an integer type, then std::arg(num) has the same effect as std::arg(std::complex<double>(num)). Integers get promoted to double, slay πŸŽ€

πŸ¦‹ EXAMPLE πŸ¦‹

// Run this code β€” literally just copy-paste it, it's fine, I promise 🫢

#include <complex>
#include <iostream>

int main()
{
    std::complex<double> z1(1, 0);
    std::complex<double> z2(0, 0);
    std::complex<double> z3(0, 1);
    std::complex<double> z4(-1, 0);
    std::complex<double> z5(-1, -0.0);
    double f = 1.;
    int i = -1;

    std::cout << "phase angle of " << z1 << " is " << std::arg(z1) << '\n'
              << "phase angle of " << z2 << " is " << std::arg(z2) << '\n'
              << "phase angle of " << z3 << " is " << std::arg(z3) << '\n'
              << "phase angle of " << z4 << " is " << std::arg(z4) << '\n'
              << "phase angle of " << z5 << " is " << std::arg(z5) << " "
                 "(the other side of the cut)\n"
              << "phase angle of " << f << " is " << std::arg(f) << '\n'
              << "phase angle of " << i << " is " << std::arg(i) << '\n';

}

⭐ Output

phase angle of (1,0) is 0
phase angle of (0,0) is 0
phase angle of (0,1) is 1.5708
phase angle of (-1,0) is 3.14159
phase angle of (-1,-0) is -3.14159 (the other side of the cut)
phase angle of 1 is 0
phase angle of -1 is 3.14159

Not the -0 giving a whole different answer 😭 that's the branch cut being dramatic, iykyk πŸ’€


🌈 SEE ALSO 🌈

  • abs(std::complex) β€” returns the magnitude of a complex number (function template) πŸ’ͺ
  • polar β€” constructs a complex number from magnitude and phase angle (function template) πŸŒ€
  • atan2, atan2f, atan2l (C++11) (C++11) β€” arc tangent, using signs to determine quadrants (function). Quadrant awareness is so main character energy πŸ‘‘
  • atan2(std::valarray) β€” applies the function std::atan2 to a valarray and a value (function template) πŸ“
  • C documentation for carg πŸ“–

Ok bye bestie, go rotate something πŸŒ€πŸ’–

Big kisses from the cppreference.com girlies β€” this page is from the 2024.06.10 snapshot of the C++ Standard Library manual (std::arg(std::complex)(3)), and yes, the date is in the footer where it belongs, we're not copying that line, we're honoring it βœ¨πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from