Maybe you think you want to do this, but you don't really want to do this. New Pythoners usually think they need to round floating point numbers because when evaluated they get unexpected results (like 1.0/10 = 0.100000000000001). Rather than do some goofy string substitution on your expression, I just created a variable for round(49/200,n)
, and did a little format cleanup. Also exp(49/200)
does not need to be evaluated 13 times, just do it once and refer to the computed value.
zz = round(49/200,n)
e_zz = exp(zz)
ans = (e_zz +
e_zz * (x-zz) +
1/2 * e_zz * (x-zz)**2 +
1/6 * e_zz * (x-zz)**3 +
1/24 * e_zz * (x-zz)**4 +
1/120 * e_zz * (x-zz)**5 +
1/720 * e_zz * (x-zz)**6 +
1/5040 * e_zz * (x-zz)**7 +
1/40320 * e_zz * (x-zz)**8 +
1/362880 * e_zz * (x-zz)**9 +
1/3628800 * e_zz * (x-zz)**10 +
1/39916800 * e_zz * (x-zz)**11)
Raising e to a rounded number is almost never appropriate. Likewise for raising a rounded number to the 11'th power. (Note also that in Python, the exponentiation operator is **
, not ^
.)
Edited:
If S.Lott hadn't suggested the algebraic simplification, I would have left this as-is. But the * e_zz
can be factored out of every term, giving the simpler (and probably faster):
zz = round(49/200,n)
e_zz = exp(zz)
ans = e_zz * (1 +
(x-zz) +
1/2 * (x-zz)**2 +
1/6 * (x-zz)**3 +
1/24 * (x-zz)**4 +
1/120 * (x-zz)**5 +
1/720 * (x-zz)**6 +
1/5040 * (x-zz)**7 +
1/40320 * (x-zz)**8 +
1/362880 * (x-zz)**9 +
1/3628800 * (x-zz)**10 +
1/39916800 * (x-zz)**11)