Sample Program Listing — Part 3
* This is the look up table used for the cosine interpolation* function.
* 0, 4, 8, 12, 16, 20, 24, 28, 32COSINE_TABLE fcb $7F, $7E, $7D, $7C, $7A, $77, $74, $70, $6B* 36, 40, 44, 48, 52, 56, 60, 64, 68
fcb $66, $61, $5B, $54, $4E, $47, $3F, $37, $2F* 72, 76, 80, 84, 88, 92, 96, 100, 104 fcb $27, $1E, $16, $0D, $04, $FC, $F3, $EA, $E2* 108, 112, 116, 120, 124, 128, 132, 136, 140 fcb $D9, $D1, $C9, $C1, $B9, $B2, $AC, $A5, $9F* 144, 148, 152, 156, 160, 164, 168, 172, 176 fcb $9A, $95, $90, $8C, $89, $86, $84, $83, $82* 180 fcb $81
Notes:
This is the look up table used for the cosine interpolation function just presented. It consists of the 46 values that represent the cosine of every fourth angle from 0 to 180 degrees, inclusive, multiplied by 127.
Together, this table and the interpolation code require 46 bytes of storage. A complete table and associated look up code for every integer degree from 0 to 180 would require 186 bytes of storage.
If the slightly reduced accuracy of this method can be tolerated, a code space savings of 75% is achieved over a fully implemented look up table. A more accurate 91 entry table (a cosine for every second angle from 0 to 180 degrees inclusive) and associated look up code require 116 bytes of storage and still manage to save 70 bytes over the complete alternative.
Other options are available for implementing trigonometric functions. Taylor series approximations exist for sine and cosine but require arithmetic capability well beyond the means of small microcontrollers. Table interpolation, then, offers a good compromise between space, accuracy, and computational requirements.