0

I'm currently having trouble understanding how to make imaginary numbers appear when I'm doing the quadratic equation. My assignment is to make the quadratic equation, and get imaginary numbers but I'm having an extremely difficult time getting there. any help you can offer would be great!

Here is the code i currently have:

import math

    print "Hello, please insert 3 numbers to insert into the quadratic equation."

a = input("Please enter the first value: ")

b = input("Please enter the second value: ")

c = input("Please enter the third value: ")

rootValue = b**2 - 4*a*c

if rootValue < 0:

    print (-b-(rootValue)**(.5)) / (2 * a)

if rootValue > 0:
    print ((-b + (rootValue)**(1/2)) /(2 * a))

if rootValue == 0:
    print -b/(2*a)

please help!!! i'm so stuck right now. I think you have to do something with the problem if rootValue < 0; but I'm not sure how to do that. I'm also not allowed to use 'import cmath', I'm supposed to make it so that you can just do this this way.

1 Answers1

0

There are a couple of problems with your code besides how to represent complex numbers. Remember that if rootValue <> 0, there are ALWAY TWO roots: (-b +/- sqrt(rootValue)/ 2a

  1. It doesn't matter if the rootValue is positive or negative, there's still two roots. You are branching and only providing one of the two roots in each case. No need for the first two if statements
  2. To make rootValue complex, so that you can have complex result when you take the square root, either set it equal to complex(b2 - 4*a*c, 0) or to (b2 - 4*a*c, 0) + 0j.
  3. You want to raise things to the 0.5 power for each of the two roots, NOT the (1/2) power, as you've done in one statement
  4. For completeness, you may want to deal with the a = 0 case.

If you still have problems, let us know.

ViennaMike
  • 2,207
  • 1
  • 24
  • 38