Why do $\frac{(e^{2x}-(e^x\cos x)^2)}{x^2}$ and $\left(\frac{e^x\sin x}{x}\right)^2$ give different numeric results?
This appears to be an instance of catastrophic cancellation.
Your example shows how a numerical computation can be rearranged to avoid it.