Name
sqrt
Synopsis
Calculates the square root of a floating-point number
#include <math.h> doublesqrt
( doublex
); floatsqrtf
( floatx
); (C99) long doublesqrtl
( long doublex
); (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.
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.