'''
Created on Jun 16, 2013

@author: Jared

This program asks the user for a, b, and c from the quadratic equation,
and then solves the equation for roots, regardless of whether they are complex.

Python conversion of Arup's Java code
'''

from math import sqrt

def main():
        # Get the input from the user.
        a = float(input("Enter a from your quadratic equation.\n"))
        b = float(input("Enter b from your quadratic equation.\n"))
        c = float(input("Enter c from your quadratic equation.\n"))

        # Calculate the discriminant.
        discriminant = pow(b, 2) - 4 * a * c

        # Complex root case.
        if (discriminant < 0):
            # Calculate both the real and imaginary components of the roots.
            realpart = -1 * b / (2.0 * a);
            imaginarypart = sqrt(abs(discriminant)) / (2.0 * a);
            
            # fix negative 0
            if (realpart == -0.0):
                realpart = 0.0;

            # Print out each root, one component at a time.
            print("First root = " + str(realpart) + " + " + str(imaginarypart) + "i.")
            print("Second root = " + str(realpart) + " - " + str(imaginarypart) + "i.")

        # Real root case.
        else:
            r1 = (-b + sqrt(discriminant)) / (2 * a)
            r2 = (-b - sqrt(discriminant)) / (2 * a)
     
            print("The roots to your quadratic are " + str(r1) + " and " + str(r2) + ".")
        
# runs main module on startup
if __name__ == "__main__":
    main()
