0

negative number within a root

I'm doing a code that can calculate the zeros of a polynomial of second degree but when there is a solution where the root is negative I throw error and I know that it can be solved with a "try" but I do not know exactly how This is my code import math as m def cuadratica(a,b,c): r1 = (-b+m.sqrt(b**2-(4*a*c)))/(2*a) r2 = (-b-m.sqrt(b**2-(4*a*c)))/(2*a) r3=-b/2*a if b**2 > 4*a*c: print("la primera solucion es "+str(r1)+"la segunda es"+str(r2)) if b**2 == 4*a*c: print("la unica solucion es "+str(r3)) if not b**2 > 4*a*c and not b**2 > 4*a*c: print("no hay solucion") cuadratica(6,6,2)

12th Dec 2018, 3:53 PM
Sergio D. Medina
Sergio D. Medina - avatar
2 Antworten
+ 7
You can try using cmath module import cmath print(cmath.sqrt(-4)) #2j
12th Dec 2018, 5:32 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 1
Hai mate
13th Dec 2018, 10:29 PM
ABHIJIT SEN
ABHIJIT SEN - avatar