How do calculators compute sine?
Introduction
Sine, one of many elementary trigonometric capabilities, performs an important position in varied fields, together with arithmetic, physics, engineering, and laptop science. Its calculation will not be trivial, particularly in terms of implementing it in digital calculators, the place effectivity and accuracy are paramount.
In earlier entries of the collection, we regarded into how calculators solve equations and how they calculate square roots. On this weblog publish, we’ll delve into the intricate means of calculating the sine perform, ranging from easy approximations to extra refined strategies.
How sine is calculated
To start, let’s examine the plot of the sine perform:
It’s instantly apparent, that the perform is periodic, and has a robust symmetry of the interval between 0 and $pi/2$:
In different phrases, it is sufficient to calculate the perform on the interval $[0;pi/2]$. Then, we will simply use flipping and negation to get the ultimate worth. To calculate $sin(x)$ inside the lowered interval, one choice is to make use of the well-known Taylor series approximation:
$$sin(x) = x-frac{x^3}{3!}+frac{x^5}{5!}-ldots$$Visualized:
Whereas this methodology is straightforward, we have to calculate very excessive exponentials, and the approximation errors can get fairly massive round $pi/2$. For instance, with a nine-degree approximation, the outcome at $ pi/2 $ can be 1.00000354258428. That is an error of 3e-6, which is kind of dangerous, since most calculations are finished to fifteen digits precision. In different phrases, that’s a lack of precision round 10 digits!
How sine is admittedly calculated
Whereas the tactic offered within the earlier paragraph is kind of dangerous, it does function a blueprint for higher strategies. Primarily, each implementation of sine makes use of the next three steps:
- Discount: Utilizing some algebraic methods, scale back $x$ to a small quantity $r$.
- Approximation: Calculate the worth of $sin(r)$ utilizing an approximation methodology, such because the Taylor collection.
- Reconstruction: Calculate the ultimate worth of $sin(x)$ primarily based on $sin(r)$.
There are lots of methods to strategy this downside. Within the following , I current what Intel makes use of of their processors, primarily based on their paper. They begin with the formulation
$$r = x-Nfrac{pi}{16}.$$ Right here, $N$ is the integer chosen such that $|r|$ is minimized. In different phrases, we approximate $x$ by $Ncdotfrac{pi}{16}$, and $r$ is the approximation error. How can we use this? Utilizing the identities of the sum of arguments:
$$displaylines{sin(x)=sinleft(r+Nfrac{pi}{16}proper)=sinleft(Nfrac{pi}{16}proper)cos(r)+cosleft(Nfrac{pi}{16}proper)sin(r)==sinleft(frac{N}{32}2piright)cos(r)+cosleft(frac{N}{32}2piright)sin(r)}$$
We now have to calculate $sin(r)$ and $cos(r)$ – that is the approximation step, extra on this later. For now, assume we all know each $sin(r)$ and $cos(r)$. We nonetheless have to search out the sine and cosine of $frac{N}{32}2pi$. Discover, that each $sin$ and $cos$ are periodic by $2pi$, so we solely actually must calculate $N$ for $N=0,1,2,ldots,31$. These are 32 values in whole, we will simply precompute them. Through the calculation of the ultimate worth, we simply must lookup them up from an inventory, which is kind of environment friendly.
Just one piece of the puzzle stays: how one can calculate $sin(r)$? Within the paper, Intel doesn’t publish the precise polynomial however they do point out that they use minimax approximation. This approximation finds a polynomial that minimizes the utmost error over an interval:
$$max_{0le r<frac{pi}{16}}|p(r)-sin(r)|,$$ the place $p$ is the approximating polynomial. One strategy to calculate $p$ is Remez’s algorithm. The outcomes would possibly appear to be this:$$x -0.166667x^3 + 0.00833x^5 -0.00019x^7 + 2.6019cdot10^{-6}x^9.$$The utmost error of this polynomial is $4.1cdot10^{-9}$, which is a thousand occasions higher than the unique Taylor-approximation!
Conclusion
In conclusion, calculating sine in computer systems entails a mix of discount, approximation, and reconstruction steps. From easy discount and Taylor collection to extra exact strategies like minimax approximation, computer systems make use of varied methods to compute sine effectively whereas sustaining acceptable ranges of accuracy. Understanding these strategies sheds gentle on the underlying arithmetic that energy computational instruments and simulations in quite a few fields.