April 13th, 2013, 09:26 PM

Imaginary numbers and square roots
Hi all,
So I am pretty new to Python. For my homework assignment in my beginner's class, I am attempting to take a quadratic equation and transfer the a, b, and c values (provided by the user) into the quadratic formula. However, after I've imported the math module, if there is a negative square root, it will give me an error such as
Code:
>>> math.sqrt(44)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
math.sqrt(44)
ValueError: math domain error
I was wondering if there is a function I can use to account for negative squareroots or imaginary numbers, as that particular example would have been 2i√11
Edit: I should add, I would like it to solve the quadratic formula, not just plug in the values.
April 13th, 2013, 10:03 PM

Use the cmath module
Code:
>>> import cmath
>>> cmath.sqrt(23)
4.795831523312719j
>>> cmath.log(complex(23,22))
(3.4603357521243416+2.378413055519064j)
>>>
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!
April 13th, 2013, 10:56 PM

cmath
Interesting, I'll look into that module. Thanks.