28
\$\begingroup\$

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?

\$\endgroup\$

closed as too broad by Eugene Sh., Chris Stratton, pipe, PlasmaHH, Daniel Grillo Oct 10 '16 at 14:03

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 10
    \$\begingroup\$ Google "CORDIC". Google "Taylor series". And your thoughts about memory are not clear. \$\endgroup\$ – Eugene Sh. Oct 7 '16 at 22:42
  • 8
    \$\begingroup\$ The "memory" method is an optimization technique, frequently used (the name is "lookup table"). But no, initially the computer doesn't have it. But if you are ever wandering about how computer calculates <placeholder>, google "<placeholder> calculation algorithm". It works better than asking on SE in most cases.. \$\endgroup\$ – Eugene Sh. Oct 7 '16 at 22:50
  • 5
    \$\begingroup\$ In addition to CORDIC and Taylors', make sure you also look for Chebyshev coupled with non-linear minimax techniques. Taylor bounds average error, while Chebyshev bounds max error and closes in faster, as well. Minimax is needed because constants aren't infinitely precise and neither are operations. And you need to exploit symmetries, like crazy! \$\endgroup\$ – jonk Oct 7 '16 at 22:53
  • 3
    \$\begingroup\$ Are you aware that there is a series that can be used to calculate sin just as there is for cos, logs, exponents, pi, square roots etc. \$\endgroup\$ – Andy aka Oct 7 '16 at 23:20
  • 2
    \$\begingroup\$ Of course you can have content in computer memory "in the first place" - how do you think computers boot? Initial values can be wired into a circuit, built into read only memory cells or the metalization layers of an ic, burned into fuses, stored as trapped charge as well as read from disk, punch tape, or toggle switches - any method usable for software is in principle useful for precomputed tables, too, and in fact many algorithm needs various constants. What is best really has to be decided based on requirements, technology, and even what resources are left over after other needs. \$\endgroup\$ – Chris Stratton Oct 8 '16 at 3:40
30
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ CORDIC 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. \$\endgroup\$ – TEMLIB Oct 8 '16 at 16:34
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Spehro Pefhany Oct 8 '16 at 16:57
  • \$\begingroup\$ 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. \$\endgroup\$ – Hot Licks Oct 9 '16 at 1:50
  • \$\begingroup\$ @HotLicks - you would be incorrect, CORDIC was developed for and used in an in-flight navigation computer nearly ten years before the HP 9100A. \$\endgroup\$ – Chris Stratton Oct 9 '16 at 3:58
  • \$\begingroup\$ @ChrisStratton - I stand corrected. \$\endgroup\$ – Hot Licks Oct 9 '16 at 12:45
14
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.