square root function

You're viewing an older version of this page (#3278). View the current version.

View versions (4)

NAME

sqrt

INTERFACE

#include <math.h>

double sqrt ( double x ) long sqrtl ( long double x ) float sqrtf ( float x )

#include <complex.h> complex sqrt(complex x) double complex csqrt(double complex x) float complex csqrtf(float complex x) long double complex csqrtl(long double complex x)

DESCRIPTION

The sqrt function compute the non-negative square root of x.

For complex numbers x, sqrt(x) give the complex root with arguments defined by References:arg (x)/2

\sqrt{x} = \sqrt { abs(x)} \left ( \cos(\frac{arg(x)}{2}) + i sin(\frac{arg(x)}{2})  \right )
(1)

Example 1

#include <stdio.h>
#include <math.h>

int main()
{
  double x=4;
  double result = sqrt(x);
  printf("The square root of %lf is %lf\n", x, result);
  return 0;
}

Ouput:

The square root of 4 is 2

SPECIAL VALUES

sqrt ( -0 ) returns -0.
sqrt ( x ) returns a NaN and generates a domain error for x < 0.

See Also

References:arg, References:abs

STANDARDS

The sqrt function conforms to ISO/IEC 9899:1999(E).

COMPATIBILITY

<table class="compat"> <tr><th></th><th>DOS</th><th>UNIX</th><th>Windows</th><th>ANSI C</th><th>C++ only</th></tr> <tr><td>sqrtl</td><td>&bull;</td><td></td><td>&bull;</td><td></td><td></td></tr> <tr><td>Real sqrt</td><td>&bull;</td><td>&bull;</td><td>&bull;</td><td>&bull;</td><td></td></tr> <tr><td>Complex sqrt</td><td>&bull;</td><td></td><td>&bull;</td><td></td><td>&bull;</td></tr> <tr><td>Complex csqrt</td><td></td><td>&bull;</td><td></td><td></td><td>&bull;</td></tr> </table>