Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#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);
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.
⁄* 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
sqrt( 45.000000 ) = 6.708204