Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Python creating a complex number here?

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?

like image 841
mstreffo Avatar asked Apr 01 '21 10:04

mstreffo


People also ask

Why does Python have complex numbers?

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).

Why did we create complex numbers?

"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."

Why does Python use j instead of i for complex numbers?

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).

How to create a complex number in Python?

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.

What is a complex number?

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.

How to create numbers in Python?

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.

How to convert complex number to int or float in Python?

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.


2 Answers

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.

like image 187
Chris Doyle Avatar answered Oct 09 '22 00:10

Chris Doyle


Hope that explains

>>> -0.021678371395529073 ** 0.09090909090909091
-0.7058780799007794
>>> (-0.021678371395529073) ** 0.09090909090909091
(0.6772850578932906+0.1988688362687656j)
>>> 0.021678371395529073 ** 0.09090909090909091
0.7058780799007794
like image 35
Neeraj Avatar answered Oct 08 '22 23:10

Neeraj