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.
double area = Math.Pow(5, 2); // 25 double side = Math.Sqrt(area); // 5
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 Console.ReadLine()), 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 Console.ReadLine()), it gets the next line from this box.
Syntax
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)returns8.0. Cast the result ((int)Math.Pow(2, 3)) only when you know it is exact; rounding first withMath.Roundis safer. - Do not use
Powfor 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. UseBigInteger.Pow, orlongmultiplication withchecked. - Squares are cheaper as multiplication.
x * xis faster and exact for small integers; reservePowfor variable or fractional exponents. - Roots of negatives.
Math.Sqrt(-4)isNaN. For cube roots of negative numbers useMath.Cbrt, sinceMath.Pow(-8, 1.0 / 3)isNaN(the exponent is not exactly one third). Hypot.double.Hypot(dx, dy)computessqrt(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 allintvalues, but forlongand larger values thedoubleconversion can be off by one near perfect squares. Fix with a check (while (r * r > n) r--;) or useBigIntegermethods. - Powers of two. For integer powers of 2, shifts are exact and fastest:
1 << 10is1024. - Platform differences.
Powis 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. decimalhas noPow. Use a loop for integer exponents, or convert todoubleand accept the precision loss.