csqrtf, csqrt, csqrtl - cppreference.com (original) (raw)
| 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.
- Type-generic macro: If
zhas type long double complex,csqrtlis called. ifzhas type double complex,csqrtis called, ifzhas type float complex,csqrtfis called. Ifzis real or integer, then the macro invokes the corresponding real function (sqrtf, sqrt, sqrtl). Ifzis imaginary, the corresponding complex number version is called.
Contents
[edit] Parameters
[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
zis±0+0i, the result is+0+0i - If
zisx+∞i, the result is+∞+∞ieven if x is NaN - If
zisx+NaNi, the result isNaN+NaNi(unless x is ±∞) and FE_INVALID may be raised - If
zis-∞+yi, the result is+0+∞ifor finite positive y - If
zis+∞+yi, the result is+∞+0i)for finite positive y - If
zis-∞+NaNi, the result isNaN±∞i(sign of imaginary part unspecified) - If
zis+∞+NaNi, the result is+∞+NaNi - If
zisNaN+yi, the result isNaN+NaNiand FE_INVALID may be raised - If
zisNaN+NaNi, the result isNaN+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 = csqrt(conj(-4)); // 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] References
C11 standard (ISO/IEC 9899:2011):
7.3.8.3 The csqrt functions (p: 196)
7.25 Type-generic math <tgmath.h> (p: 373-375)
G.6.4.2 The csqrt functions (p: 544)
G.7 Type-generic math <tgmath.h> (p: 545)
C99 standard (ISO/IEC 9899:1999):
7.3.8.3 The csqrt functions (p: 178)
7.22 Type-generic math <tgmath.h> (p: 335-337)
G.6.4.2 The csqrt functions (p: 479)
G.7 Type-generic math <tgmath.h> (p: 480)