![]() |
|
[Tut] A Guide to Python’s pow() Function - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] A Guide to Python’s pow() Function (/thread-98872.html) |
[Tut] A Guide to Python’s pow() Function - xSicKxBot - 12-22-2020 A Guide to Python’s pow() Function 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! Definition For Code: pow(x, y)Code: pow()Code: xCode: yCode: **Code: x**yExamples without mod Code: >>> pow(5, 2)
25
>>> pow(-3, 3)
-27
>>> pow(2, -2)
0.25Code: pow(base, exp, mod=None)The Code: pow()Code: baseCode: expCode: modCode: NoneParameter | Description exp | A number that represents the base of the function, whose power is to be calculated. base | A number that represents the exponent of the function, to which the base will be raised. mod | A number with which the modulowill be computed. Return value: The output of Code: baseCode: expUsing the pow() function without the mod argument When using the Code: pow(x, y)Code: x**yCode: xCode: yComparison of the two methods Code: >>> pow(6, 4)
1296
>>> 6 ** 4
1296The Code: pow()Examples using float types Code: >>> pow(3.0, 4)
81.0
>>> pow(4.5, 2.3)
31.7971929089206As 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. Example using complex numbers Code: >>> pow(4+2j, 3)
(16+88j)The return type will also depend on whether your arguments are non-negative or negative, as is shown in the below table. base | exp | Return type Non-negative | Non-negative | int Non-negative | Negative | foat Negative | Non-negative | int Negative | Negative | float Examples of return values with different input types Code: >>> pow(7, 2)
49
>>> pow(4, -5)
0.0009765625
>>> pow(-6, 3)
-216
>>> pow(-9, -4)
0.00015241579027587258What sets the Code: pow()Code: **Code: modThe process of operations when using the mod argument is as follows: If we have Code: pow(x, y, z)Code: xCode: yCode: zCode: (x**y) % zExamples using mod Code: >>> pow(14, 7, 5)
4
>>> pow(-8, 3, 5)
3
>>> pow(2, 4, -3)
-2The general rule for using the mod argument is that all values must be of integer type, the Code: expCode: expSo, when using the Code: pow()Code: expCode: pow(inv_base, -exp, mod)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()Example of modular inverse Code: >>> pow(87, -1, 25)
23In this example, the straight modular inverse is calculated because Code: inv_baseExample of modular inverse when exp is not -1 Code: >>> 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)
10Unfortunately, Python does not have a built-in function to calculate the nth root of a number. The math module only has a function to calculate square roots, Code: math.sqrt()We know that Code: nxCode: x1nCode: pow(x, (1/n))Code: x**(1/n)Examples of calculating nth roots Code: >>> pow(625, (1/4))
4.0
>>> 729**(1/3)
8.999999999999998Note that performing an nth root calculation will always return a float when not using complex numbers. Since Python’s float type works on approximations, 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. 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. Examples of calculating nth roots of negative bases Code: >>> pow(-16, (1/2))
(2.4492935982947064e-16+4j)
>>> pow(-27, (1/3))
(1.5000000000000004+2.598076211353316j)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 Fundamental theorem of algebra. math.pow() Function In the math moduleof Python, there is a similar function called Code: math.pow()Code: pow()Code: math.pow()Code: math.pow()Example of using math.pow() Code: >>> import math
>>> math.pow(9, 5)
59049.0When deciding between using the Code: pow()Code: **Code: timeit.timeit()Code: timeitExamples of using timeit with simple numbers Code: >>> import timeit
>>> timeit.timeit('pow(5, 2)')
0.25059129999863217
>>> timeit.timeit('5**2')
0.008814800001346157When performing a simple power computation, the Code: **Examples using modulo Code: >>> timeit.timeit('pow(52, 2, 4)')
0.7482693000001746
>>> timeit.timeit('52**2 % 4')
0.012026999998852261The same is true even when we include a modulo operation. However, when we want to perform power operations with very large numbers, the Code: pow()Code: pow()Examples using large numbers Code: >>> timeit.timeit('pow(5234, 2341, 124)')
0.9020593000004737
>>> timeit.timeit('5234**2341 % 124')
152.56075580000106Here the Code: pow()Code: **Code: pow()The post A Guide to Python’s pow() Function first appeared on Finxter. |