Exponentiation

Syntax

• value1 ** value2
• pow(value1, value2[, value3])
• value1.__pow__(value2[, value3])
• value2.__rpow__(value1)
• operator.pow(value1, value2)
• operator.__pow__(value1, value2)
• math.pow(value1, value2)
• math.sqrt(value1)
• math.exp(value1)
• cmath.exp(value1)
• math.expm1(value1)

Exponentiation using builtins: ** and pow()

Exponentiation can be used by using the builtin `pow`-function or the `**` operator:

For most (all in Python 2.x) arithmetic operations the result's type will be that of the wider operand. This is not true for `**`; the following cases are exceptions from this rule:

• Base: `int`, exponent: `int < 0`:

• This is also valid for Python 3.x.

• Before Python 2.2.0, this raised a `ValueError`.

• Base: `int < 0` or `float < 0`, exponent: `float != int`

• Before python 3.0.0, this raised a `ValueError`.

The `operator` module contains two functions that are equivalent to the `**`-operator:

or one could directly call the `__pow__` method:

Computing large integer roots

Even though Python natively supports big integers, taking the nth root of very large numbers can fail in Python.

OverflowError: long int too large to convert to float

When dealing with such large integers, you will need to use a custom function to compute the nth root of a number.

Exponential function minus 1: math.expm1()

The `math` module contains the `expm1()`-function that can compute the expression `math.e ** x - 1` for very small `x` with higher precision than `math.exp(x)` or `cmath.exp(x)` would allow:

For very small x the difference gets bigger:

The improvement is significant in scientic computing. For example the Planck's law contains an exponential function minus 1:

Exponential function: math.exp() and cmath.exp()

Both the `math` and `cmath`-module contain the Euler number: e and using it with the builtin `pow()`-function or `**`-operator works mostly like `math.exp()`:

However the result is different and using the exponential function directly is more reliable than builtin exponentiation with base `math.e`:

Exponentiation using the math module: math.pow()

The `math`-module contains another `math.pow()` function. The difference to the builtin `pow()`-function or `**` operator is that the result is always a `float`:

Which excludes computations with complex inputs:

TypeError: can't convert complex to float

and computations that would lead to complex results:

ValueError: math domain error

Magic methods and exponentiation: builtin, math and cmath

Supposing you have a class that stores purely integer values:

Using the builtin `pow` function or `**` operator always calls `__pow__`:

The second argument of the `__pow__()` method can only be supplied by using the builtin-`pow()` or by directly calling the method:

While the `math`-functions always convert it to a `float` and use the float-computation:

`cmath`-functions try to convert it to `complex` but can also fallback to `float` if there is no explicit conversion to `complex`:

Neither `math` nor `cmath` will work if also the `__float__()`-method is missing:

TypeError: a float is required

Modular exponentiation: pow() with 3 arguments

Supplying `pow()` with 3 arguments `pow(a, b, c)` evaluates the modular exponentiation ab mod c:

For built-in types using modular exponentiation is only possible if:

• First argument is an `int`
• Second argument is an `int >= 0`
• Third argument is an `int != 0`

These restrictions are also present in python 3.x

For example one can use the 3-argument form of `pow` to define a modular inverse function:

Roots: nth-root with fractional exponents

While the `math.sqrt` function is provided for the specific case of square roots, it's often convenient to use the exponentiation operator (`**`) with fractional exponents to perform nth-root operations, like cube roots.

The inverse of an exponentiation is exponentiation by the exponent's reciprocal. So, if you can cube a number by putting it to the exponent of 3, you can find the cube root of a number by putting it to the exponent of 1/3.

Square root: math.sqrt() and cmath.sqrt

The `math` module contains the `math.sqrt()`-function that can compute the square root of any number (that can be converted to a `float`) and the result will always be a `float`:

The `math.sqrt()` function raises a `ValueError` if the result would be `complex`:

ValueError: math domain error

`math.sqrt(x)` is faster than `math.pow(x, 0.5)` or `x ** 0.5` but the precision of the results is the same. The `cmath` module is extremely similar to the `math` module, except for the fact it can compute complex numbers and all of its results are in the form of a + bi. It can also use `.sqrt()`:

What's with the `j`? `j` is the equivalent to the square root of -1. All numbers can be put into the form a + bi, or in this case, a + bj. `a` is the real part of the number like the 2 in `2+0j`. Since it has no imaginary part, `b` is 0. `b` represents part of the imaginary part of the number like the 2 in `2j`. Since there is no real part in this, `2j` can also be written as `0 + 2j`.