|
|
|
Defined in header <math.h> | ||
float sqrtf(float arg); | (1) | (since C99) |
double sqrt(double arg); | (2) | |
longdouble sqrtl(longdouble arg); | (3) | (since C99) |
Defined in header <tgmath.h> | ||
#define sqrt( arg ) | (4) | (since C99) |
sqrtl
is called. Otherwise, ifarg has integer type or the typedouble,sqrt
is called. Otherwise,sqrtf
is called. Ifarg is complex or imaginary, then the macro invokes the corresponding complex function (csqrtf,csqrt,csqrtl).Contents |
arg | - | floating-point value |
If no errors occur, square root ofarg (\({\small \sqrt{arg} }\)√arg), is returned.
If a domain error occurs, an implementation-defined value is returned (NaN where supported).
If a range error occurs due to underflow, the correct result (after rounding) is returned.
Errors are reported as specified inmath_errhandling
.
Domain error occurs ifarg is less than zero.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
sqrt
is required by the IEEE standard to be correctly rounded from the infinitely precise result. In particular, the exact result is produced if it can be represented in the floating-point type. The only other operations which require this are thearithmetic operators and the functionfma. Other functions, includingpow, are not so constrained.
#include <errno.h>#include <fenv.h>#include <math.h>#include <stdio.h>// #pragma STDC FENV_ACCESS ON int main(void){// normal useprintf("sqrt(100) = %f\n", sqrt(100));printf("sqrt(2) = %f\n", sqrt(2));printf("golden ratio = %f\n",(1+ sqrt(5))/2); // special valuesprintf("sqrt(-0) = %f\n", sqrt(-0.0)); // error handlingerrno=0;feclearexcept(FE_ALL_EXCEPT);printf("sqrt(-1.0) = %f\n", sqrt(-1));if(errno==EDOM)perror(" errno == EDOM");if(fetestexcept(FE_INVALID))puts(" FE_INVALID was raised");}
Possible output:
sqrt(100) = 10.000000sqrt(2) = 1.414214golden ratio = 1.618034sqrt(-0) = -0.000000sqrt(-1.0) = -nan errno = EDOM: Numerical argument out of domain FE_INVALID was raised
(C99)(C99) | computes a number raised to the given power (\(\small{x^y}\)xy) (function)[edit] |
(C99)(C99)(C99) | computes cube root (\(\small{\sqrt[3]{x} }\)3√x) (function)[edit] |
(C99)(C99)(C99) | computes square root of the sum of the squares of two given numbers (\(\scriptsize{\sqrt{x^2+y^2} }\)√x2 +y2 ) (function)[edit] |
(C99)(C99)(C99) | computes the complex square root (function)[edit] |
C++ documentation forsqrt |