from math import exp, factorial

x = -40.0
nmax = 120

print("Error due to information buried: exp(-x) for large x")
print()

exact = exp(x)
print("exact: exp(", x, ") =", exact)

print("{:^3}".format("n"), ":\t", "{:^18}".format("sum(x^n/n!)"), "\t", "{:^18}".format("1 / sum((-x)^n/n!)"))

s1 = 0.0
s2 = 0.0

n = 0
while n < nmax:
    nfact = factorial(n)
    s1 = s1 + (x ** n) / nfact
    s2 = s2 + ((-x) ** n) / nfact
    print("{:^3d}".format(n), ":\t",
          "{:>18.8g}".format(s1), "\t",
          "{:>18.8g}".format(1.0 / s2))
    n = n + 1
