All products featured on WIRED are independently selected by our editors. However, we may receive compensation from retailers and/or from purchases of products through these links.
This all started with a student my wife was helping with high school trigonometry homework. Here is one of the questions.
Here is the solution that the homework was looking for:
This uses the trigonometry sum of angles formula and breaks the cosine into a function of sines and cosine with angles that we know the answer for (45° and 30°).
The student was really confused on this problem. She asked a great question: "why can't I just put cos(75°) into my calculator? What is the point of this question?" She's right. What is the point? Yes, there is indeed two points to this question.
- This question is like lifting weights in the gym. If the point is to raise some weights, there are better ways to do it. However, by actually lifting weights you get stronger. We call this exercise. Using the sum of angles formula to evaluate the cosine is a mathematical exercise.
- It wasn't all that long ago that this is what you had to do to evaluate trigonometric functions. Really, 30 or so years ago calculators didn't have trig functions on them. I think this is really amazing.
This brings us (me) to the main point. How does a calculator evaluate the cosine function? Answer: I'm not sure. I can think of two ways that the calculator might evaluate these functions. Now, I will do an experiment to see if I can figure out how this "black box" called a calculator works.
Trig Method 1: Trig Tables
This is the old way of calculating trig functions. You just look it up in a table. I have a late 80's version of the CRC Math Tables book. It has a few pages with different angles and the trig functions evaluated at these angles.
Here is a sample of trig table.
This works nicely if you need to get the cosine of 0.1774 radians or 0.1804 radians. But what if your angle is somewhere in between these two? Let's say you need the cosine of 0.1792 radians. In this case, you need to use interpolation. The most basic form of interpolation is linear interpolation. This assumes there is a linear function between known data points. Let's say I have some function f(x) and there is a table with values at x1 and x2. How would I find the value of the function at some point in between these two x values? Basically, f(x) would be between the two known values by the same amount that x is in between x1 and x2. So, that would look like this:
You take the function at the lower value of x that you know. Then you increase that function by the ratio of the x value to the change in the function value. I know the formula looks complicated, but it's pretty straight forward. If I want to find the cosine of 0.1792 radians from the table above, I would get 0.984 - try it yourself.
But what does this have to do with a calculator? I think this is one way a calculator could work. It could have a trig table in memory and just interpolate the answer. I will check in a little bit.
Trig Method 2: Series Expansion
What if you were able to expand the cosine function as an infinite series? This wouldn't help you evaluate the function if you tried to evaluate an infinite series (since it would take forever). However, maybe the first 5 terms give you a good enough answer. Just maybe.
How do you expand a function? My go-to expansion is the Taylor Series. In short, the Taylor series allows you to write a function by looking at its value at some known location and then looking at the derivatives of this function. For any generic function f(x), the expansion would look like this as expanded around some constant a.
With this, I can take cosine and approximate it with a polynomial. I will skip all the details, but this is how I can write the cosine function expanded about x = 0.
Maybe it would be better to write this as:
I guess I never said what the "!" means. That is the factorial. As an example, 4! = 4*3*2*1. This actually is a problem with a large term expansion. The factorial really starts to blow up for large n.
Now for a quick test. Warning: this contains some cheating. I am going to calculate the cosine of π/4 since I know the answer to this is 1 over the square root of 2. And here is the first cheat. How do you calculate the square root of two? I will just assume whatever value the computer gives me will be close enough to exact that it won't matter.
Here is a plot of the Taylor expansion of cosine as a function of n.
Here you can see after just the third term, the series gets very close to the correct value. This isn't good enough though. I want to see this error in more detail. How about a plot of the absolute value of the error as a log plot. Yes. That might look nice.
At N = 7 in the series, the error has dropped down to about 10-15. That's pretty small. I think this series works well enough. I will probably be fine with just 5 terms (even that is more than I need).^^
Testing a Calculator
Now for the values from a calculator. I wanted to find an older calculator - and this what I came up with.
Yup. A nice solar powered TI-31. How do you like that?
Now I just need to collect some data. Here are some random angles between 0 and π/4 (that's as high as my trig table goes). For each value, I am going to evaluate the cosine by the following methods:
- Interpolation and the trig tables.
- Series expansion (7 terms - if I go with 7 my homemade function gives the same value as python).
- TI-31 plug and chug.
My first idea was to plot the cosine as computed by the series expansion and the table interpolation as a function of the cosine from the TI-31. Here is what that would look like.
You can see that the plot isn't very useful. Really, the only point that stands out as different is the the smallest cosine value. It turns out this was a user error in the tables.
Let me make a different plot. If I assume that the 7 sum series calculation is the correct value for the cosine, then I can plot the difference for these other methods.
What does this say? It says that even for the table, the error is pretty small (I had to multiply by 108 just so you could see the differences). Also, I think I can already say that the TI-31 does not use THIS table. Maybe there is a better table, but I have a feeling that the calculator uses the first few terms in something like the Taylor expansion. Also, I have only expanded about the point x = 0. However, you could do an expansion around other known values of cosine - like π/4.
Maybe I have a better way of looking at this. What if I plot a histogram of the differences between cosines calculated by different methods? This won't work since there is a huge variation in differences. Instead, I will plot the natural log of the differences. Oh, one more cheat. A couple of these methods had zero differences a couple of times. I had to put in a non-zero value for these instances in order to take the natural log. Sorry about that. Here's what I get though.
Here you can see that the N=3 series doesn't give a great value for cosine. It's not worthless, but it's not great either. You would be better off using the CRC tables. However, if you bump this up to N=5, it's way better. But look at the values for the TI-31. They are mostly better than N=5 Taylor series. Could the calculator be doing N=6? No. I get different values using the N=6 series - but it's close.
If I had to guess, (and apparently I do) I would say that this TI-31 is either using a Taylor series and rounding off a little bit (I would guess N=6 and rounding) or some other series expansion. Actually, I am changing my answer. If you print out the values for cosine for the different methods, I get the same thing for N=4 and for the TI-31 values. For some reason, when python subtracts these numbers I get something crazy small (even though they show the same thing). I probably need to use the longer decimal format in python.