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

Categories

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

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

1 Answer

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)

"""

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...