33 ms·
Technically radians and degrees are dimensionless since they're ratios. More to the point, though, is 'sin' and 'cos' are also methods, so having 'to_degrees'
by widdma 10y ago
Technically radians and degrees are dimensionless since they're ratios.
More to the point, though, is 'sin' and 'cos' are also methods, so having 'to_degrees' as a method seems reasonable.
- steveklabnik 10y agoAlso, methods can be called with a function-like syntax, if you prefer: let r = f32::to_radians(5.0); let x: f32 = 5.0; let r = x.to_radians();
- JoshTriplett 10y agoShouldn't to_degrees and to_radians be part of a trait that f32 and f64 (and potentially other types) both implement, rather than being independent methods directly on f32 and f64?
- steveklabnik 10y agoOver the course of Rust's history, we have tried a number of times to get a trait hierarchy for numerics working, and it's always had problems. It's still a general open question.
- JoshTriplett 10y agoI'm not suggesting a trait hierarchy for numerics, or a numerical tower. I'm purely suggesting putting individual methods or families of related methods into independent traits that each numeric type can implement. For instance: trait AngleConversion { fn to_degrees(&self) -> Self; fn to_radians(&self) -> Self; } impl AngleConversion for f32 { ... } impl AngleConversion for f64 { ... } Operators like + and / already live in traits; why not put methods like these into traits as well, so that users can implement them for their own types (variable-precision floats, decimal floating point, etc)? (As a side note, can a trait accept an AsRef<Self> in place of &self or self, so that register-sized objects implementing Copy don't need to use &self?)
- steveklabnik 10y agoAs always, it's a tradeoff. Doing something as a trait is hard for its own reasons, at least in a standard library. Should radians and degrees be two traits, or one? If we pick wrong, are we okay with having that be stable forever? Plus, since you can define your own traits, if this is functionality you need, you can just do it, and delegate to the inherent implementation for those two types.
- bjz_ 10y agoWe really do need to spend time on the numerics APIs though... I would love to pour some time into it again... just figuring out how to fit it in with all the other stuff I'm doing :( I agree with the caution though. This takes time to get right.
- whyever 10y agoThe advantage of the current approach is that you can just define the traits you need in your crate, and the standard library does not have to stabilize unrefined interfaces.