[英]turn negative number to imaginary
I am making a program that can calculate the resolution of a quadratic equation. 我正在制作一个可以计算二次方程式分辨率的程序。 This works when the discriminant
d
is non-negative, but not for complex roots. 当判别式
d
为非负数时有效,但对于复数根而言则无效。
How can I insert a imaginary square root? 如何插入假想的平方根?
import math
a = input("insert the value of a: ")
b = input("insert the value of b: ")
c = input("insert the value of c: ")
def d(a,b,c) : return (b**2)-(4.*a*c)
def x1(d,a,b,c) : return (b-(2.*b))+(math.sqrt ( d ))/2.*a
def x2(d,a,b,c) : return (b-(2.*b))-(math.sqrt ( d ))/2.*a
print ("the values of D,X' e X'' respectively: ")
print (d(a,b,c),x1(d,a,b,c),x2(d,a,b,c))
Also, when I use a=4 b=4 c=1
and d
was going to be 0
, I get the following error. 另外,当我使用
a=4 b=4 c=1
且d
将为0
,出现以下错误。 I'm not sure what's wrong here. 我不确定这是怎么回事。
Traceback (most recent call last):
File "H:/Python27/equation2.py", line 9, in <module>
print (d(a,b,c),x1(d,a,b,c),x2(d,a,b,c))
File "H:/Python27/equation2.py", line 6, in x1
def x1(d,a,b,c) : return (b-(2.*b))+(math.sqrt ( d ))/2.*a
TypeError: a float is required
You're trying to take the sqrt of a function. 您正在尝试使用某个函数的sqrt。 The function name does not automatically represent the result of the most recent call.
函数名称不会自动表示最近调用的结果。 Try a temporary variable:
尝试一个临时变量:
d_result = d(a,b,c)
print (d_result, x1(d_result,a,b,c), x2(d_result,a,b,c))
This fixes only the fatal error; 这仅修复了致命错误; the immediate problem is still that you don't handle complex roots.
当前的问题仍然是您不处理复杂的根。 Note: for your given case of a=b=c=4, the discriminant is -48, not 0, as your posting states.
注意:对于您给定的a = b = c = 4的情况,判据为-48, 而不是 0,如您的过帐状态所示。
Here, you have two courses: 在这里,您有两门课程:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.