0

My Python has started to imagine that square root of -1 is 1, so be careful with ** and unary -.

print (-1**0.5) ## ** binds stronger than unary minus!

i=-1
print "Square root of %i is %f" (i,i**0.5) # gives proper error
""" Output:
-1.0

Traceback (most recent call last):
  File "D:/test/sqrt-1.py", line 4, in <module>
    print "Square root of %i is %f" (i,i**0.5) # gives proper error
ValueError: negative number cannot be raised to a fractional power
"""

Also (and more difficult to see):

>>> i=1
>>> -i**0.5
-1.0
>>> (-i)**0.5

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    (-i)**0.5
ValueError: negative number cannot be raised to a fractional power
>>>

Edited by pyTony: n/a

6
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by Skyelher
0

Works correctly.
Order of operations.

This also works:

(-1+0j)**.5

without error.

Edited by jcao219: n/a

0

I gave the solution myself but wanted to warn that not to use ** without ().

0

Works correctly.
Order of operations.

This also works:

(-1+0j)**.5

without error.

sqrt(-1) = j, the basic definition of complex numbers, so interesting thing is that the result is not actually exact:

>>> (-1+0j)**.5
(6.123233995736766e-17+1j)

Edited by pyTony: n/a

0

lol i used to get marks deducted for not encircling -1 in brackets in math class

Edited by ultimatebuster: n/a

0

Was one of the best in my age class in high school, but for me it is not obvious that unary minus would not have highest priority. You must have had good teacher.

Or maybe I am only getting senile:S

0

you have no idea lol.
This would be 1 mark off:

[TEX]\frac{1}{2}^2[/TEX]

The correct form would be:
[TEX](\frac{1}{2})^2[/TEX]

0

I'm still in high school, taking pre-calculus next year.
I just learned about imaginary numbers (-1)**.5 last year in Algebra class.

0

I just took Algebra 2/ Trig, and this was extremely annoying. I knew to do it in my head, but, just like ultimatebuster, my teacher took off for it.
I get Calc next year, and i hope there isn't any of this

0

Why is it a bug, then?

Everyone can report what the think is a bug,that dos not mean is a real bug.
This bug report was closed,and was not a bug.
Just something that easy can be misunderstood -1 need to be (-1) to work correct.

Edited by snippsat: n/a

0

Everyone can report what the think is a bug,that dos not mean is a real bug.
This bug report was closed,and was not a bug.
Just something that easy can be misunderstood -1 need to be (-1) to work correct.

Thanks for clearing it up...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.