307. to_degrees / to_radians — Stop Hand-Typing 180.0 / PI
sin wants radians, your users want degrees, and somewhere in your codebase a hand-typed * 180.0 / PI is waiting to be fat-fingered. The conversion has been a method on floats since Rust 1.0.
Every trig function in std speaks radians. Every protractor, compass heading, and UI slider speaks degrees. So this line gets written over and over:
| |
Same in the other direction — bite 300’s atan2 hands you radians, and to_degrees turns them back into something a human can read:
| |
The readability win is obvious: no magic constant to mistype (57.2958, 0.0174533, and their truncated cousins show up in real codebases), and the intent is in the method name instead of a comment.
There’s a correctness win too. to_degrees multiplies by one precomputed constant — 180.0 / PI rounded once at full precision. The hand-rolled version does two operations, and the intermediate x * 180.0 can overflow even when the final result is representable:
| |
The everyday use is keeping unit confusion out of trig calls — convert at the boundary, and everything inside stays in radians:
| |
If a PI / 180.0 appears anywhere outside a constants module, it wants to be a to_radians.