Export (0) Print
Expand All
and
div
eof
not
or
xor
Expand Minimize
This topic has not yet been rated - Rate this topic

sqrt, sqrtf, sqrtl

Calculates the square root.

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 
);
x

Non-negative floating-point value

Because C++ allows overloading, you can call overloads of sqrt that take float or long double types. In a C program, sqrt always takes and returns double.

The sqrt functions return the square-root of x. By default, if x is negative, sqrt returns an indefinite NaN.

Input

SEH Exception

_matherr Exception

± QNAN,IND

none

_DOMAIN

- ∞

none

_DOMAIN

x<0

none

_DOMAIN

Function

C header

C++ header

sqrt, sqrtf, sqrtl

<math.h>

<cmath>

For compatibility information, see Compatibility.

// crt_sqrt.c
// This program calculates a square root.

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

int main( void )
{
   double question = 45.35, answer;
   answer = sqrt( question );
   if( question < 0 )
      printf( "Error: sqrt returns %f\n", answer );
   else
      printf( "The square root of %.2f is %.2f\n", question, answer );
}
The square root of 45.35 is 6.73
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.