Home
Search
C# Math.Pow Method, ExponentsUse the Math.Pow method to compute exponents with 2 arguments.
Math.Pow. This C# method computes exponential values. It takes the powers of numbers such as by squaring values. It receives a double argument.
Method details. Pow() 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.
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.
Using System
Finally The program also cubes those small integers. The Math.Pow method returns a double type number.
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); } }
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
A discussion. The Math.Pow method is not extremely slow. But if you can somehow cache the power number in a local variable, the execution time of your method will likely be reduced.
Info You can use arrays as lookup tables to cache the results of common numeric computations (such as exponents).
A summary. We saw the Math.Pow method, which provides a clear way to compute exponential values. The variable returned is a double type.
Home
© 2007-2022 sam allen.
see site info on the changelog.