Quadratic equations

Write and save the following functions into the same file named quadratic.py.

  1. real_roots() that takes three parameters a, b and c, corresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\) with two different real roots, and returns its roots.

    See the following examples:

    >>> a, b, c = 1, -4, 3
    >>> x1, x2 = real_roots(a, b, c)
    >>> a*x1**2+b*x1+c == 0
    True
    

    Note

    More tests are provided in the quadratic1.txt file.

  2. has_real_roots() that takes three parameters a, b and c, corresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\), and retuns True if the quadratic equation has real roots and False otherwise.

    See the following examples:

    >>> a, b, c = 1, -4, 3
    >>> r = has_real_roots(a, b, c)
    >>> r
    True
    

    Note

    More tests are provided in the quadratic2.txt file.

  3. is_a_root() that takes five parameters a, b, c corresponding to the coefficients of a quadratic equation \(y=ax^2+bx+c\), x corresponding to a candidate root x, and eps corresponding to a tolerance. This function returns True if x is a root of the quadratic equation taking into account the given tolerance, i. e., when abs(y) <= eps, and False otherwise.

    See the following examples:

    (x+1)(x-2/3) = 3x²+1x-2
    
    >>> a, b, c = 3, 1, -2
    >>> is_a_root(a, b, c, -1, 0)
    True
    
    >>> is_a_root(a, b, c, 0.66, 0.00001)
    False
    
    >>> is_a_root(a, b, c, 0.6666, 0.01)
    True
    
    >>> is_a_root(a, b, c, 1, 0)
    False
    

    Note

    More tests are provided in the quadratic3.txt file.

Solutions

A solution of these functions is provided in the quadratic.py file.