# C programming sqrt , sqrtf and sqrtl math.h

In C programming the <math.h> sqrt,sqrtf and sqrtl function compute the square root of the value.The declaration of the functions are given below.

1 float sqrt(float x);
2 double sqrt(double x);
3 long double sqrt(long double x);
4 float sqrtf(float x);
5 long double sqrtl(long double x);

Parameters:
x -The value whose square root is to be calculated.

Return type
floating point -The square root of x.

All the functions are the same,they compute the square root of the argument.The 1st,2nd and the 3rd function have the same name i.e.’sqrt’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

i) sqrt(x) is same as √x.

ii) A domain error occur if x<0.

iii) If NAN is passed ‘nan’ is returned.

iv) If +∞ is passed +∞ is retunred,if -∞ is passed ‘nan’ is returned.

Code example

double d=12 , x1=2e200 ;

float f=12 ;

long double ld=12 ;

printf(“%lf”, sqrt( d ) ) ; //calls the 2nd version
printf(“\n%f”, sqrt( f ) ) ; //calls the 1st version
printf(“\n%Lf”, sqrt( ld ) ) ;//calls the 3rd version

printf(“\n%lf”, sqrt( x1 ) ) ;
printf(“\n%lf”, sqrt( INFINITY ) ) ;

Output in Visual Studio ,

3.464102
3.464102
3.464102
-nan(ind)
inf

Since x1 is very large the 4th output is -nan(ind)(NaN) and when infinity is passed ‘inf'(infinity) is returned.

#### sqrtf

The ‘sqrtf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘sqrtf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=81.81 ;

printf(“%f”, sqrt( x ) ); //Calls the 1st version
printf(“\n%f”, powf( 81.81) ); Calls the 4th version

Output in Visual Studio,

9.044888
9.044888

#### sqrtl

The ‘sqrtl‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘sqrtl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=81.81;

printf(“%Lf”, sqrt( x ) ); //Calls the 3rd version
printf(“\n%Lf”, sqrtl( 81.81 ) ); Calls the 5th version

Output,

9.044888
9.044888

*Side Note

sqrt(x) is much faster than the equivalent function pow(x , 0.5).