I'm getting a strange error in Python 3.8:
When I run this calculation, I get a complex number:
>>> (1.0 / (2.0 - 0.5222232592740141 * 92.16159106468214)) ** (1.0 / (10.0 + 1))
(0.6772850578932906+0.1988688362687656j)
However, if I calculate this manually in Python I get a float answer:
>>> (1.0 / (2.0 - 0.5222232592740141 * 92.16159106468214))
-0.021678371395529073
>>> (1.0 / (10.0 + 1))
0.09090909090909091
>>> -0.021678371395529073 ** 0.09090909090909091
-0.7058780799007794
Why is this?
Complex numbers have their uses in many applications related to mathematics and python provides useful tools to handle and manipulate them. An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y).
"These arise as solutions to the wave equations, so we use complex numbers to describe various waves, such an electromagnetic wave. Thus, as in math, complex calculus in physics is an extremely useful tool for simplifying calculations."
I is the variable, and A is the unit. i is only ever used for imaginary numbers. j as a suffix is actually more confusing, because J as a suffix is actually used in physics and engineering (Joules). i is sometimes used to represent AC current in order to distinguish from DC values (for example).
A complex number contains two part – real and imaginary. The imaginary part is written with the “j” suffix. We can also use the complex () function to create a complex number. We can pass two ints or float arguments to the complex () function. The first argument is the real part and the second argument is the complex part.
Complex number is made up of real and imaginary parts. Real part is a float number, and imaginary part is any float number multiplied by square root of -1 which is defined as j. The resulting object is of complex data type.
In Python, numbers are also an object. Their data types are – int, float, and complex. There are built-in functions to create numbers – int (), float (), and complex (). We can also create a number by directly assigning the value to a variable. The complex numbers are mostly used in geometry, calculus and scientific calculations.
We can use complex () function to convert an int or float to the complex number, the imaginary part will be 0j. We can’t convert a complex number to int or float. Numbers are an integral part of any programming language. Python support three types of numbers – int, float, and complex.
your not taking into account the negative at the start. what python will actually interpret
-0.021678371395529073 ** 0.09090909090909091
as is really -(0.021678371395529073 ** 0.09090909090909091)
So do the power with 2 positive numbers then apply the negative. However if you wrap the first number and negative together to python knows the negative applies to that first number not just the net result of the expression you get the complex number
(-0.021678371395529073) ** 0.09090909090909091
(0.6772850578932906+0.1988688362687656j)
this is what happens in your real example as python knows the term is negative, not the result
To explain why this is happening you have to look at the order of operations. In your expression you have two operations -
and **
and you have two terms 0.021678371395529073
, 0.09090909090909091
below table lays out the order of operations, those that appear higher in the table are done first.
Operator
Description
() Parentheses (grouping)
f(args...) Function call
x[index:index] Slicing
x[index] Subscription
x.attribute Attribute reference
** Exponentiation
~x Bitwise not
+x, -x Positive, negative
*, /, % Multiplication, division, remainder
+, - Addition, subtraction
<<, >> Bitwise shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in, not in, is, is not, <, <=, >, >=,
<>, !=, == Comparisons, membership, identity
not x Boolean NOT
and Boolean AND
or Boolean OR
lambda Lambda expression
from this table you can see that Exponentiation(**) has a higher precedent than Negation (-x). So that means python will first raise to the power of the two terms, then apply the negation to the result of that. So if you want the negation to be applied to the first number before raising to the power, then you need to wrap that in Parentheses.
Hope that explains
>>> -0.021678371395529073 ** 0.09090909090909091
-0.7058780799007794
>>> (-0.021678371395529073) ** 0.09090909090909091
(0.6772850578932906+0.1988688362687656j)
>>> 0.021678371395529073 ** 0.09090909090909091
0.7058780799007794
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With