Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] A Guide to Python’s pow() Function

#1
A Guide to Python’s pow() Function

<div><p><em>Exponents are superscript numbers that describe how many times you want to multiply a number by itself. Calculating a value raised to the power of another value is a fundamental operation in applied mathematics such as finance, machine learning, statistics, and data science. This tutorial shows you how to do it in Python!</em></p>
<h2>Definition</h2>
<p>For <code>pow(x, y)</code>, the <code>pow()</code> function returns the value of <code>x</code> raised to the power <code>y</code>. It performs the same function as the power operator <code><a href="https://blog.finxter.com/python-double-asterisk/" target="_blank" rel="noreferrer noopener" title="Python Double Asterisk (**)">**</a></code> , i.e. <code>x**y</code>, but differs in that it comes with an optional argument called mod.</p>
<h2>Examples without mod</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(5, 2)
25
>>> pow(-3, 3)
-27
>>> pow(2, -2)
0.25</pre>
<h2>Parameters and Syntax</h2>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong><code>pow(base, exp, mod=None)</code></strong></td>
</tr>
</tbody>
</table>
</figure>
<p>The <code>pow()</code> function includes two compulsory arguments, <code>base</code> and <code>exp</code>, and one optional argument, <code>mod</code>, whose default value is <code>None</code>. All arguments must be of numeric data type.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Parameter</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td><em>exp</em></td>
<td>A number that represents the base of the function, whose power is to be calculated.</td>
</tr>
<tr>
<td><em>base</em></td>
<td>A number that represents the exponent of the function, to which the base will be raised.</td>
</tr>
<tr>
<td><em>mod</em></td>
<td>A number with which the <a href="https://blog.finxter.com/python-modulo/" target="_blank" rel="noreferrer noopener" title="Python Modulo">modulo </a>will be computed.</td>
</tr>
</tbody>
</table>
</figure>
<p><strong>Return value: </strong>The output of <code>base</code> raised to the power <code>exp</code> and will be a numeric data type, int, float or <a href="https://blog.finxter.com/python-complex/" target="_blank" rel="noreferrer noopener" title="Python complex() — A Useless Python Feature?">complex</a>, depending on what you input.</p>
<h2>Using the pow() function without the mod argument</h2>
<p>When using the <code>pow(x, y)</code> function without the optional mod argument, it will perform the same operation as the power operator <code>x**y</code>, raising <code>x</code> to the power <code>y</code>.</p>
<h3>Comparison of the two methods</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(6, 4)
1296
>>> 6 ** 4
1296</pre>
<p>The <code>pow()</code> function accepts all numeric data types, i.e. int, float and even complex numbers. In general the return value will depend on what data types you input. The example above shows that both arguments are type int, therefore, an int type is returned. However, if you were to instead use a <a href="https://blog.finxter.com/decimal-pythons-float-trap-and-how-to-solve-it/" target="_blank" rel="noreferrer noopener" title="Decimal: Python’s Float Trap and How to Solve it">float </a>number as one or both of the arguments, the function will automatically return a float type.</p>
<h3>Examples using float types</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(3.0, 4)
81.0
>>> pow(4.5, 2.3)
31.7971929089206</pre>
<p>As with float type inputs leading to float outputs, the same reasoning applies to complex numbers. If you enter a complex number as one or both of the arguments, a complex number will be returned.</p>
<h3>Example using complex numbers</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(4+2j, 3)
(16+88j)</pre>
<p>The return type will also depend on whether your arguments are non-negative or negative, as is shown in the below table.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>base</td>
<td>exp</td>
<td><strong>Return type</strong></td>
</tr>
<tr>
<td>Non-negative</td>
<td>Non-negative</td>
<td>int</td>
</tr>
<tr>
<td>Non-negative</td>
<td>Negative</td>
<td>foat</td>
</tr>
<tr>
<td>Negative</td>
<td>Non-negative</td>
<td>int</td>
</tr>
<tr>
<td>Negative</td>
<td>Negative</td>
<td>float</td>
</tr>
</tbody>
</table>
</figure>
<h3>Examples of return values with different input types</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(7, 2)
49
>>> pow(4, -5)
0.0009765625
>>> pow(-6, 3)
-216
>>> pow(-9, -4)
0.00015241579027587258</pre>
<h2>Using the pow() function with a mod argument</h2>
<p>What sets the <code>pow()</code> function apart from the <code>**</code> operator is its third optional argument, <code>mod</code>, which gives you the ability to do a modulo operation within the function. </p>
<p>The process of operations when using the mod argument is as follows:</p>
<p>If we have <code>pow(x, y, z)</code>, the function first performs the task of raising <code>x</code> to the power <code>y</code> and then that result is used to perform the modulo task with respect to <code>z</code>. It would be the equivalent of <code>(x**y) % z</code> .</p>
<h3>Examples using mod</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(14, 7, 5)
4
>>> pow(-8, 3, 5)
3
>>> pow(2, 4, -3)
-2</pre>
<p>The general rule for using the mod argument is that all values must be of integer type, the <code>exp</code> argument must be non-negative and the mod argument must be non-zero. However, <a href="https://blog.finxter.com/check-python-version-in-your-script-a-helpful-illustrated-guide/" target="_blank" rel="noreferrer noopener" title="Check Python Version in Your Script — A Helpful Illustrated Guide">Python 3.8</a> now comes with the functionality of computing modular inverses. In this case, the <code>exp</code> argument may be negative, on the condition that base is <em>relatively prime</em> to mod, i.e, the only common integer divisor of base and mod is 1.</p>
<p>So, when using the <code>pow()</code> function with negative <code>exp</code>, the function will perform as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pow(inv_base, -exp, mod)</pre>
<p>In other words, the function will compute the modular inverse of base and mod first and then that result will be used in the <code>pow()</code> function as base to be computed as normal with the  exp argument being converted to its non-negative counterpart.</p>
<h3>Example of modular inverse</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(87, -1, 25)
23</pre>
<p>In this example, the straight modular inverse is calculated because <code>inv_base</code> will be raised to the power 1.</p>
<h3>Example of modular inverse when exp is not -1</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(34, -5, 19)
10
# The modular inverse of 34 mod 19 is 14, therefore, we end up with the function pow(14, 5, 19)
>>> pow(14, 5, 19)
10</pre>
<h2>Calculating the <em>nth</em> root of a number using pow()</h2>
<p>Unfortunately, Python does not have a built-in function to calculate the <em>nth</em> root of a number. The math module only has a function to calculate square roots, <code><a href="https://blog.finxter.com/python-math-module/" title="Python Math Module [Ultimate Guide]">math.sqrt()</a></code>, therefore, we have to get creative in order to calculate <em>nth</em> roots.</p>
<p>We know that <code>nx</code> is equivalent to <code>x1n</code>. Thus, using this knowledge we can calculate the nth root in Python by using either <code>pow(x, (1/n))</code> or <code>x**(1/n)</code>. </p>
<h3>Examples of calculating <em>nth</em> roots</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(625, (1/4))
4.0
>>> 729**(1/3)
8.999999999999998</pre>
<p>Note that performing an <em>nth</em> root calculation will always return a float when not using complex numbers. Since Python’s <a href="https://blog.finxter.com/decimal-pythons-float-trap-and-how-to-solve-it/" target="_blank" rel="noreferrer noopener" title="Decimal: Python’s Float Trap and How to Solve it">float type works on approximations</a>, it will often return the approximation rather than the exact number, even when an exact answer is possible. This is demonstrated in the second example above.</p>
<p>When calculating the nth root of a negative number, the return value will be a complex number whether an integer number is possible or not.&nbsp;</p>
<h3>Examples of calculating <em>nth</em> roots of negative bases</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> pow(-16, (1/2))
(2.4492935982947064e-16+4j)
>>> pow(-27, (1/3))
(1.5000000000000004+2.598076211353316j)</pre>
<p>We would expect the second example above, the cubed root of -27, to result in -3, but instead we get a complex number. This is because Python returns the principal root rather than the real root. For an explanation of these different types of roots, you can look up the <a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_algebra">Fundamental theorem of algebra</a>.</p>
<h2>math.pow() Function</h2>
<p>In the <a href="https://blog.finxter.com/python-math-module/" target="_blank" rel="noreferrer noopener" title="Python Math Module [Ultimate Guide]">math module </a>of Python, there is a similar function called <code>math.pow()</code>. To use this we first need to import the math function, thus, the <a href="https://blog.finxter.com/python-built-in-functions/" target="_blank" rel="noreferrer noopener" title="Python Built-In Functions">built-in</a> <code>pow()</code> function will be very slightly faster. The main differences between the two functions is that <code>math.pow()</code> does not allow for the optional mod argument and it will always return a float. So if you want to ensure that you get a float result, <code>math.pow()</code> is a better option.</p>
<h3>Example of using math.pow()</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import math
>>> math.pow(9, 5)
59049.0</pre>
<h2>When to use the pow() function vs when to use the ** operator</h2>
<p>When deciding between using the <code>pow()</code> function or the <code>**</code> operator, the most important factor to consider would be the efficiency of your code. We can use the <code>timeit.timeit()</code> function from the <code>timeit</code> module to find out how fast Python executes our code.</p>
<h3>Examples of using timeit with simple numbers</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> import timeit
>>> timeit.timeit('pow(5, 2)')
0.25059129999863217
>>> timeit.timeit('5**2')
0.008814800001346157</pre>
<p>When performing a simple power computation, the <code>**</code> operator appears to be much faster.</p>
<h3>Examples using modulo</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> timeit.timeit('pow(52, 2, 4)')
0.7482693000001746
>>> timeit.timeit('52**2 % 4')
0.012026999998852261</pre>
<p>The same is true even when we include a modulo operation.&nbsp;</p>
<p>However, when we want to perform power operations with very large numbers, the <code>pow()</code> function is much quicker, showing that the power of the <code>pow()</code> function lies in executing longer computations.</p>
<h3>Examples using large numbers</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> timeit.timeit('pow(5234, 2341, 124)')
0.9020593000004737
>>> timeit.timeit('5234**2341 % 124')
152.56075580000106</pre>
<p>Here the <code>pow()</code> function is extremely fast compared to the <code>**</code> operator. Therefore, we can generalize these findings by saying that when you want to perform short, simple calculations, the ** operator is the better option, however, if your operations involve very large numbers, the <code>pow()</code> function is much more efficient.</p>
<p>The post <a href="https://blog.finxter.com/a-guide-to-pythons-pow-function/" target="_blank" rel="noopener noreferrer">A Guide to Python’s pow() Function</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/12/...-function/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016