trigint: An Integer-based Trigonometry Library


trigint is an integer-based trigonometry library for ANSI C. The trigonometry functions in the standard C library use floating point data types (double or float), which may be too slow or unavailable in an embedded environment. This library uses only integer parameters, return values, and calculations. It has been tested on the iPhone with Xcode and on an Atmel AVR microcontroller with avr-gcc, but it is written in ANSI C99, and should work anywhere with a modern C compiler.

Because the standard C library uses radians as the angle unit, they must be represented with a floating point types. To avoid using floating point for angles, a new angle unit is used, trigint_angle_t. This splits the circle into 16,384 angle units, instead of 2*PI radians or 360 degrees. Thus, the angle parameters can be safetly stored in an unsigned 16-bit data type.

The original purpose of this library was to generate sine waves for audio, so the main functions are:

See also:
Trigonometry functions returning 16-bit integer values
Trigonometry functions returning 8-bit integer values

These functions use lookup tables plus linear interoplation to estimate the values for speed. The lookup tables are very small, only 16 entries, to conserve space. See the Performance of trigint_sin16 on iOS page for the speed gains on iOS compared to the standard library functions. The trade-off for linear inerpolation is accuracy, but the accuracy, even with only 16 entry lookup tables, is often sufficient, epsecialy when generating audio. See the Accuracy of trigint_sin16 page for more error analysis.

This library is essentially a C version of the Scott Dattalo's sine wave routine for the PIC microcontroller. Credit goes to Scott for coming up with the algorithm. He's also got a whole write up of sine wave theory.