Skip to main content
Post Made Community Wiki
Source Link
Dan Piponi
  • 8.7k
  • 6
  • 66
  • 98

They're useful just for doing ordinary geometry when programming.

A common pattern I have seen in a great many computer programs is to start with a bunch of numbers that are really ratios of distances. Theses numbers get converted to angles with inverse trig functions. Then some simple functions are applied to the angles and the trig functions are used on the results.

Trig and inverse trig functions are expensive to compute on a computer. In high performance code you want to eliminate them if possible. Quite often, for the above case, you can eliminate the trig functions. For example $\cos(2\cos^{-1} x) = 2x^2-1$ (for $x$ in a suitable range) but the version on the right runs much faster.

The catch is remembering all those trig formulae. It'd be nice to make the compiler do all the work. A solution is to use complex numbers. Instead of storing $\theta$ we store $(\cos\theta,\sin\theta)$. We can add angles by using complex multiplication, multiply angles by integers and rational numbers using powers and roots and so on. As long as you don't actually need the numerical value of the angle in radians you need never use trig functions. Obviously there comes a point where the work of doing operations on complex numbers may outweigh the saving of avoiding trig. But often in real code the complex number route is faster.

(Of course it's analogous to using quaternions for rotations in 3D. I guess it's somewhat in the spirit of rational trigonometry except I think it's easier to work with complex numbers.)