How to compute $\cos(x)$ within $n$ digit accuracy when $x = \sqrt{y}$ with $y \in \mathbb{N}$

34 Views Asked by At

How does one compute $\cos(x)$ within desired $n$ digit accuracy when $x = \sqrt{y}$ with $y \in \mathbb{N}$ and $x$ is not rational?

The reason I am asking this question is that calculators definitely encounter these problems and there should be some method handling this..

1

There are 1 best solutions below

3
On

Considering that $y$ could be any positive number and that you want to compute $\cos(\sqrt y)$, you need to subtract $2k\pi$ from $\sqrt y$ before doing anything. This means that you search for the minimum positive or negative $a$ such that $$a+k\pi=\sqrt y$$ So, now, you need to compute $$\cos(a+k\pi)=(-1)^k \cos(a)$$ At this point, you can use Taylor expansion $$\cos(a)=\sum_{n=0}^\infty \frac{(-1)^n}{(2n)!}a^{2n}$$ and since it is an alternating series stop the calculations at $n=p$ such that $$\frac{a^{2p}}{(2p)!} \leq \epsilon$$

Let us try with $y=1234$; we find $k=11$ and $$a=\sqrt{1234}-11 \pi\approx 0.570816951013$$ and, for $\epsilon=10^{-15}$, $7$ terms should be required.

Now consider $$S_p=\sum_{n=0}^p \frac{(-1)^n}{(2n)!}a^{2n}$$ We shall obtain the sequence $$S_1=0.837084004218188$$ $$S_2=0.841507607831784$$ $$S_3=0.841459562779274$$ $$S_4=0.841459842325973$$ $$S_5=0.841459841313915$$ $$S_6=0.841459841316413$$ $$S_7=0.841459841316408$$ which is the solution for $15$ significant figures.

Another solution would be to first find an angle $b$ for which we already know the values of the sine and cosine and writing $$\cos(a)=\cos((a-b)+b)=\cos(b+\Delta)$$ build the Taylor expansion at $b$ to get $$\cos(b+\Delta)=\cos(b) \sum_{n=0}^\infty \frac{(-1)^n}{(2n)!}\Delta^{2n}-\sin(b)\sum_{n=0}^\infty \frac{(-1)^n}{(2n+1)!}\Delta^{2n+1}$$ In the case treated above $b=\frac \pi 6$ so $\Delta\approx 0.0472181754146$ and the sequence will be $$T_1=0.841459662935040$$ $$T_2=0.841459841329687$$ $$T_3=0.841459841316408$$