Turns are Better than Radians
This article posits a controversial stance: ditching radians (and even tau) for 'turns' in programming makes trigonometric functions more efficient and precise. It argues that common code patterns involve needless conversions to and from radians, leading to computational overhead and potential floating-point inaccuracies. The author suggests adopting 'turns' (where a full circle is 1) as a superior, mathematically valid unit for angle representation in software.
The Lowdown
Casey Muratori challenges the conventional use of radians and tau in software development, advocating for a lesser-known unit called 'turns'. He argues that while many championed 'tau' (2π) over 'pi' for representing a full circle, an even more impactful optimization is to eliminate explicit radian conversions altogether.
- Muratori observes that programmers often convert values to radians (multiplying by π or τ) before calling trigonometric functions. However, the internal implementations of these functions immediately convert the input back (dividing by π or τ) to work with their own optimized representations.
- This results in redundant and computationally expensive multiplication/division operations, which also introduce floating-point inaccuracies because irrational constants like π or τ cannot be perfectly represented.
- He proposes using 'turns' where a full circle is represented by 1. In this system, common angles like 90 degrees become 0.25, 180 degrees become 0.5, and 360 degrees become 1.0. These values are exactly representable as floating-point numbers, unlike their radian equivalents.
- The article demonstrates that 'turns' are not a novel concept but a legitimate mathematical unit. Switching to turns simplifies code by removing unnecessary constants and improving clarity.
- For existing codebases, Muratori suggests adjusting
sinandcosfunctions to accept turns, or leveraging platform-specific intrinsics like CUDA'ssincospiwhich already operate on a 'half-turn' (input multiplied by pi) basis.
By adopting turns, developers can streamline trigonometric calculations, enhance numerical precision for common angles, and eliminate superfluous constants from their code, leading to cleaner and potentially faster programs.
The Gossip
Calculus and Constant Conundrums
Many commenters argued that while turns might offer computational benefits, radians are mathematically fundamental, especially when it comes to calculus. They highlight that the derivatives of sine and cosine are simplest in radians (d/dx sin(x) = cos(x)), and introducing turns would re-introduce the 2π factor into mathematical expressions, making formulas more complex. This mathematical 'purity' is seen as a strong reason to stick with radians for general scientific and engineering contexts.
Precision and Practicality Ponderings
Discussion revolved around the practical aspects of precision and efficiency. Proponents of turns (including the author) emphasized the exact representation of common angles (like 0.25 for 90 degrees) and the elimination of redundant internal conversions. Critics raised questions about whether compilers could optimize away the unnecessary pi operations and noted that floating-point math introduces its own set of challenges, meaning 'exact' might be relative in computation.
Contextual Convention Concerns
Commenters explored whether the 'best' angular unit is application-dependent. While turns might be beneficial for specific domains like game development where angles are often normalized, many argued that radians' connection to arc length and their use in diverse fields beyond geometry (e.g., phase angles in signals) makes them universally suitable. The consensus leaned towards using the appropriate unit for the specific problem at hand, acknowledging that changing established mathematical conventions is a significant undertaking.