python - 我的代码有什么问题???ValueError:数学域错误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=",int(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
代码有什么问题??OAO “数学域错误”是什么意思??对不起,我的英语不好......
解决方案
也许你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)
推荐阅读
- python - 包含编号和名称排序的列表
- vba - VBA最小值和表格
- visual-studio-code - 如何在 Visual Studio 代码中按源列出快捷方式?
- android - 水平 RecyclerView 的 ItemTouchHelper
- javascript - 如何使 EXT JS 模型和代理商店工作?(请帮忙!)
- amazon-web-services - 如何从控制台获取 aws apigateway 中 api 的部署 ID?
- amazon-web-services - 具有 S3 源的 CloudFront 分配以 XML ListBucketResult 响应
- excel - 使用没有日期/时间格式的 Excel VBA 检查时间
- javascript - 开发工具如何确定它可以在控制台中预览和不能预览的内容?
- angular - 如何使用拖放在 Angular 4 中使用 Kendo UI 上传文件