Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::sqrt(std::complex)

      From cppreference.com
      <cpp‎ |numeric‎ |complex
       
       
       
       
      Defined in header<complex>
      template<class T>
      std::complex<T> sqrt(conststd::complex<T>& z);

      Computes the square root of the complex numberz with a branch cut along the negative real axis.

      Contents

      [edit]Parameters

      z - complex number to take the square root of

      [edit]Return value

      If no errors occur, returns the square root ofz, in the range of the right half-plane, including the imaginary axis ([0; +∞) along the real axis and(−∞; +∞) along the imaginary axis).

      [edit]Error handling and special values

      Errors are reported consistent withmath_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))
      • Ifz is(±0,+0), the result is(+0,+0)
      • Ifz is(x,+∞), the result is(+∞,+∞) even if x is NaN
      • Ifz is(x,NaN), the result is(NaN,NaN) (unless x is ±∞) andFE_INVALID may be raised
      • Ifz is(-∞,y), the result is(+0,+∞) for finite positive y
      • Ifz is(+∞,y), the result is(+∞,+0) for finite positive y
      • Ifz is(-∞,NaN), the result is(NaN,∞) (sign of imaginary part unspecified)
      • Ifz is(+∞,NaN), the result is(+∞,NaN)
      • Ifz is(NaN,y), the result is(NaN,NaN) andFE_INVALID may be raised
      • Ifz is(NaN,NaN), the result is(NaN,NaN)

      [edit]Notes

      The semantics of this function are intended to be consistent with the C functioncsqrt.

      [edit]Example

      Run this code
      #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)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2597C++98specification mishandles signed zero imaginary partserroneous requirement removed

      [edit]See also

      complex power, one or both arguments may be a complex number
      (function template)[edit]
      (C++11)(C++11)
      computes square root (\(\small{\sqrt{x}}\)x)
      (function)[edit]
      applies the functionstd::sqrt to each element of valarray
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/complex/sqrt&oldid=150872"

      [8]ページ先頭

      ©2009-2025 Movatter.jp