Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 13th, 2013, 09:26 PM
 Dusto
Registered User

Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 43 m 55 sec
Reputation Power: 0
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.

#2
April 13th, 2013, 10:03 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
Use the cmath module

Code:
```>>> import cmath
>>> cmath.sqrt(-23)
4.795831523312719j
>>> cmath.log(complex(-23,22))
(3.4603357521243416+2.378413055519064j)
>>> ```
Nyktos agrees!
__________________
[code]Code tags[/code] are essential for python code!

#3
April 13th, 2013, 10:56 PM
 Dusto
Registered User

Join Date: Mar 2013
Posts: 4
Time spent in forums: 1 h 43 m 55 sec
Reputation Power: 0
cmath

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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Imaginary numbers and square roots