Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Wednesday, May 8, 2013

Quadratic Equation


Problem: Compute the values of x in the given quadratic equations ax^2+ bx + c for given a, b, c.


Solution in Python:



from __future__ import division

import math

def solve(a, b, c, eps=1e-12):
    """
    Author: Mayur P Srivastava
    """

    if abs(a) < eps:
        return None, None

    discriminant = b * b - 4 * a * c
    if discriminant < 0:
        return None, None

    return (-b + math.sqrt(discriminant)) / (2*a), (-b - math.sqrt(discriminant)) / (2*a)



Concepts Learned: Numbers.

No comments:

Post a Comment