All problems
Taylor Truncation Error Value
Problem
For f(x) = e^x, the actual truncation error of the degree-n Taylor polynomial about 0 is E = e^x - sum from k = 0 to n of x^k / k!.
Given x and n, accumulate the polynomial terms and return the difference. The result is negative for negative x once the leading omitted term dominates.
Hint
Build the partial sum term by term to avoid recomputing factorials.
Test cases
case 1
in: [1,5]
expected: 0.0016151617923787498
case 2
in: [0,10]
expected: 0
case 3
in: [2,3]
expected: 1.0557227655973174
case 4
in: [-1,4]
expected: -0.0071205588285577215
Starter codePython
def taylor_truncation_error(x, n):
# Your code here
pass4 test cases · run your code against them in the editor