
# expRound.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
Code that shows how an estimation of e^{-x} using a 
Maclaurin series may never get close to the true value, 
no matter how many terms are included, due to 
round-off errors. 
'''

import math

def expApprox(x, n):
  exp = 0
  for i in range(n+1):
    exp += (x**i)/math.factorial(i)
  return exp


x = -10
print(f"x == {x}")
print(f"Math e^{x} == {math.exp(x):.5e}")
n = 10
print(f"Using {n} terms, approx == {expApprox(x,n):.5e}")
n = 200
print(f"Using {n} terms, approx == {expApprox(x,n):.5e}")

x = -30
print(f"\nx == {x}")
print(f"Math e^{x} == {math.exp(x):.5e}")
print(f"Using {n} terms, approx == {expApprox(x,n):.5e}")
n = 1000
print(f"Using {n} terms, approx == {expApprox(x,n):.5e}")

