Skip to main content

peridot_math/
numtraits.rs

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