Advertisement
mjawili

Marco Jawili, Quadratic Equation

Mar 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def get_coeff():
  5.     """
  6.  function displays a prompt to the user and guarantees the returned
  7.  value is float.
  8.  :
  9.  This function takes input from the user and utilizes
  10.  exception handling to make sure that the user only inputs numbers.
  11.  :
  12.  
  13.  """
  14.     while True:
  15.         try:
  16.            
  17.             a = float(input("a: "))
  18.             b = float(input("b: "))
  19.             c = float(input("c: "))
  20.            
  21.        
  22.             return (a,b,c)
  23.        
  24.         except ValueError:
  25.             print ("Please only enter a numebr")
  26.         else:
  27.             if float(a) == 0:
  28.                 print ("You cannot divide by zero")
  29.            
  30.                
  31.        
  32. def quad_frm(a,b,c):
  33.     """
  34.  given the coefficients for a quadratic equation, returns the roots, either
  35.  real or imaginary, as a tuple of values.
  36.  :
  37.  :
  38.  This code starts by intializing variables of the quadratic formula to be more legible for the reader.
  39.  It sets the discriminant as well as the denominator to avoid repitition.
  40.  Then it checks whether the discriminant is negative or positive to establish whether the roots are real or complex.
  41.  Lastly, it returns the complex roots in a tuple of tuples and the real roots as just a singular tuple.
  42.  
  43.  
  44.  BEGIN DOCTESTS
  45.  >>> quad_frm(1,2,1)
  46.  (-1.0, -1.0)
  47.  
  48.  >>> quad_frm(1, 0, 1)
  49.  ((0.0, 1.0), (0.0, -1.0))
  50.  
  51.  >>> quad_frm(1, -1, -6)
  52.  (3.0, -2.0)
  53.  
  54.  >>> quad_frm(1,-4,13)
  55.  ((2.0, 3.0), (2.0, -3.0))
  56.  
  57.  >>> quad_frm(2,-1,-1)
  58.  (1.0, -0.5)
  59.  """
  60.     discriminant = (b**2)-(4*a*c)
  61.     denominator = 2 * a
  62.    
  63.     if discriminant < 0:
  64.         complexRoot1 = ((discriminant/-1)**.5)/(denominator)
  65.         complexRoot2 = -((discriminant/-1)**.5)/(denominator)
  66.         b = (b*(-1))/(denominator)
  67.        
  68.         return((b,complexRoot1),(b,complexRoot2))
  69.     else:
  70.         rOne = (-(b) + ((discriminant)**.5))/denominator
  71.         rTwo = (-(b) - ((discriminant)**.5))/denominator
  72.         return(rOne,rTwo)
  73.  
  74. def print_roots(rOne,rTwo):
  75.     """
  76.   This function checks for real and imaginary roots and formats accordingly,
  77.   all numbers formatted to round for three decimals.
  78.   """
  79.     if type(rOne) == tuple and type(rTwo) == tuple:
  80.         realNumber = round(rOne[0],3)
  81.         rOneComplex = round(rOne[1],3)
  82.         rTwoComplex = round(rTwo[1],3)
  83.         print ("r1: " + str(realNumber) + " +" + str(rOneComplex) + "*i" " r2: " + str(realNumber) + str(rTwoComplex) + "*i")
  84.        
  85.     else:
  86.         print ("r1: " + str(round(rOne , 3)) + " r2: " + str(round(rTwo, 3)))
  87.        
  88. def main():
  89.     value = get_coeff()
  90.     x = quad_frm(value[0],value[1],value[2])
  91.     print_roots(x[0],x[1])    
  92.  
  93. if __name__ == "__main__":
  94.     import doctest
  95.     doctest.testmod()
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement