C# Tutorial C# Advanced C# References

C# Math - Math.Pow() Method



The C# Math.Pow() method returns the base raise to the Power of exponent. In special cases it returns the following:

  • If any of the arguments is NaN, the method returns NaN.
  • If the base is -1 and exponent is either positive infinity or negative infinity, the method returns NaN.
  • If the base is less than zero but not negative infinity and exponent is not Integer, positive infinity or negative infinity, the method returns NaN.
  • If the base is negative infinity and exponent is positive but not odd integer, the method returns positive infinity.
  • If the absolute value of base is less than 1 and exponent is negative infinity, the method returns positive infinity.
  • If the absolute value of base is greater than 1 and exponent is positive infinity, the method returns positive infinity.

Syntax

public static double Pow (double base, double exponent);

Parameters

base Specify the base.
exponent Specify the exponent.

Return Value

Returns the base raise to the Power of exponent.

Example:

In the example below, Math.Pow() method is used to calculate the base raised to the Power of exponent.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.Pow(10, 2) = " 
                      + Math.Pow(10, 2));  
    Console.WriteLine("Math.Pow(5.2, 3) = " 
                      + Math.Pow(5.2, 3));
    Console.WriteLine("Math.Pow(5.2, -3) = " 
                      + Math.Pow(5.2, -3));
    Console.WriteLine("Math.Pow(5, Double.NaN) = " 
                      + Math.Pow(5, Double.NaN)); 
    Console.WriteLine("Math.Pow(Double.NaN, 2) = " 
                      + Math.Pow(Double.NaN, 2));
    Console.WriteLine("Math.Pow(2, Double.PositiveInfinity) = "
                      + Math.Pow(2, Double.PositiveInfinity));
    Console.WriteLine("Math.Pow(2, Double.NegativeInfinity) = "
                      + Math.Pow(2, Double.NegativeInfinity)); 
  }
}

The output of the above code will be:

Math.Pow(10, 2) = 100
Math.Pow(5.2, 3) = 140.608
Math.Pow(5.2, -3) = 0.00711197086936732
Math.Pow(5, Double.NaN) = NaN
Math.Pow(Double.NaN, 2) = NaN
Math.Pow(2, Double.PositiveInfinity) = Infinity
Math.Pow(2, Double.NegativeInfinity) = 0

❮ C# Math Methods

5