When evaluating on the computer the following function:
$$f(x)=\frac{x^2}{(\cos(\sin(x)))^2-1}$$
there is a big relative error for values $x\approx0$ (values very close to zero). I used the Taylor Series for the above function and I got: $$ -1 - \frac{2}{3}x^2 - \frac{2}{15}x^4 + \frac{1}{945}x^6 - \dotsb $$ and for $x=0.1\mbox{e-}7$ I got the result $-1$ when the result should be different but the $x^2$, $x^4$, $x^6$ and $x^8$ terms are ignored. I suppose it is because of the floating point representation. Why is this happening? What's the explanation? And how can I find a method to compute $f$ for values $|x|<1$ at machine precision?


This happens because you are trying to divide two very small numbers, and the denominator has a lot more error in it. Let $x \to 0$, so then $\sin x \approx x$, and $\cos x \approx 1-x^2/2$, so $$ f(x) \approx \frac{x^2}{(1-\frac{1}{2}x^2)^2-1} = \frac{x^2}{-x^2+\frac{1}{4}x^4}$$ When $x=10^{-8}$, then $x^4$ and $x^2$ differ by $10^{16}$, which is below machine precision for double floating point numbers, so $-x^2+x^4 = -x^2$, resulting in $-1$.
You can evaluate this more robustly by using the Taylor series below a certain value of $|x|$:
You can determine the value of
tolusing the error bound for an alternating series, but it will be more stringent than necessary. Empirically, a value around $10^{-4}$ or $10^{-5}$ seems appropriate, by asking wolframalpha to plot the difference between $f(x)$ and it's truncated Taylor series.In practice, such kinds of functions are numerically evaluated using Chebyshev polynomial approximations or Pade rational polynomial expansions which can achieve very low uniform error with on the order of 10th order polynomials.