-2

the code is :

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)

it gives me wrong answers when i enter :

a = 1 b = 1 c = -1 d = -1
M == 0     

but it skips M and goes to delta

and the results are :

X1 = 1.0
X2 = (-1+4.8e-09j)
X3 = (-1-4.8e-09j)

i don't know what is wrong ? it is suppose to go to M and X1 , X2 and X3 become real by calculating in M

MAE
  • 72
  • 1
  • 11

1 Answers1

2

Negative number raised to a fractional power either gives a complex number (in Python 3.x) (Even for an odd power like 1/3 or 1/5) or a Value error in Python 2.x (ValueError: negative number cannot be raised to a fractional power).

Example -

>>> (-27)**(1/3)
(1.5000000000000004+2.598076211353316j)

You would need to handle that case yourself, example, if you are doing power of (1/3) , you will have to handle it like -

x = -27
if x < 0:
    pow = -((-x) **(1/3))
elif x > 0:
    pow = x **(1/3)
else:
    pow = 0
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • another problem above – MAE Jul 09 '15 at 07:08
  • Q = (A**2-3*B)/9 R = (2*A**3-9*A*B+27*C)/54 M = R**2-Q**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 when i enter 1 1 -1 -1 M == 0 but it goes to delta and solve X2 = (-1+4.8e-09j) X3 = (-1-4.8e-09j) – MAE Jul 09 '15 at 12:10
  • Again, your `u` and `v` are calculated in the same way , using `((q/2)+sqrt(delta))**(1/3)` , this can cause complex numbers, as i indicated in my answer, you can write a function that does the second code in my answer, and call that function for doing cube root. – Anand S Kumar Jul 09 '15 at 12:18
  • when i enter a = 1 b = 1 c = -1 d = -1 M = 0 my code is if M <= 0 then it calculate three real numbers but when M = 0 it skips M and goes to delta and X2 and X3 become complex – MAE Jul 09 '15 at 12:23
  • Where are you using `M` I cannot see any `M` in the code in your question – Anand S Kumar Jul 09 '15 at 12:28
  • i will edit the question above to show you what i mean – MAE Jul 09 '15 at 12:30
  • When you pass in those values M is not <= 0. M is `1.3877787807814457e-17` , maybe you can use `M = round(M,10)` . It may work. – Anand S Kumar Jul 09 '15 at 13:26
  • Q = 4/9 R = -8/27 – MAE Jul 09 '15 at 20:01
  • sorry? I didn't understand that? – Anand S Kumar Jul 10 '15 at 03:09
  • When a = 1 b = 1 c = -1 d = -1 Q = 4/9 and R = -8/27 then M equals zero – MAE Jul 10 '15 at 10:56
  • not in python, python's floats do not work that way, use what i said , round it to some number of decimal places like 5 or 10 – Anand S Kumar Jul 10 '15 at 10:59