Why does complex number sign change?

Entering 'a=2-1/2i' returns 'a = 2 + 0.5i'. Entering 'b=2-0.5i' returns 'b = 2 - 0.5i'.
Why does the first example seem to change the sign of the imaginary part?
MATLAB sign change

 Accepted Answer

Matt J
Matt J on 28 Jul 2022
Edited: Matt J on 28 Jul 2022
Because it is equivalent to 2-1/(2i). In all likelihood, you intended to do this:
a=2-(1/2)*i
a = 2.0000 - 0.5000i

3 Comments

J B
J B on 28 Jul 2022
Edited: J B on 28 Jul 2022
Hi @Matt J, great, thank you for the explanation and correction suggestion. Much appreciated. Thus this seems to be an order-of-operations condition: '2i' is not # times # but rather # times variable, which makes it a variable multiple, which has higher precedence than the '1/2' division. Since 'i' is always and only the square root of -1, a number, I expected the order of operations to be '1/2', then that result times root -1 (i.e., left-to-right evaluation). Thanks for clearing up how MATLAB interprets this.
Thanks for the additional example, @Stephen23. In the context, that now makes sense. Much appreciated.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

J B
on 28 Jul 2022

Commented:

J B
on 28 Jul 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!