std::sqrt(std::complex)(3)

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

🌸 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 z is (Β±0,+0), the result is (+0,+0).
  • If z is (x,+∞), the result is (+∞,+∞) even if x is NaN. 😭
  • If z is (x,NaN), the result is (NaN,NaN) (unless x is ±∞) and FE_INVALID may be raised.
  • If z is (-∞,y), the result is (+0,+∞) for finite positive y.
  • If z is (+∞,y), the result is (+∞,+0) for finite positive y.
  • If z is (-∞,NaN), the result is (NaN,∞) (sign of imaginary part unspecified).
  • If z is (+∞,NaN), the result is (+∞,NaN).
  • If z is (NaN,y), the result is (NaN,NaN) and FE_INVALID may be raised.
  • If z is (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 to C++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 function std::sqrt to 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