Explicitly calling factorial
and computing powers of x
separately for each term in a Taylor series is not a good approach. Not only is this very slow (you have a lot of redundant computations!), but it can also overflow to 0*Inf
if you compute enough terms.
See, for example, the new implementation of the cosc
function in Base for an example of how to compute a Taylor series iteratively (to arbitrary precision), with specialized cases (evalpoly
with hardcoded coefficients to a fixed order) for single and double precision.