For the sake of practice, I am trying to build a simple function that solves quadratic roots - including complex roots and returns a nice and rounded result. However, neither the round() function or f-string formatting seems to work.
Here is the code I wrote:
from cmath import sqrt
a = float(input("Input ax^2: "))
b = float(input("Input bx: "))
c = float(input("Input c: "))
def quadratic_roots(a,b,c):
x_1 = (-b - sqrt((b ** 2) - (4 * a * c))) / (2 * a)
x_2 = (-b sqrt((b ** 2) - (4 * a * c))) / (2 * a)
return x_1,x_2
print(f"--- {a}x^2 {b}x {c} = 0 ----")
#Attempting to use round() fuction.
print(f"x_1 = {round(quadratic_roots(a,b,c)[0],2)}") #Produces error
print(f"x_2 = {round(quadratic_roots(a,b,c)[1],2)}") #Produces error
#Attempting to use f-string formatting .to round up to three digits.
print(f"x_1 = {quadratic_roots(a,b,c)[0]:.3d}") #Produces error
print(f"x_2 = {quadratic_roots(a,b,c)[1]:.3d}") #Produces error
And here is the output/error message I'm getting:
print(f"x_1 = {round(quadratic_roots(a,b,c)[0],2)}")
TypeError: type complex doesn't define __round__ method
Is there anyone out there who can help me sort this out and return the result as a nice and rounded result?
Any help is welcomed and appreciated :-)
CodePudding user response:
The issue was with round. I fix the issue and hope it will work now.
Code:
from cmath import sqrt
a = float(input("Input ax^2: "))
b = float(input("Input bx: "))
c = float(input("Input c: "))
def quadratic_roots(a,b,c):
x_1 = (-b - sqrt((b ** 2) - (4 * a * c))) / (2 * a)
x_2 = (-b sqrt((b ** 2) - (4 * a * c))) / (2 * a)
return x_1, x_2
print(f"--- {a}x^2 {b}x {c} = 0 ----")
x_1, x_2 = quadratic_roots(a,b,c)
#Attempting to use round()
print(f"x_1 = {round(x_1.real, 3) round(x_1.imag, 3) * 1j}")
print(f"x_2 = {round(x_2.real, 3) round(x_2.imag, 3) * 1j}")
#Attempting to use f-string formatting .to round up to three digits.
print(f"x_1 = {x_1:.3f}")
print(f"x_2 = {x_2:.3f}")
Input:
Input ax^2: 1
Input bx: 1
Input c: 1
Output:
--- 1.0x^2 1.0x 1.0 = 0 ----
x_1 = (-0.5-0.866j)
x_2 = (-0.5 0.866j)
x_1 = -0.500-0.866j
x_2 = -0.500 0.866j
CodePudding user response:
You can round the real and imaginary parts separately and then join them
print(f"x_1 = {round(quadratic_roots(a,b,c[0].real,2)} {round(quadratic_roots(a,b,c)[0].imag,2)*1j}")
print(f"x_2 = {round(quadratic_roots(a,b,c)[1].real,2)} {round(quadratic_roots(a,b,c)[1].imag,2)*1j}")
CodePudding user response:
After some help from the comments, this problem was fixed this by using
print(f"x_1 = {quadratic_roots(a,b,c)[0]:.3f}")
print(f"x_2 = {quadratic_roots(a,b,c)[1]:.3f}")
The problem was that I used the wrong f-string formatting:
I tried to use f"{:.d}"
, which didn't work. Replacing this with f"{:.f}"
worked just fine.
However, as suggested in the comments, there are better ways of solving this problem.
A massive thank you to everyone who helped out and contributed!
CodePudding user response:
Complex numbers have a real part and an imaginary part.
Try:
x_1 = quadratic_roots(a, b, c)[0].real quadratic_roots(a, b, c)[0].imag
x_2 = quadratic_roots(a, b, c)[1].real quadratic_roots(a, b, c)[1].imag
print(f"x_1 = {round(x_1,2)}")
print(f"x_2 = {round(x_2,2)}")