Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

Math.Pow() and Math.Sqrt() methods

Reference for Math.Pow, Math.Sqrt and Math.Cbrt in C#: powers, roots, special values, precision limits for integers, and BigInteger.Pow for exact results.

Math.Pow(x, y) raises x to the power y. Math.Sqrt(x) returns the square root, and Math.Cbrt(x) the cube root. They work on double, so the results are floating-point approximations: Math.Pow(2, 10) is exactly 1024, but values past 2^53 cannot all be represented.

Roots are powers with a fractional exponent (Math.Pow(x, 0.5) is the square root), but Sqrt is faster and more accurate than Pow, so prefer the dedicated method. For exact integer results, multiply in a loop or use BigInteger.Pow.

C#
double area = Math.Pow(5, 2);      // 25
double side = Math.Sqrt(area);     // 5

Try it

C# Powers and roots

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 Console.ReadLine()), it gets the next line from this box.

C# Special values and exact integer powers

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 Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static double Pow(double x, double y);
public static double Sqrt(double d);
public static double Cbrt(double d);

// Generic math versions on the types
double.Pow(double x, double y);
double.Sqrt(double x);
double.RootN(double x, int n);
double.Hypot(double x, double y);
Name Description
x The base.
y The exponent (any double, positive, negative or fractional).
d The number whose root is wanted.
n (RootN) The degree of the root, as an int.

Returns: x to the power y, or the square or cube root, as a double.

Errors / undefined behavior: nothing is thrown. A negative base with a non-integer exponent gives NaN; so does Sqrt of a negative number. Pow(0, negative) is Infinity, and results above double.MaxValue are Infinity. Pow(x, 0) is 1 for every x, including NaN.

Usage notes

  • Everything is double. Math.Pow(2, 3) returns 8.0. Cast the result ((int)Math.Pow(2, 3)) only when you know it is exact; rounding first with Math.Round is safer.
  • Do not use Pow for exact big integers. Beyond 2^53 (about 9 * 10^15) the result is rounded, as the second example shows: the loop prints the true value of 3^39 while (long)Math.Pow(3, 39) ends in different digits. Use BigInteger.Pow, or long multiplication with checked.
  • Squares are cheaper as multiplication. x * x is faster and exact for small integers; reserve Pow for variable or fractional exponents.
  • Roots of negatives. Math.Sqrt(-4) is NaN. For cube roots of negative numbers use Math.Cbrt, since Math.Pow(-8, 1.0 / 3) is NaN (the exponent is not exactly one third).
  • Hypot. double.Hypot(dx, dy) computes sqrt(dx*dx + dy*dy) without intermediate overflow; Math.Sqrt(dx * dx + dy * dy) is fine for ordinary ranges.
  • Integer square root. (int)Math.Sqrt(n) is correct for all int values, but for long and larger values the double conversion can be off by one near perfect squares. Fix with a check (while (r * r > n) r--;) or use BigInteger methods.
  • Powers of two. For integer powers of 2, shifts are exact and fastest: 1 << 10 is 1024.
  • Platform differences. Pow is implemented by the platform's math library. Results for ordinary inputs agree to many digits, but the last bit can differ between machines, so round before comparing.
  • decimal has no Pow. Use a loop for integer exponents, or convert to double and accept the precision loss.