Name

sqrt

Synopsis

Calculates the square root of a floating-point number

#include <math.h>
doublesqrt( double x );
float sqrtf( float x );        (C99)
long double sqrtl( long double x );        (C99)

The sqrt() functions return the square root of the argument x. If the argument is less than zero, a domain error occurs.

Example

double x[ ] = { 0.5, 0.0, -0.0, -0.5 };

for ( int i = 0; i < ( sizeof(x) / sizeof(double) ); i++)
{
  printf("The square root of %.2F equals %.4F\n", x[i], sqrt( x[i] ) );
  if ( errno )
    perror( _  _FILE_  _ );
}

This code produces the following output:

The square root of 0.50 equals 0.7071
The square root of 0.00 equals 0.0000
The square root of -0.00 equals -0.0000
The square root of -0.50 equals NAN
sqrt.c: Numerical argument out of domain

sqrt() is also used in the examples shown at erf(), feholdexcept(), frexp() and signbit() in this chapter.

See Also

The complex arithmetic function csqrt(); the cube root function cbrt() and the hypotenuse function, hypot()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.