Click here to see how trig functions can be used.
If a high degree of accuracy is not required, and memory space is limited, then computation provides an alternative to table look up methods. The following example illustrates this approach for the cosine function.
Given a processor allowing 32 bit signed integer arithmetic, a fair approximation for cosine(x), scaled by a multiplier of 10000 is
10000 * cos(x) = 10000 - X^2 - (X^2 / 2) + (X^4 / 25864)
where x is angle in degrees and "^" means exponentiation. For example, a^4 = aaaa. (This formula is derived from the Taylor series for the cosine function.)
The above formula may be implemented by NXT-G arithmetic blocks as shown below
The above is done in the NXT-G programming environment. Each arithmetic block may be thought of as its NXT-G counter part, placed sequentially on the sequence beam.
The table below shows the values calculated by the above formula versus the more accurate, Excel cosine function.
Click here for Excel spreadsheet that computes the table above.
As you can see, the error tends to increase as we approach 90 degrees. However if our application involves only mid-range angles, then the above algorithm provides accuracy to about plus or minus 2%.