Python Forum
Compute complex solutions in quadratic equations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compute complex solutions in quadratic equations
#1
Hi, my homework is to solve quadratic equations and compute complex solutions(the solutions to the quadratic equations are not real numbers). Furthermore, we want to limit ourselves to only 2 decimal places of precision. As such, i am tasked to use the function round(value, dp).
My task is to write two functions solve_qe_small(a, b, c) and solve_qe_large(a, b, c).

however, my following code presented a type error: "type complex doesn't define __round__ method"
I'm not sure how I can compute both complex numbers and at the same time round off the answers to 2 decimal places.

from math import *
from cmath import *
def solve_qe_small(a, b, c):
  d = (b**2) - (4*a*c)
  x1 = (-b - sqrt(d))/(2*a)
  answer1 = str(round(x1,2))
  return answer1

def solve_qe_large(a, b, c):
  d = (b**2) - (4*a*c)
  x2 = (-b + sqrt(d))/(2*a)
  answer2 = str(round(x2, 2))
  return answer2

print(solve_qe_small(1, 1, 8))
print(solve_qe_large(1, 1, 8))
Reply
#2
You could perhaps define your own round
def cround(z, ndigits=0):
    z = complex(z)
    return complex(round(z.real, ndigits), round(z.imag, ndigits))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Maths and python: Compute stress level cheerful 1 3,401 Oct-20-2021, 10:05 AM
Last Post: Larz60+
  How to compute conditional unigram probabilities? jbond 2 3,299 Jan-25-2020, 02:58 PM
Last Post: jbond
  To extract a specific column from csv file and compute the average vicson 2 9,216 Oct-20-2018, 03:18 AM
Last Post: vicson
  Write a program to compute the sum of the terms of the series: 4 - 8 + 12 - 16 + 20 - chewey777 0 3,325 Mar-24-2018, 12:39 AM
Last Post: chewey777
  How do you compute tf-idf from a list without using the counter class syntaxkiller 8 6,793 Dec-01-2017, 05:24 PM
Last Post: nilamo
  compute gross pay jamesuzo 1 11,152 Sep-07-2017, 01:47 PM
Last Post: ichabod801
  More Errors. Less Solutions. SyntaxError123 7 6,321 Mar-30-2017, 07:55 PM
Last Post: micseydel
  Solutions to this questions jerrykatoh 8 192,438 Feb-23-2017, 11:59 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020