std::arg(std::complex)(3)
π 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 ofzin the interval[βΟ; Ο]. Iconic, bounded, well-behaved π - A) Zero if
fis positive or+0,Οiffis negative or-0,NaNotherwise. Yes, that "otherwise" is doing a lot of heavy lifting π - B) Zero if
iis 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
numhas a standard (until C++23) floating-point typeT, thenstd::arg(num)has the same effect asstd::arg(std::complex<T>(num)). Same energy, different wrapper β¨ - Otherwise, if
numhas an integer type, thenstd::arg(num)has the same effect asstd::arg(std::complex<double>(num)). Integers get promoted todouble, 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 functionstd::atan2to 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
- 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