Quadratic formula solver in python
<p>I'm somewhat new to python, but I'm trying my best to learn. My code is</p>

<pre><code>import math

a = 5
b = 5
c = 5

def quad_solve(a, b, c):
    q1 = b*b
    q2 = 4*a*c
    q3 = 2*a
    q4 = q1-q2
    sqr = math.sqrt(q4)
    sol1p1 = b+sqr
    sol1p2 = sol1p1/2
    sol2p1 = b-sqr
    sol2p2 = sol2p1/2
    print ("(",sol1p2,",",sol2p2,")")

quad_solve(a, b, c)
</code></pre>

<p>And when I run it it gives the error </p>

<pre><code>Traceback (most recent call last):
  File "python", line 19, in &lt;module&gt;
  File "python", line 12, in quad_solve
ValueError: math domain error
</code></pre>

<p>which I don't really understand. </p>

<p>I'm trying to create a quadratic formula solver. I use the math module, and then define three variables, a, b, and c. Then, I define a function that takes in those variables (I call the function at the end). In the function, I define four quantities. <code>q1</code> is the b squared under the square root, <code>q2</code> is the 4ac also under the square root, <code>q3</code> is the denominator, and <code>q4</code> calculates the total under the square root (i.e., <code>q1</code> - <code>q2</code>). Then, I define a variable called <code>sqr</code> which is equal to the square root of <code>q4</code>. Then, I define four more variables, which calculate the solutions. <code>sol1p1</code> takes b + sqr, and <code>sol1p2</code> takes <code>sol1p1</code> and divides it by two. This gives the first solution. Then, <code>sol2p1</code> takes b - sqr, and <code>sol2p2</code> takes <code>sol2p1</code> and divides it by two. Finally, <code>sol1p2</code> and <code>sol2p2</code> are printed, in a set of parentheses with a comma between. I hope that makes sense; if any clarification is needed about the variable names, please let me know. </p>

<p>I am using the online compiler repl.it (I don't know if there's anything special to consider with that).</p>

<p>Thanks!</p>

<hr>

<p><strong>Edit:</strong></p>

<p>I updated my code, per Code Apprentice's recommendations. I started by adding an if statement:</p>

<pre><code>import math

a = 5
b = 5
c = 5

def quad_solve(a, b, c):
    q1 = b*b
    q2 = 4*a*c
    q3 = 2*a
    q4 = q1-q2
    check = math.tan(q2)
    if (q1 &gt; check):
        sqr = math.sqrt(q4)
        sol1p1 = b+sqr
        sol1p2 = sol1p1/2
        sol2p1 = b-sqr
        sol2p2 = sol2p1/2
        print ("(",sol1p2,",",sol2p2,")")
    else:
        print "Imaginary number. There are no zeros."

    quad_solve(a, b, c)
</code></pre>

<p>but it is continuing to return the error </p>

<pre><code>Traceback (most recent call last):
  File "python", line 23, in &lt;module&gt;
  File "python", line 14, in quad_solve
ValueError: math domain error
</code></pre>

<p>I'm not sure why. </p>

<p>There are two ways to do this (I figured this out thanks to the great hints given by Code-Apprentice, Pablo Iocco, and Tom Pitts).</p>

<pre><code>import cmath
import math

a = 1
b = 3
c = 2

def quad_solve_exact(a, b, c):
    d = (b*b)-(4*a*c)
    solution1 = (-b-cmath.sqrt(d))/(2*a)
    solution2 = (-b+cmath.sqrt(d))/(2*a)
    print (solution1,solution2)

quad_solve_exact(a, b, c)

def quad_solve(a, b, c):
    if (b*b &gt; 4*a*c):
        print "There is a solution!"
        d = math.sqrt((b*b)-(4*a*c))
        solution1 = (-b-math.sqrt(d))/(2*a)
        solution2 = (-b+math.sqrt(d))/(2*a)
        print (solution1,solution2)
    else:
        print "No solutions, imaginary number"

quad_solve(a, b, c)
</code></pre>

<p>The first way is the more exact. The problem is the <code>sqrt</code> function used by the normal <code>math</code> package can't handle negative numbers. There is a package, <code>cmath</code>, however, that <em>can</em> handle negative numbers. So, you import both packages (the normal <code>math</code> package is used in the second example) and then define a, b, and c. Then, in your function you can combine the variables much more than I did, leading to shorter/clearer code. So, the variable <code>d</code> is used to denote what is under the square root. Then, for each solution, -b is added or subtracted to the square root of <code>d</code>, all of which is divided by 2a. Then, the solutions are printed.</p>

<p>The second solution is less exact, but perfectly serviceable for my purposes. The new function also takes a, b, and c. Then, an if statement is used to make sure that the number being square rooted is not negative. If it is negative, then the else statement is ran, which prints that there is no solution. If the variables pass the if statement, it prints that there is indeed a solution, and basically uses the same code as before, except instead of <code>cmath.sqrt</code>, <code>math.sqrt</code> is used.</p>

python math compiler-errors