Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <math.h>

double sqrt(double x);
double pow(double x, double y);

Try it

C Roots, powers and domain errors

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.

C Distance, integer square roots and compound growth

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

C
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) with x < 0 returns a NaN and signals a domain error.
  • pow(x, y) with x < 0 and a non-integer y returns a NaN. A result too large returns HUGE_VAL (or infinity) and signals a range error; one too small returns 0 (or a denormal).
  • pow(0, y) with y < 0 is a pole error and returns an infinity.
  • pow(x, 0) is 1 for every x, even NaN, and pow(1, y) is 1 for every y.

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 -lm to link math functions. The course runner links them without extra flags; on your own computer, add -lm if you get "undefined reference to sqrt". 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. sqrt is correctly rounded on IEEE 754 systems, so perfect squares up to 2^53 give exact results. pow is not always exact: for integer exponents, repeated multiplication is exact for small values, while pow may be off by an ulp on some libraries.
  • Do not use pow for 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 with round / lround.
  • Squares. x * x is faster, exact and clearer than pow(x, 2).
  • Integer square roots. Converting a large long long to double loses 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. Use cbrt(-8) or take the sign out first.
  • Overflow and underflow: pow(10, 400) overflows a double; pow(10, -400) underflows to 0.
  • Complex roots. For roots of negative numbers use csqrt from complex.h.
  • Standard version. Both in C89. The f and l variants arrived in C99.