How does a computer calculate a sin value? Logically, when I think about it the only apparent way is to put many sin values into memory, and when a sin value needs to be "calculated" it would just pull data from a specific memory address.(ex. sin(x) would pull data from the memory address containing the value of sin(x) )That seems like the only possible way to do it. Or is there a function which can be used to calculate the sin of a value? I'm really trying to ask how a computer calculates sin on a base level. Is there a way to approximate sin values using a different function composed of more "basic" operations, and the ALU would be able to do multiple "basic" operations to approximate the sin value, or is it just pulling values from memory?
2 Answers
Typically high resolution sin(x) functions would be implemented with a CORDIC (COrdiate Rotation DIgital Computer) algorithm, which can be accomplished with a small number of iterations using only shifts and add/subtract and a small lookup table. The original paper The CORDIC Computing Technique by Jack Volder is from 1959. It also works nicely when implemented with hardware in an FPGA (and a similar algorithm would be implemented in a hardware FPU for those micros that have an FPU).
For lower resolution, for example to create synthesized sine wave for an inverter or motor VFD (Variable Frequency Drive), a lookup table (LUT) with or without interpolation works well. It's only necessary to store the values for one quadrant of the sine wave because of symmetry.
As @Temlib points out, the algorithms used inside modern FPUs use range reduction followed by an evaluation using something like the Remez algorithm to limit the maximum absolute error. More can be found in this Intel paper Formal verification of floating point trigonometric functions.
- 397,265
- 22
- 337
- 893
-
2CORDIC is rather for a pure hardware fixed function conversion (its first historical application). For a computer with a FPU, polynomial approximations are faster and more suitable as they reuse existing arithmetic operators instead of a special CORDIC shift-and-add machine. – Grabul Oct 08 '16 at 16:34
-
1@TEMLIB Yes, that's a valid point, I'll add that to the answer. CORDIC was used in the first scientific calculators too, such as the HP-35. – Spehro Pefhany Oct 08 '16 at 16:57
-
To my knowledge, CORDIC had it's first hardware implementation in the HP 9100A desk calculator. It had a printed circuit card about a foot square, covered with diodes, which served as the ROM storing the parameters used by the CORDIC algorithms. – Hot Licks Oct 09 '16 at 01:50
-
@HotLicks - you would be incorrect, CORDIC was developed for and used in an in-flight navigation computer nearly ten years before the HP 9100A. – Chris Stratton Oct 09 '16 at 03:58
-
-
Also worth pointing out that some processors have hardware trig. implementations. I only know this because someone found an error in one of the processors a while ago. Secondly, a good resource is to look at the standard C libraries for the implementation. If I remember correctly, completely different algorithms were used depending on the angle size. They were also extremely hardware dependent, which is something you don't often see nowadays with all the layers of abstraction. – user110971 Oct 09 '16 at 15:18
Most computer trig libraries are based on polynomial approximations, which gives the best balance between speed an accuracy. For example, a dozen or so multiplication and add/subtract operations is enough to give full single-precision accuracy for sine and cosine.
- 172,781
- 17
- 234
- 402
<placeholder>, google "<placeholder>calculation algorithm". It works better than asking on SE in most cases.. – Eugene Sh. Oct 07 '16 at 22:50