C Reference ยท ctype.h, math.h and others
C sqrt() and pow() functions
Reference for C sqrt() and pow() from math.h: compute roots and powers of doubles, domain errors, float and long double variants and integer pitfalls.
sqrt() returns the square root of a double. pow() raises a base to an exponent: pow(x, y) is x to the power y. Both live in <math.h>, work on floating-point numbers and return double.
Each has variants for other types: sqrtf and powf for float, sqrtl and powl for long double. If you include <tgmath.h>, plain sqrt and pow pick the right variant from the argument type. Roots of other degrees are done with pow (for example pow(x, 1.0 / 3.0)) or with cbrt.
#include <math.h> double sqrt(double x); double pow(double x, double y);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
double sqrt(double x); float sqrtf(float x); long double sqrtl(long double x); double pow(double x, double y); float powf(float x, float y); long double powl(long double x, long double y);
| Name | Description |
|---|---|
x |
For sqrt, the number whose root is taken. For pow, the base. |
y |
The exponent (pow only). |
Returns: sqrt returns the non-negative square root. pow returns x raised to y.
Errors / undefined behavior: these functions do not have undefined behavior, but they report domain and range errors:
sqrt(x)withx < 0returns a NaN and signals a domain error.pow(x, y)withx < 0and a non-integeryreturns a NaN. A result too large returnsHUGE_VAL(or infinity) and signals a range error; one too small returns 0 (or a denormal).pow(0, y)withy < 0is a pole error and returns an infinity.pow(x, 0)is 1 for everyx, even NaN, andpow(1, y)is 1 for everyy.
Errors are reported through errno (EDOM, ERANGE) and/or the floating-point exception flags, depending on math_errhandling. The easiest portable check is isnan() on the result.
Usage notes
- Linking. With glibc, some compilers need
-lmto link math functions. The course runner links them without extra flags; on your own computer, add-lmif you get "undefined reference tosqrt". Calls with constant arguments are often folded by the compiler and need no library at all, which can hide the problem until the argument becomes a variable. - Exactness.
sqrtis correctly rounded on IEEE 754 systems, so perfect squares up to 2^53 give exact results.powis not always exact: for integer exponents, repeated multiplication is exact for small values, whilepowmay be off by an ulp on some libraries. - Do not use
powfor integer powers in integer code.(int)pow(10, 2)may be 99 on a library that returns 99.999..., and the conversion truncates. Write a small loop, or round the result withround/lround. - Squares.
x * xis faster, exact and clearer thanpow(x, 2). - Integer square roots. Converting a large
long longtodoubleloses precision above 2^53, so correct the result as in the example. - Negative bases.
pow(-8, 1.0 / 3.0)is NaN because 1/3 is not exactly representable as a whole-number fraction. Usecbrt(-8)or take the sign out first. - Overflow and underflow:
pow(10, 400)overflows adouble;pow(10, -400)underflows to 0. - Complex roots. For roots of negative numbers use
csqrtfromcomplex.h. - Standard version. Both in C89. The
fandlvariants arrived in C99.