I am getting errors trying to raise a negative number to a decimal power. For example, I want to calculate this
(-2)^(4.8)
which should result in valid, real number output (27.85 from my TI-89).
I have tried using numpy.power(), but according to their documentation “Negative values raised to a non-integral value will return Nan” unless you specify that the type to be complex
np.power(-2.0,4.8)
<ipython-input-3-8fe5c2443c98>:1: RuntimeWarning: invalid value encountered in power
np.power(-2.0,4.8)
nan
np.power((-2),4.8, dtype = complex)
(-22.53728640541592+16.374297039371754j)
The same is similar when using math.pow(), resulting in Nan.
I could make a workaround by using the law of exponents (given that a numerator is even):
#(-2)^(4.8) == (-2)^(48/10) --> (-2)^(24/5) num = np.power(-2.0, 24)
np.power(num,1/5)
27.85761802547598
But is there existing way to compute a negative base to a decimal exponent? Or is this the only way?
westcoasting__ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
If you don’t believe np.power
with dtype='complex'
, use numpy.emath
, “Mathematical functions with automatic domain”.
import numpy as np
np.emath.power(-2, 4.8)
It gives you the correct result, which is complex: (-22.53728640541592+16.374297039371754j)
Or since you know the result is complex, just use built in complex math by making at least one of the arguments complex.
(-2+0j)**4.8
(-22.537286405415916+16.37429703937175j)
Your TI is giving you the magnitude of the complex number.
abs((-2+0j)**4.8)
27.85761802547597
0