代码是:
-f
当我进入时,它给了我错误的答案:
A = b/a
B = c/a
C = d/a
Q = (A**2-3*B)/9
R = (2*A**3-9*A*B+27*C)/54
M = R**2-Q**3
p = (3*B-A**2)/3
q = (2*A**3-9*A*B+27*C)/27
delta = (q/2)**2+(p/3)**3
if M <= 0 :
from math import sqrt,acos,cos,pi
Z = acos(R/sqrt(Q**3))
X1 = -(2*sqrt(Q)*cos(Z/3))-A/3
X2 = -(2*sqrt(Q)*cos((Z+2*pi)/3))-A/3
X3 = -(2*sqrt(Q)*cos((Z-2*pi)/3))-A/3
elif delta > 0 :
from math import sqrt
import cmath
u = ((-q/2)+sqrt(delta))**(1/3)
v = ((q/2)+sqrt(delta))**(1/3)
X1 = u-v-A/3
X2 = -(1/2)*(u-v)-A/3+(u+v)*(sqrt(3)/2)*cmath.sqrt(-1)
X3 = -(1/2)*(u-v)-A/3-(u+v)*(sqrt(3)/2)*cmath.sqrt(-1)
但它会跳过M并转到delta
结果是:
a = 1 b = 1 c = -1 d = -1
M == 0
我不知道出了什么问题? 假设通过M
计算到M和X1,X2和X3变为实数答案 0 :(得分:2)
提升到分数幂的负数或者给出一个复数(在Python 3.x中)(即使是奇数幂,如1/3或1/5)或Python 2.x中的值错误({{1 }})。
示例 -
ValueError: negative number cannot be raised to a fractional power
你需要自己处理这个案子,例如,如果你正在执行(1/3)的权力,你必须像 -
一样处理它>>> (-27)**(1/3)
(1.5000000000000004+2.598076211353316j)