imaginary number i - Printable Version +- Python Forum ( https://python-forum.io)+-- Forum: Python Coding ( https://python-forum.io/forum-7.html)+--- Forum: General Coding Help ( https://python-forum.io/forum-8.html)+--- Thread: imaginary number i ( /thread-35009.html) |

imaginary number i - mr_byte31 - Sep-23-2021
I was trying the following commands : >>> -1 ** 0.5 -1.0 >>> (-1) ** 0.5 (6.123233995736766e-17+1j)I was expecting "1j" , "1i" or "i" I am just surprised to find different values for the same problem. I was expecting to see that python has higher priority of "sign" over the power "**" any reason for those results? RE: imaginary number i - bowlofred - Sep-23-2021
The second one is 1j to within the precision of the computation. Floating point math isn't necessarily exact. https://docs.python.org/3/tutorial/floatingpoint.html RE: imaginary number i - deanhystad - Sep-23-2021
The reason -1**0.5 == -1 is that ** has higher precedence than unary -. The reason (-1)**5 ~= 1j is that the parenthesis change the order of operations. Unary - is first, then **. As for the small remainder, you don't need imaginary numbers to see that you cannot represent an infinite set of numbers using a limited set of bytes. RE: imaginary number i - mr_byte31 - Sep-23-2021
then why the first one not correct?? >>> -1 ** 0.5 -1.0 RE: imaginary number i - jefsummers - Sep-23-2021
The first one IS correct. The square root of 1 is 1 Then apply the unary minus and you get -1 RE: imaginary number i - bowlofred - Sep-23-2021
(Sep-23-2021, 04:01 PM)mr_byte31 Wrote: then why the first one not correct?? As mentioned, the precedence rules mean the exponentiation happens first. The effect is the same as - (1 ** 0.5)Operator precedence RE: imaginary number i - deanhystad - Sep-23-2021
It gets really confusing when it looks like Python can exactly represent all the numbers involved. Why is 0.3 not 0.3? print(0.1, 0.2, 0.1 + 0.2, 0.3)
RE: imaginary number i - mr_byte31 - Sep-23-2021
(Sep-23-2021, 03:59 PM)deanhystad Wrote: The reason -1**0.5 == -1 is that ** has higher precedence than unary -. This is the first programming language that I see to give priority to "power" than to sign "-" RE: imaginary number i - buran - Sep-23-2021
(Sep-23-2021, 04:28 PM)mr_byte31 Wrote: This is the first programming language that I see to give priority to "power" than to sign "-" https://math.stackexchange.com/q/1299236 And one of the answers gives an example - FORTRAN Same precedence in Matlab - https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html |