Question: Module with a function to approximate e ** x. def exp(x,err=1e-6): Returns the value of (e ** x) to within

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

Step by Step Solution

3.32 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

def expx err1e6 Returns the value of e x to within the given margin of ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!