Domain error with square roots
<p>I am trying to copy a code from the book I am learning from. The program is supposed to find the square roots of a quadratic function but once I run the module in IDLE I get an error.</p>

<pre><code>#This is a program to find the square roots of a quadratic function.



import math

def main():

    print ("this is a program to find square roots of a quadratic function")
    print()
    a,b,c = eval(input("enter the value of the coefficients respectively"))

    discRoot = math.sqrt( b * b - 4 * a * c )

    root1 = ( - b + discRoot ) / 2 * a 
    root2 = ( - b - discRoot ) / 2 * a

    print ("The square roots of the equation are : ", root1, root2 )
    print()

main()
</code></pre>

<p>I am getting the following error :</p>

<pre><code>Traceback (most recent call last):
File "/home/ubuntu/Desktop/untitled.py", line 21, in &lt;module&gt;
main()
File "/home/ubuntu/Desktop/untitled.py", line 13, in main
discRoot = math.sqrt( b * b - 4 * a * c )
ValueError: math domain error
</code></pre>

<p>What exactly am I doing wrong here ? Is it because the discRoor value is turning out to be negative ? </p>

<p>Everytime <code>b * b - 4 * a * c</code> is a negative number, <code>math.sqrt(b * b - 4 * a * c)</code> will raise a <code>ValueError</code>.</p>

<p>Either check that before, or use <code>sqrt</code> from the <a href="https://docs.python.org/3.4/library/cmath.html" rel="nofollow"><code>cmath</code></a> module to allow complex roots:</p>

<pre><code>.
.
delta = b * b - 4 * a * c
if delta &gt; 0:
     discRoot = math.sqrt(delta)
else:
    print("No solutions")
.
.
</code></pre>

<p>Or allow complex roots:</p>

<pre><code>import cmath

.
.
discRoot = cmath.sqrt( b * b - 4 * a * c )
.
.
</code></pre>

python python-3.x