#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481

    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

    • Nyktos agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    cmath


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

IMN logo majestic logo threadwatch logo seochat tools logo