peridot_math/
numtraits.rs

1//! PeridotExtendedMathematics: Numeric Traits
2
3/// Provides zero(additive identity)
4pub trait Zero {
5    const ZERO: Self;
6}
7/// Provides one(multiple identity)
8pub trait One {
9    const ONE: Self;
10}
11
12impl Zero for u8 {
13    const ZERO: Self = 0;
14}
15impl Zero for i8 {
16    const ZERO: Self = 0;
17}
18impl Zero for u16 {
19    const ZERO: Self = 0;
20}
21impl Zero for i16 {
22    const ZERO: Self = 0;
23}
24impl Zero for u32 {
25    const ZERO: Self = 0;
26}
27impl Zero for i32 {
28    const ZERO: Self = 0;
29}
30impl Zero for u64 {
31    const ZERO: Self = 0;
32}
33impl Zero for i64 {
34    const ZERO: Self = 0;
35}
36impl Zero for f32 {
37    const ZERO: Self = 0.0;
38}
39impl Zero for f64 {
40    const ZERO: Self = 0.0;
41}
42impl One for u8 {
43    const ONE: Self = 1;
44}
45impl One for i8 {
46    const ONE: Self = 1;
47}
48impl One for u16 {
49    const ONE: Self = 1;
50}
51impl One for i16 {
52    const ONE: Self = 1;
53}
54impl One for u32 {
55    const ONE: Self = 1;
56}
57impl One for i32 {
58    const ONE: Self = 1;
59}
60impl One for u64 {
61    const ONE: Self = 1;
62}
63impl One for i64 {
64    const ONE: Self = 1;
65}
66impl One for f32 {
67    const ONE: Self = 1.0;
68}
69impl One for f64 {
70    const ONE: Self = 1.0;
71}
72
73/// Provides minimum value selector
74pub trait Min<Other = Self> {
75    type Output;
76    fn min(self, other: Other) -> Self::Output;
77}
78/// Provides maximum value selector
79pub trait Max<Other = Self> {
80    type Output;
81    fn max(self, other: Other) -> Self::Output;
82}
83
84impl Min for f32 {
85    type Output = f32;
86    fn min(self, other: f32) -> f32 {
87        self.min(other)
88    }
89}
90impl Min for f64 {
91    type Output = f64;
92    fn min(self, other: f64) -> f64 {
93        self.min(other)
94    }
95}
96impl Max for f32 {
97    type Output = f32;
98    fn max(self, other: f32) -> f32 {
99        self.max(other)
100    }
101}
102impl Max for f64 {
103    type Output = f64;
104    fn max(self, other: f64) -> f64 {
105        self.max(other)
106    }
107}
108
109/// Indicates real numbers type
110pub trait Real: Sized {
111    fn sqrt(self) -> Self;
112    fn sin(self) -> Self;
113    fn cos(self) -> Self;
114    fn sin_cos(self) -> (Self, Self);
115    fn acos(self) -> Self;
116    fn pow(self, rank: Self) -> Self;
117}
118
119impl Real for f32 {
120    #[inline(always)]
121    fn sqrt(self) -> Self {
122        f32::sqrt(self)
123    }
124
125    #[inline(always)]
126    fn sin(self) -> Self {
127        f32::sin(self)
128    }
129
130    #[inline(always)]
131    fn cos(self) -> Self {
132        f32::cos(self)
133    }
134
135    #[inline(always)]
136    fn sin_cos(self) -> (Self, Self) {
137        f32::sin_cos(self)
138    }
139
140    #[inline(always)]
141    fn acos(self) -> Self {
142        f32::acos(self)
143    }
144
145    #[inline(always)]
146    fn pow(self, rank: Self) -> Self {
147        f32::powf(self, rank)
148    }
149}
150impl Real for f64 {
151    #[inline(always)]
152    fn sqrt(self) -> Self {
153        f64::sqrt(self)
154    }
155
156    #[inline(always)]
157    fn sin(self) -> Self {
158        f64::sin(self)
159    }
160
161    #[inline(always)]
162    fn cos(self) -> Self {
163        f64::cos(self)
164    }
165
166    #[inline(always)]
167    fn sin_cos(self) -> (Self, Self) {
168        f64::sin_cos(self)
169    }
170
171    #[inline(always)]
172    fn acos(self) -> Self {
173        f64::acos(self)
174    }
175
176    #[inline(always)]
177    fn pow(self, rank: Self) -> Self {
178        f64::powf(self, rank)
179    }
180}