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

Download script from .\information_buried.py


from math import exp, factorial

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

#===================
# parameters
#===================
x = -40.0
nmax = 120

#===================
# main routine
#===================
print("Error due to information buried: exp(-x) for large x")
print("")

exact = exp(x)
print("exact: exp({}) = {}".format(x, exact))

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

s1 = 0.0
s2 = 0.0
for n in range(nmax):
    nfact = factorial(n)
    s1 += pow(x, n) / nfact
    s2 += pow(-x, n) / nfact
    print("{:^3d}:\t{:>18.8g}\t{:>18.8g}\t".format(n, s1, 1.0 / s2))