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?
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.
then why the first one not correct??
>>> -1 ** 0.5
-1.0
The first one IS correct.
The square root of 1 is 1
Then apply the unary minus and you get -1
(Sep-23-2021, 04:01 PM)mr_byte31 Wrote: [ -> ]then why the first one not correct??
>>> -1 ** 0.5
-1.0
As mentioned, the precedence rules mean the exponentiation happens first. The effect is the same as
- (1 ** 0.5)
Operator precedence
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)
Output:
0.1 0.2 0.30000000000000004 0.3
(Sep-23-2021, 03:59 PM)deanhystad Wrote: [ -> ]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.
This is the first programming language that I see to give priority to "power" than to sign "-"