Function that solves quadratic equations

e-papa 0 Tallied Votes 4K Views Share

It solves quadratic equations, for both real and complex roots.
Please answer the pol to let me know how I'm doing.
This function just needs the python 3.x environment, no modules needed.

"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
"""this functions solves Quadratic equations
    using the quadratic formula"""
from math import*
def quad(a,b,c):
    """solves quadratic equations of the form
        aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)"""
    root=b**2-4*a*c
    if root <0:
        root=abs(complex(root))
        j=complex(0,1)
        x1=(-b+j+sqrt(root))/2*a
        x2=(-b-j+sqrt(root))/2*a
        return x1,x2
    else:
        x1=(-b+sqrt(root))/2*a
        x2=(-b-sqrt(root))/2*a
        return x1,x2

#example
#print(quad(1,3,4))
#>>>
#((-0.17712434446770464+0.5j), (-0.17712434446770464-0.5j))
#print(quad(1,9,4))
#>>>
#(-0.46887112585072543, -8.5311288741492746)
e-papa 13 Posting Pro in Training

Please i need the plaudits in the house to scrutinize this snippet.

TrustyTony 888 pyMod Team Colleague Featured Poster

Your function fails, please test your code before sending as code snippet (ask advice in normal thread first, when everything is tested, post it as snippet):

from __future__ import print_function, division
"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
"""this functions solves Quadratic equations
    using the quadratic formula"""
from cmath import *
delta = 1e-9

def quad(a, b, c):
    """ solves quadratic equations 

        form aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)

    """
    discriminant = (b ** 2) -  4  * a * c
    print('Discriminant', discriminant) # debug
    if discriminant > 0:
        return (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a)
    elif discriminant == 0:
        return (-b +  sqrt(discriminant)) / (2 * a)
    else:
        return (-b +  sqrt(discriminant))  / (2 * a) , (-b -  sqrt(discriminant))  / (2 * a) 

def quad_epapa(a,b,c):
    """solves quadratic equations of the form
        aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)"""
    root=b**2-4*a*c
    if root <0:
        root=abs(complex(root))
        j=complex(0,1)
        x1=(-b+j+sqrt(root))/2*a
        x2=(-b-j+sqrt(root))/2*a
        return x1,x2
    else:
        x1=(-b+sqrt(root))/2*a
        x2=(-b-sqrt(root))/2*a
        return x1,x2
#example
for vals in (2, 4, -30), (1,3,4), (1,9,4):
    for test in quad, quad_epapa:
        print(40 * '-')
        print('Testing function %r' % test.__name__)
        solutions = test(*vals)
        for solution in solutions:
            a,b,c = vals
            print(vals, 'solution', solution)
            print('%s < %f' % ((a * (solution ** 2) + b * solution + c), delta))
            assert abs(a * (solution ** 2) + b * solution + c) < delta

This is one rule that I am still not yet internalized from the formatting of the doc strings:

- PEP 257 describes good docstring conventions. Note that most
importantly, the """ that ends a multiline docstring should be on a line
by itself, and preferably preceded by a blank line, e.g.:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

- For one liner docstrings, it's okay to keep the closing """ on the same
line.

e-papa 13 Posting Pro in Training

I'm very sorry, will debug and do as you said.

e-papa 13 Posting Pro in Training

Your function fails, please test your code before sending as code snippet (ask advice in normal thread first, when everything is tested, post it as snippet):

from __future__ import print_function, division
"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
"""this functions solves Quadratic equations
    using the quadratic formula"""
from cmath import *
delta = 1e-9

def quad(a, b, c):
    """ solves quadratic equations 

        form aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)

    """
    discriminant = (b ** 2) -  4  * a * c
    print('Discriminant', discriminant) # debug
    if discriminant > 0:
        return (-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a)
    elif discriminant == 0:
        return (-b +  sqrt(discriminant)) / (2 * a)
    else:
        return (-b +  sqrt(discriminant))  / (2 * a) , (-b -  sqrt(discriminant))  / (2 * a) 

def quad_epapa(a,b,c):
    """solves quadratic equations of the form
        aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)"""
    root=b**2-4*a*c
    if root <0:
        root=abs(complex(root))
        j=complex(0,1)
        x1=(-b+j+sqrt(root))/2*a
        x2=(-b-j+sqrt(root))/2*a
        return x1,x2
    else:
        x1=(-b+sqrt(root))/2*a
        x2=(-b-sqrt(root))/2*a
        return x1,x2
#example
for vals in (2, 4, -30), (1,3,4), (1,9,4):
    for test in quad, quad_epapa:
        print(40 * '-')
        print('Testing function %r' % test.__name__)
        solutions = test(*vals)
        for solution in solutions:
            a,b,c = vals
            print(vals, 'solution', solution)
            print('%s < %f' % ((a * (solution ** 2) + b * solution + c), delta))
            assert abs(a * (solution ** 2) + b * solution + c) < delta

This is one rule that I am still not yet internalized from the formatting of the doc strings:

Thanks for seeing the flaws in my program, actually you really helped me because your post in my topic

What does math domain error mean

really helped, here is the quadratic equation, back and better. Yet again i will like you to also debug and test with various values, I believe there is more. This is the new function using the knowledge of cmath module in python.

"""ADEGOKE OBASA, adegokeobasa@yahoo.com"""
"""this functions solves Quadratic equations
    using the quadratic formula"""
import math
import cmath
def quad(a,b,c):
    """solves quadratic equations of the form
        aX^2+bX+c, inputs a,b,c,
        works for all roots(real or complex)"""
    D=b**2-4*a*c
    if D <0:
        x1=(-b+cmath.sqrt(D))/2*a
        x2=(-b-cmath.sqrt(D))/2*a
        return x1,x2
    else:
        x1=(-b+math.sqrt(D))/2*a
        x2=(-b-math.sqrt(D))/2*a
        return x1,x2

#example
#print(quad(1,0,9))
#>>>
#(3j, -3j)
#print(quad(1,0,4))
#>>>
#(2.0, -2.0)

Thanks.

TrustyTony 888 pyMod Team Colleague Featured Poster
>>> 1./2*2
1.0
>>> 1./(2*2)
0.25

You can borrow the test cases from my alternative Quadratic Equation solver I posted today as example for posting code snippets.
I recommend to do install improved Python module installer

easy_install pip

and install program pep8 for checking correct form of Python program. Very educating and helps to learn good habits.

e-papa commented: Thanks again +1
e-papa 13 Posting Pro in Training

Thanks again how do i go about installing PEP8, please post me the directions I will be glad to do so.
Thanks for the tips.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.