sqrt(), sqrtf(), sqrtl() — Calculate square root

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99

both  

Format

#include <math.h>

double sqrt(double x);
float sqrt(float x);                  /* C++ only */
long double sqrt(long double x);      /* C++ only */
float sqrtf(float x);
long double sqrtl(long double x);

General description

Calculates the square root of x.
Note: These functions work in both IEEE Binary Floating-Point and hexadecimal floating-point formats. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.

Returned value

If successful, returns the square root of x.

If x is negative, the function sets errno to EDOM, and returns 0. If the correct value would cause underflow, zero is returned and the value ERANGE is stored in errno.

Special behavior for IEEE

If x< -0, the function returns NaNQ and sets errno to EDOM.

If x is a NaN, a NaN will be returned.

If x is ±0 or +INF, x will be returned.

If x is -INF, a EDOM will be set, and NaNQ will be returned.

Example

CELEBS30
⁄* CELEBS30                                      

   This example computes the square root of the quantity passed                 
   as the first argument to main.                                               
   It prints an error message if you pass a negative value.                     
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <math.h>                                                               
                                                                                
int main(int argc, char ** argv)                                                
{                                                                               
  char * rest;                                                                  
  double value;                                                                 
                                                                                
  if ( argc != 2 )                                                              
    printf( "Usage: %s value\n", argv[0] );                                     
  else                                                                          
  {                                                                             
    value = strtod( argv[1], &rest );                                           
    if ( value < 0.0 )                                                          
       printf( "sqrt of a negative number\n" );                                 
    else                                                                        
       printf("sqrt( %f ) = %f\n", value, sqrt( value ));                       
  }                                                                             
}                                                                               

Output

If the input is 45, then the output should be:
sqrt( 45.000000 ) = 6.708204

Related information