# Changing $(1-\cos(x))/x$ to avoid cancellation error for $0$ and $π$?

I have to change this formula: $$\frac{1-\cos(x)}{x}$$ so that I can avoid the cancellation error. I can do this for 0 but not for $π$. So I get: $$\frac{\sin^2(x)}{x(1+\cos(x))}$$ which for $x$ close to $0$ gets rid of the cancellation error. But I don't know how to fix the error for $x$ close to $π$? I just want to know if I should be using trigonometric identities again? I've tried to use trig identities but nothing works. Any suggestions or hints?

Edit: So for π I meant that sin(π) would be 0 so it wouldn't give me the correct value as (1-cosπ)/π=2/π. The second equation would overall give me 0. That's the error I meant for π. Sorry for the confusion there.

-

Another possiblity that avoids cancellation error at both places is $$\frac{2 \sin^2(x/2)}{x}$$

-

The expression $\dfrac{1-\cos x}{x}$ becomes $0/0$ when $x=0$, but not when $x=\pi$. The expression $\dfrac{\sin^2 x}{x(1+\cos x)}$ becomes $0/0$ when $x=\pi$ but not when $x=0$. The two are equal to each other if $x$ is anything between $0$ and $\pi$. So you've got one form that "gets rid of the error" when $x=0$ and another form that "gets rid of the error" when $x=\pi$. The answer to your final question seems to be just to stick with what you started with.

-

If I got you right:

$$\lim_{x \to 0} \frac{1-\cos x}{x}= \lim_{x \to 0} \frac{1-(1-\frac{x^2}{2}+O(x^4))}{x}=0\\ \lim_{x \to \pi} \frac{1-\cos x}{x}=\frac{2}{\pi}$$ The first limit is due to Taylor series expansion around 0, the second is since $\cos \pi=-1$

-