Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

math domain error python for quadratic equation

I'm a complete beginner and for some reason when I try to run the code it says that there's a math domain error. I don't really understand what the problem is, help would be greatly appreciated:)

a = float(input('Rentrer la valeur de a : '))
b = float(input('Rentrer la valuer de b : '))
c = float(input('Rentrer la valuer de c : '))
delta = b**2 - 4*a*c

x1 = ((-b) + sqrt(delta)) / (2*a)
x2 = ((-b) - sqrt(delta)) / (2*a)

console:

Traceback (most recent call last):
  File "main.py", line 31, in <module>
    x1 = ((-b) + sqrt(delta)) / (2*a)
ValueError: math domain error
question from:https://stackoverflow.com/questions/65944316/math-domain-error-python-for-quadratic-equation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer is quite surprisingly easy: you need the math lib:


from math import sqrt

a = float(input('Rentrer la valeur de a : '))
b = float(input('Rentrer la valuer de b : '))
c = float(input('Rentrer la valuer de c : '))
delta = b**2 - 4*a*c

x1 = ((-b) + sqrt(delta)) / (2*a)
x2 = ((-b) - sqrt(delta)) / (2*a)

print(x1, x2)

"""

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...