0

I'm searching for Trigonometric Functions in Pseudo Code. I'm Not good at mathematics, so I can't do much with the formulas in the Wikipedia. Mainly I'm searching for Sine, Cosine, Tangent and the inverse functions (sin⁻¹, cos⁻¹, tan⁻¹) of them. There are also other Trigonometric Functions. But for me the above are the most important.

If it is possible, I would be happy if in the pseudo code only variables, for, if, and operators (+, -, *, /, %, sqrt()) are used, because I do not have a library with advanced mathematics functions.

TheEquah
  • 121
  • 8

1 Answers1

2

Trigonometry functions are Transcendental.
You cannot find an exact expression of them in term of polynomial algebra.

You can approximate them though.

The usual approach is to use periodicity and symmetry to reduce an angle α into and equivalent angle α′ such that sin(α) = sin(α′) but α′ ≪ α.
Simply put you reduce any angle into and angle in the first quadrant or similar, this is easier than it looks.
Once you have a small angle, you can use Taylor Series Expansion to compute the function up to a fixed error magnitude.

Here is a tutorial page.


Another approach is to use a lookup table.
This is especially useful when you can keep track of the required precision of the process and is very fast.
However it takes more memory and may give rise to a step-looking function. Here an introductory page.


Another approach is to use CORDIC Algorithm, this is specially suited for hardware that lacks multiplication support (like some MIPS and ARM chips). From Wikipedia:

CORDIC is generally faster than other approaches when a hardware multiplier is not available (e.g., a microcontroller) [...]

On the other hand, when a hardware multiplier is available (e.g., in a DSP microprocessor), table-lookup methods and power series are generally faster than CORDIC.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124