# 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.

-

## 3 Answers

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$

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 22, "mathjax_display_tex": 4, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.956348180770874, "perplexity_flag": "head"}