# C# Math.Pow Method, Exponents

Use the Math.Pow method to compute exponents with 2 arguments. Math.Pow computes exponential values. It takes the powers of numbers such as by squaring values. It is a simple and convenient way to compute exponents in the C# language. There are issues related to its usage. Example. This program computes the powers of various numbers using the Math.Pow method. The parameters must be doubles or types that are convertible to double types. The compiler automatically converts the parameters for you if it can.Double

Parameters: The first parameter indicates the number you are trying to base the result on. The second parameter is the exponent itself.

Tip: If you are trying to square two, you will use 2, 2 as the parameters. Visual Studio provides hints on the use of parameters.

Here: The program first uses the Math.Pow method in the System namespace by calling it with common small integer parameters.

Finally: The program also cubes those small integers. The Math.Pow method returns a double type number.

C# program that uses Math.Pow method using System; class Program { static void Main() { // // Compute the squares of the first parameters to Math.Pow. // double value1 = Math.Pow(2, 2); double value2 = Math.Pow(3, 2); double value3 = Math.Pow(4, 2); double value4 = Math.Pow(5, 2); // // Compute the cubes of these values. // double value5 = Math.Pow(2, 3); double value6 = Math.Pow(3, 3); double value7 = Math.Pow(4, 3); double value8 = Math.Pow(5, 3); // // Check some edge cases with the smallest, biggest, // ... and special values. // double value9 = Math.Pow(double.MinValue, double.MaxValue); double value10 = Math.Pow(double.MinValue, 0); double value11 = Math.Pow(double.NaN, 2); double value12 = Math.Pow(double.PositiveInfinity, 2); double value13 = Math.Pow(double.NegativeInfinity, 2); // // Compute fractional powers. // double value14 = Math.Pow(2, 2.1); double value15 = Math.Pow(Math.E, 2); double value16 = Math.Pow(Math.PI, 1); // // Write results to the screen. // Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.WriteLine(value4); Console.WriteLine(value5); Console.WriteLine(value6); Console.WriteLine(value7); Console.WriteLine(value8); Console.WriteLine(value9); Console.WriteLine(value10); Console.WriteLine(value11); Console.WriteLine(value12); Console.WriteLine(value13); Console.WriteLine(value14); Console.WriteLine(value15); Console.WriteLine(value16); } } Output 4 Two squared. 9 Three squared. 16 Four squared. 25 Five squared. 8 Two cubed (2 * 2 * 2). 27 Three cubed (3 * 3 * 3). 64 Four cubed (4 * 4 * 4). 125 Five cubed (5 * 5 * 5). Infinity 1 NaN Infinity Infinity 4.28709385014517 7.38905609893065 3.14159265358979 Discussion. The Math.Pow method is not an extremely inefficient method. But if you can somehow cache the power number in a local variable, the execution time of your method will likely be reduced.

Note: The Math.Pow method is implemented in code external to the managed code shipped as part of the .NET Framework. The book Code Complete by Steve McConnell shows specific optimization techniques. You can use arrays as lookup tables to cache the results of common numeric computations such as logarithms (or exponents) in your programs. Summary. We saw the Math.Pow method, which provides a clear way to compute exponential values. The variable returned is a double type and this must be cast down to an integer if you need an integer.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.