Skip to main content

celestial_core/angle/
ops.rs

1//! Arithmetic operators for [`Angle`].
2//!
3//! Implements standard math ops: `+`, `-`, `*`, `/`, and unary `-`.
4
5use super::core::Angle;
6use core::ops::*;
7
8/// Angle + Angle → Angle
9impl Add for Angle {
10    type Output = Angle;
11    #[inline]
12    fn add(self, rhs: Self) -> Self {
13        Angle::from_radians(self.radians() + rhs.radians())
14    }
15}
16
17/// Angle - Angle → Angle
18impl Sub for Angle {
19    type Output = Angle;
20    #[inline]
21    fn sub(self, rhs: Self) -> Self {
22        Angle::from_radians(self.radians() - rhs.radians())
23    }
24}
25
26/// Angle * scalar → Angle
27impl Mul<f64> for Angle {
28    type Output = Angle;
29    #[inline]
30    fn mul(self, k: f64) -> Self {
31        Angle::from_radians(self.radians() * k)
32    }
33}
34
35/// Angle / scalar → Angle
36impl Div<f64> for Angle {
37    type Output = Angle;
38    #[inline]
39    fn div(self, k: f64) -> Self {
40        Angle::from_radians(self.radians() / k)
41    }
42}
43
44/// -Angle → Angle
45impl Neg for Angle {
46    type Output = Angle;
47    #[inline]
48    fn neg(self) -> Self {
49        Angle::from_radians(-self.radians())
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_add_sub() {
59        let a = Angle::from_radians(1.0);
60        let b = Angle::from_radians(0.5);
61        assert_eq!((a + b).radians(), 1.5);
62        assert_eq!((a - b).radians(), 0.5);
63    }
64
65    #[test]
66    fn test_mul_div() {
67        let a = Angle::from_radians(1.0);
68        assert_eq!((a * 2.0).radians(), 2.0);
69        assert_eq!((a / 2.0).radians(), 0.5);
70    }
71
72    #[test]
73    fn test_neg() {
74        let a = Angle::from_radians(1.0);
75        assert_eq!((-a).radians(), -1.0);
76        assert_eq!((-(-a)).radians(), 1.0);
77    }
78}