square root function

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

View versions (4)

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 computes the non-negative square root of x, i.e

sqrt(x)=\sqrt x

For complex numbers x, sqrt returns the complex root of x, using the following formula

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

where \mathrm{arg}(x) is the argument of x.

Example 1

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

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

Ouput:

The square root of 1234321.00 is 1111.00

SPECIAL VALUES

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

STANDARDS

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

COMPATIBILITY

DOSUNIXWindowsANSI CC++ only
sqrtl••
Real sqrt••••
Complex sqrt•••
Complex csqrt••