import math


def secant():
    #  Solve  f(x) = 0  using secant method

    #  Input:
    # x = starting point
    # tol = tolerance for stopping
    # f(x) is at end of file

    x = 0.0
    X = -2.0
    tol = 10 ** (-10)

    k = 0
    err = 10 * tol
    print("\n RIE = relative iterative error \n")
    print(" k  \t   x_k \t\t\t  RIE ")
    while err > tol:
        k = k + 1
        z = f(x) * (x - X) / (f(x) - f(X))
        X = x
        x = x - z
        err = abs(z / x)
        print(f" {k:d} \t  {x:19.12e} \t  {err:5.1e}  ")
    print("\n")


def f(x):
    # g=x^3+2*x+2;
    g = math.exp(-x) - (2 + x)
    return g


if __name__ == "__main__":
    secant()
