Module with a function to approximate e ** x. def exp(x,err=1e-6): Returns
Question:
Module with a function to approximate e ** x.
def exp(x,err=1e-6):
"""
Returns the value of (e ** x) to within the given margin of error.
Do NOT return (math.E ** x). This function is more precise than that answer.
The value (e ** x) is given by the Power Series
1 + x + (x ** 2)/2 + (x ** 3)/3! + ... + (x ** n)/ n! + ...
We cannot add up infinite values in a program. So we APPROXIMATE (e ** x) by choosing a value n and stopping at that:
1 + x + (x ** 2)/2 + (x ** 3)/3! + ... + (x ** n)/ n!
The error of this approximation is
abs( (x ** (n+1))/(n+1)!
which we want less than err. So to compute e ** x, we just keep computing the term = (x ** n)/ n! in a loop until this value is less than our error. If it
is not less than the error, we add it to the accumulator, which we return at the end.
Hint: (x**(n+1))/(n+1)! == (x**n)/n! * x/(n+1)
Use this fact to simplify your loop.
Parameter x: the exponent for e ** x
Precondition: x is a number
Parameter err: The margin of error (OPTIONAL: default is e-6)
Precondition: err > 0 is a number
"""
pass
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young