What's wrong in my code???ValueError: math domain error Python -sqrt

710 Views Asked by At
import math
a,b,c = map(int,input().split())
x = float((-b + math.sqrt(b**2-4*a*c))/2*a)
y = float((-b - math.sqrt(b**2-4*a*c))/2*a)

if y>x:
    x = x1
    y = x
    x1 = y
    
if (b**2-4*a*c >=1):
    print("Two different roots "+"x1=",int(x)," , "+"x2=",int(y),sep="")
elif (b**2-4*a*c == 0):
    print("Two same roots x=",int(x),sep="")
else:
    print("No real root")

Error message:

Traceback (most recent call last):
  File "/7760537/code_7760537.py", line 3, in 
    x = float((-b + math.sqrt(b**2-4*a*c))/2*a)
ValueError: math domain error

What's wrong in code?? OAO What is "math domain error" meaning?? Sorry my English is not good....

1

There are 1 best solutions below

1
Sozy On

Maybe your b**2-4*a*c gives a negativ number which doesn't work in math, you should put a

delta = b**2-4*a*c
if delta > 0:
    x = float((-b + math.sqrt(delta))/2*a)
    y = float((-b - math.sqrt(delta))/2*a)