std::sqrt(std::complex)(3)
πΈ NAME πΈ
okay so bestie, welcome to the std::sqrt(std::complex)(3) page from the C++ Standard Libary β yes, that manual name is literally folded right in here, and we're about to make complex square roots make sense. π
std::sqrt(std::complex) β literally std::sqrt(std::complex), the complex square-root bestie. No cap, that's the whole name. π
β¨ Synopsis β¨
Here's the exact signature situation, copied with love:
Defined in header <complex>
template< class T >
std::complex<T> sqrt( const std::complex<T>& z );
So this function computes the square root of the complex number z with a branch cut along the negative real axis. Lowkey iconic, highkey important. π¦
π Parameters π
zβ the complex number you want to take the square root of. That's it, that's the parameter. π
π Return value π
If no errors occur, it returns the square root of z, in the range of the right half-plane, including the imaginary axis: [0; +β) along the real axis and (ββ; +β) along the imaginary axis. Slay. β¨
π«§ Error handling and special values π«§
Errors are reported consistent with math_errhandling.
If the implementation supports IEEE floating-point arithmetic:
- The function is continuous onto the branch cut taking into account the sign of imaginary part. π
std::sqrt(std::conj(z)) == std::conj(std::sqrt(z))- If
zis(Β±0,+0), the result is(+0,+0). - If
zis(x,+β), the result is(+β,+β)even ifxis NaN. π - If
zis(x,NaN), the result is(NaN,NaN)(unlessxisΒ±β) andFE_INVALIDmay be raised. - If
zis(-β,y), the result is(+0,+β)for finite positivey. - If
zis(+β,y), the result is(+β,+0)for finite positivey. - If
zis(-β,NaN), the result is(NaN,β)(sign of imaginary part unspecified). - If
zis(+β,NaN), the result is(+β,NaN). - If
zis(NaN,y), the result is(NaN,NaN)andFE_INVALIDmay be raised. - If
zis(NaN,NaN), the result is(NaN,NaN).
That's the whole special-values vibe check, bestie. Every single one. π
π¦ Notes π¦
The semantics of this function are intended to be consistent with the C function csqrt. Periodt. π
π Example π
Run this code, bestie:
#include <complex>
#include <iostream>
int main()
{
std::cout << "Square root of -4 is "
<< std::sqrt(std::complex<double>(-4.0, 0.0)) << '\n'
<< "Square root of (-4,-0) is "
<< std::sqrt(std::complex<double>(-4.0, -0.0))
<< " (the other side of the cut)\n";
}
π Output π
Square root of -4 is (0,2)
Square root of (-4,-0) is (0,-2) (the other side of the cut)
See how the signed zero changes sides of the cut? The math is mathing. π
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2597β applied toC++98; behavior as published: specification mishandles signed zero imaginary parts; correct behavior: erroneous requirement removed.
Retroactive glow-up, honestly. π
π See also π
pow(std::complex)β complex power, one or both arguments may be a complex number (function template). π₯sqrtβ computes square root ((\small{\sqrt{x}})) (function).sqrtfβ (C++11) computes square root (function).sqrtlβ (C++11) computes square root (function).sqrt(std::valarray)β applies the functionstd::sqrtto each element of valarray (function template). β¨- C documentation for
csqrtβ the C bestie counterpart, iykyk.
mwah π β source/version: http://cppreference.com, date 2024.06.10, page std::sqrt(std::complex)(3). Bye bestie, go take some square roots! π«Ά
π¦ 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