Mark C
Mark C

Reputation: 11

Math syntax error in python script

Hello Stack Overflow Community! When I try to run this python script (its purpose is to solve for x in a quadratic equation), it returns this message:

Traceback (most recent call last):
    File "/private/var/mobile/Containers/Shared/AppGroup/C692DE78-B0DD-44FF-9815-4675E6956B0A/Pythonista3/Documents/quadratic solver.py", line 13, in <module>
    root_b_squared_minus_four_a_c = sqrt(b_squared_minus_four_a_c)
ValueError: math domain error

Here is the code I am running:

from math import *
# Set up Variables A, B, and C, by asking for input
a=int(input("Please enter an a value: "))
b=int(input("Please enter a b value: "))
c=int(input("Please enter a c value: "))
#Set up variables for later use, ac, 4ac, and 2a, in the quadratic formula
ac = a*c
four_ac = 4 * ac
two_a = 2*a
b_squared_minus_four_a_c = int(b*b - four_ac)
minus_b = -b
answer_one_step_one = 0
root_b_squared_minus_four_a_c = sqrt(b_squared_minus_four_a_c)
#This section is the solution to the quadratic if a is equal to one, to prevent against printing 1 in front of x

if a == 1:
     print("solving equation x^2+" + str(b) + "x+" + str(c))
     answer_one_step_one = minus_b + root_b_squared_minus_four_a_c
     answer_one = answer_one_step_one / two_a

     answer_two_step_one = minus_b - root_b_squared_minus_four_a_c
     answer_two = answer_two_step_one / two_a
     print("The two solutions for x are:" + str(answer_one) + ",and " + str(answer_two))

else:
    print("Solving equation" + str(a)+ "x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + sqrt(b_squared_minus_four_a_c)
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - sqrt(b_squared_minus_four_a_c)
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and " + str(answer_two))

How do I fix this??

Thanks!

Mark

Upvotes: 1

Views: 208

Answers (3)

oli5679
oli5679

Reputation: 1749

As some of the other comments also indicate, the error is caused by you taking the square root of a negative number. If you add an if condition before everything else that checks for this, it should resolve your issue:

#First check if there are any real solutions?
if b_squared_minus_four_a_c < 0:
    print('No real solutions')

elif a == 1:
    print("solving equation x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + root_b_squared_minus_four_a_c
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - root_b_squared_minus_four_a_c
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and "     + 
    str(answer_two))

else:
    print("Solving equation" + str(a)+ "x^2+" + str(b) + "x+" + str(c))
    answer_one_step_one = minus_b + sqrt(b_squared_minus_four_a_c)
    answer_one = answer_one_step_one / two_a

    answer_two_step_one = minus_b - sqrt(b_squared_minus_four_a_c)
    answer_two = answer_two_step_one / two_a
    print("The two solutions for x are:" + str(answer_one) + ",and " +         
    str(answer_two))

Upvotes: 0

Angelo Ramirez Ortega
Angelo Ramirez Ortega

Reputation: 11

You are receiving this error because you are passing the sqrt() function a negative number. To avoid this validate when b^2 - 4ac is negative, because the square root of said result can only be obtained when it is is greater or equal to 0.

Upvotes: 1

Bob Jacobsen
Bob Jacobsen

Reputation: 1160

“ValueError: math domain error” in this case means you’re passing a negative number to the square-root calculation. It can’t do that, so that error is the result.

You need to reorganize your code to check for that possibility first. Not every quadratic equation has real solutions.

Upvotes: 0

Related Questions