Namespaces
Variants
Views
Actions

csqrtf, csqrt, csqrtl

From cppreference.com
< c‎ | numeric‎ | complex
Defined in header <complex.h>
float complex       csqrtf( float complex z );
(1) (since C99)
double complex      csqrt( double complex z );
(2) (since C99)
long double complex csqrtl( long double complex z );
(3) (since C99)
Defined in header <tgmath.h>
#define sqrt( z )
(4) (since C99)
1-3) Computes the complex square root of z with branch cut along the negative real axis.
4) Type-generic macro: If z has type long double complex, csqrtl is called. if z has type double complex, csqrt is called, if z has type float complex, csqrtf is called. If z is real or integer, then the macro invokes the corresponding real function (sqrtf, sqrt, sqrtl). If z is imaginary, the corresponding complex number version is called.

Contents

[edit] Parameters

z - complex argument

[edit] Return value

If no errors occur, 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.)

[edit] 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
  • csqrt(conj(z)) == conj(csqrt(z))
  • If z is ±0+0i, the result is +0+0i
  • If z is x+∞i, the result is +∞+∞i even if x is NaN
  • If z is x+NaNi, the result is NaN+NaNi (unless x is ±∞) and FE_INVALID may be raised
  • If z is -∞+yi, the result is +0+∞i for finite positive y
  • If z is +∞+yi, the result is +∞+0i) for finite positive y
  • If z is -∞+NaNi, the result is NaN±∞ (sign of imaginary part unspecified)
  • If z is +∞+NaNi, the result is +∞+NaNi
  • If z is NaN+yi, the result is NaN+NaNi and FE_INVALID may be raised
  • If z is NaN+NaNi, the result is NaN+NaNi

[edit] Example

#include <stdio.h>
#include <complex.h>
 
int main(void)
{
    double complex z1 = csqrt(-4);
    printf("Square root of -4 is %.1f%+.1fi\n", creal(z1), cimag(z1));
 
    double complex z2 = conj(z1); // or, in C11, CMPLX(-4, -0.0)
    printf("Square root of -4-0i, the other side of the cut, is "
           "%.1f%+.1fi\n", creal(z2), cimag(z2));
}

Output:

Square root of -4 is 0.0+2.0i
Square root of -4-0i, the other side of the cut, is 0.0-2.0i

[edit] See also

(C99)(C99)(C99)
computes the complex power function
(function) [edit]
(C99)(C99)
computes square root (x)
(function) [edit]