HeadlinesBriefing favicon HeadlinesBriefing.com

Turns Beat Radians in Code Efficiency

Hacker News •
×

The tau versus pi debate has dominated discussions about mathematical constants in programming, but a more significant opportunity has been overlooked: eliminating radians altogether.

Most code uses pi or tau to convert values to radians for trigonometric functions. For example, in the Godot Engine source code, developers multiply a periodic value h by tau before calling sin, even though h is already in the range [0, 1]. This conversion is unnecessary because math libraries internally multiply the input by 4/pi immediately, effectively undoing the conversion.

Using turns instead of radians simplifies code and improves precision. A turn parameterizes a circle from 0 to 1, where 0.25 represents 90 degrees exactly, unlike radians which cannot represent common angles precisely. This approach eliminates redundant multiplications and divisions.

For those using existing libraries, half-turns offer a practical alternative. The CUDA sincospi intrinsic computes sine and cosine of inputs multiplied by pi, effectively using half-turns. By calling sincospi with half-turns instead of sin and cos with radians, developers can immediately stop using pi and tau constants.

The article argues that switching to turns or half-turns throughout a codebase usually involves minimal changes, often just deleting unnecessary conversion code. Libraries typically convert away from radians internally anyway, making this transition straightforward and beneficial.