In C#, Math.Pow(x, y) returns xraised to the powery. In school maths that is xy. Math.Pow(2, 10) means 2 multiplied by itself 10 times, which is 1024.
Think of a number that keeps doubling: 2, 4, 8, 16, and so on. The power says how many times it doubled. The exponent can be negative (Pow(2, -1) is a half) or a fraction (Pow(9, 0.5) is 3, the same as Sqrt()). Both arguments and the result are double, so cast to int when you need a whole number.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Pow(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Pow is in every .NET version. It takes and returns double.
Syntax
Pass the base first and the exponent second. Both are double values, and so is the answer.
C#
double a = Math.Pow(2, 10); // 1024
double b = Math.Pow(5, 2); // 25
double c = Math.Pow(9, 0.5); // 3 (square root)
int d = (int)Math.Pow(3, 3); // 27 as an int
Call
What it does
Math.Pow(2, 10)
2 to the power 10: 1024.
Math.Pow(2, -1)
A negative exponent means one divided by the power: 0.5.
Math.Pow(16, 0.5)
An exponent of 0.5 is the square root: 4.
Math.Pow(-8, 0.5)
A negative base with a fractional exponent has no real answer: NaN.
Any number to the power 0 is 1, and any number to the power 1 is itself. Because the result is a double, large answers lose exactness above 253, so avoid Pow for exact big integer work. For integers use BigInteger.Pow or a loop. An overflow does not throw. It gives Infinity.
Library declarations
C#
public static double Pow(double x, double y);
Parameters
Parameter
Description
x
Required. The base: the number to raise.
y
Required. The exponent: the power to raise x to. It can be negative or fractional.
Return Value
Return Value
Description
double
x raised to the power y. The result is NaN for a negative x with a non-whole y, Infinity when the answer is too large (or for Pow(0, -1)).
C# Examples
Example 1 — Squares, Cubes, and Powers of Two
Raise a few numbers to whole-number powers.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Pow(2, 10));
Console.WriteLine(Math.Pow(5, 2));
Console.WriteLine(Math.Pow(3, 3));
Console.WriteLine(Math.Pow(10, 3));
Console.WriteLine(Math.Pow(7, 0));
}
}
Output
1024
25
27
1000
1
How It Works
Powers of two.Math.Pow(2, 10) is 2 multiplied by itself ten times, 1024. This is how bytes and memory sizes grow.
Square and cube.Pow(5, 2) is 5 x 5 = 25, and Pow(3, 3) is 3 x 3 x 3 = 27.
Powers of ten.Pow(10, 3) is 1000. These whole-number answers print without a decimal point.
Power of zero. Any number to the power 0 is 1, so Pow(7, 0) prints 1.
Example 2 — Negative and Fractional Exponents
See what a negative exponent, a fractional exponent, and a negative base do.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
Console.WriteLine(Math.Pow(2, -1).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Pow(4, -1).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Pow(16, 0.5));
Console.WriteLine(Math.Pow(-2, 3));
Console.WriteLine(Math.Pow(-8, 0.5));
}
}
Output
0.5
0.25
4
-8
NaN
How It Works
Negative exponent.Pow(2, -1) is 1 divided by 2, which is 0.5. Pow(4, -1) is 0.25. The culture setting makes sure the decimal mark is a dot.
Fractional exponent. An exponent of 0.5 means "square root", so Pow(16, 0.5) is 4.
Negative base, whole exponent.Pow(-2, 3) is (-2) x (-2) x (-2) = -8. This is fine.
Negative base, fractional exponent. The square root of -8 is not a real number, so you get NaN. No exception is thrown.
Example 3 — Compound Interest and Big Results
Grow 1000 at 10 percent a year, then see a result that is too big for an int and one that is infinite.
The formula. Money grows by 10 percent each year, so after n years it is start * 1.10 to the power n. After 2 years that is 1210.0000000000002, so we round to 2 decimals and get 1210.
Three years. The same formula with the power 3 gives about 1331. Rounding hides the tiny double error.
Bigger than int. 2 to the power 31 is 2147483648, one more than int.MaxValue. A double holds it exactly, but an (int) cast would not.
Cast when it fits.(int)Math.Pow(2, 10) gives the int 1024. And Pow(0, -1) means 1 divided by 0, so the answer is Infinity, not an exception.
Frequently Asked Questions
It raises a number to a power. Math.Pow(2, 10) is 1024. Both arguments and the result are double values.
Cast the result, as in (int)Math.Pow(2, 10). The cast cuts off any decimals, so for exact results that are not whole numbers, round first with Math.Round. For large exact integers use BigInteger.Pow.
No. C# has no exponent operator. Use Math.Pow(x, y) for doubles, or multiply in a loop for small integer powers.
A negative base with a fractional exponent has no real answer for Pow, so it returns NaN. For cube roots of negative numbers use Math.Cbrt(-8), which gives -2.
No. Math is a static class, so you call Math.Pow(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.