我的代码有什么问题???ValueError: math domain error Python -sqrt

问题描述

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=",sep="")
else:
    print("No real root")

错误信息:

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

代码有什么问题??澳航 “数学域错误”是什么意思?? 抱歉我的英语不好....

解决方法

也许您的 b**2-4*a*c 给出了一个在数学中不起作用的负数,您应该输入一个

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)