import math


def table2_6():
    #  Solve  f(x) = 0  using Newton

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

    # Solution of  exp(-x)-(2+x) = 0  to 20 digits
    # xe=-0.4428544010023885831;

    x = 0.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) / df(x)
        x = x - f(x) / df(x)
        err = abs(z / x)
        print(f" {k:d} \t  {x:19.15f} \t  {err:5.1e}")
        # print(f'\\, {k:d} \\, & \\,\\, {x:19.15f} \\,\\, & \\,\\, {err:5.1e}  \\\\\\hline')
    print("\n")


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


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


if __name__ == "__main__":
    table2_6()
