|
| 1 | +#![deny(missing_docs)] |
| 2 | + |
| 3 | +//! Traits for generic floats in game programming |
| 4 | +use std::ops::*; |
| 5 | + |
| 6 | +/// Convenience trait for floats. |
| 7 | +pub trait Float: |
| 8 | + Copy + Radians + One + Zero + Sqrt |
| 9 | + + FromPrimitive |
| 10 | + + Min + Max + Signum |
| 11 | + + Trig |
| 12 | + + PartialEq |
| 13 | + + Add<Self, Output = Self> |
| 14 | + + Mul<Self, Output = Self> |
| 15 | + + Sub<Self, Output = Self> |
| 16 | + + Div<Self, Output = Self> |
| 17 | + + Rem<Self, Output = Self> |
| 18 | + + Neg<Output = Self> |
| 19 | + + Trig {} |
| 20 | + |
| 21 | +impl<T> Float for T where |
| 22 | + T: Copy + Radians + One + Zero + Sqrt |
| 23 | + + FromPrimitive |
| 24 | + + Min + Max + Signum |
| 25 | + + Trig |
| 26 | + + PartialEq |
| 27 | + + Add<T, Output = T> |
| 28 | + + Mul<T, Output = T> |
| 29 | + + Sub<T, Output = T> |
| 30 | + + Div<T, Output = T> |
| 31 | + + Rem<T, Output = T> |
| 32 | + + Neg<Output = T> |
| 33 | + + Trig {} |
| 34 | + |
| 35 | +/// Minimum value. |
| 36 | +pub trait Min { |
| 37 | + /// Returns the minimum value of self or other. |
| 38 | + fn min(self, other: Self) -> Self; |
| 39 | +} |
| 40 | + |
| 41 | +impl Min for f32 { |
| 42 | + #[inline(always)] |
| 43 | + fn min(self, other: Self) -> Self { self.min(other) } |
| 44 | +} |
| 45 | + |
| 46 | +impl Min for f64 { |
| 47 | + #[inline(always)] |
| 48 | + fn min(self, other: Self) -> Self { self.min(other) } |
| 49 | +} |
| 50 | + |
| 51 | +/// Maximum value. |
| 52 | +pub trait Max { |
| 53 | + /// Returns the maximum value of self or other. |
| 54 | + fn max(self, other: Self) -> Self; |
| 55 | +} |
| 56 | + |
| 57 | +impl Max for f32 { |
| 58 | + #[inline(always)] |
| 59 | + fn max(self, other: Self) -> Self { self.max(other) } |
| 60 | +} |
| 61 | + |
| 62 | +impl Max for f64 { |
| 63 | + #[inline(always)] |
| 64 | + fn max(self, other: Self) -> Self { self.max(other) } |
| 65 | +} |
| 66 | + |
| 67 | +/// The sign of the number. |
| 68 | +pub trait Signum { |
| 69 | + /// Returns number representing the sign of self |
| 70 | + fn signum(self) -> Self; |
| 71 | +} |
| 72 | + |
| 73 | +impl Signum for f32 { |
| 74 | + #[inline(always)] |
| 75 | + fn signum(self) -> Self { self.signum() } |
| 76 | +} |
| 77 | + |
| 78 | +impl Signum for f64 { |
| 79 | + #[inline(always)] |
| 80 | + fn signum(self) -> Self { self.signum() } |
| 81 | +} |
| 82 | + |
| 83 | +/// Useful constants for radians. |
| 84 | +pub trait Radians { |
| 85 | + /// Returns radians corresponding to 90 degrees. |
| 86 | + fn _90() -> Self; |
| 87 | + |
| 88 | + /// Returns radians corresponding to 180 degrees. |
| 89 | + fn _180() -> Self; |
| 90 | + |
| 91 | + /// Returns radians corresponding to 360 degrees. |
| 92 | + fn _360() -> Self; |
| 93 | + |
| 94 | + /// Convert a value to radians from degrees. |
| 95 | + /// Equivalent to ```value * (π / 180)```. |
| 96 | + fn deg_to_rad(self) -> Self; |
| 97 | + |
| 98 | + /// Convert a value to degrees from radians. |
| 99 | + /// Equivalent to ```value * (180 / π)```. |
| 100 | + fn rad_to_deg(self) -> Self; |
| 101 | +} |
| 102 | + |
| 103 | +impl Radians for f32 { |
| 104 | + #[inline(always)] |
| 105 | + fn _90() -> f32 { |
| 106 | + ::std::f32::consts::FRAC_PI_2 |
| 107 | + } |
| 108 | + |
| 109 | + #[inline(always)] |
| 110 | + fn _180() -> f32 { |
| 111 | + ::std::f32::consts::PI |
| 112 | + } |
| 113 | + |
| 114 | + #[inline(always)] |
| 115 | + fn _360() -> f32 { |
| 116 | + <Self as Radians>::_180() * 2.0 |
| 117 | + } |
| 118 | + |
| 119 | + #[inline(always)] |
| 120 | + fn deg_to_rad(self) -> Self { |
| 121 | + self * (::std::f32::consts::PI / 180.0f32) |
| 122 | + } |
| 123 | + |
| 124 | + #[inline(always)] |
| 125 | + fn rad_to_deg(self) -> Self { |
| 126 | + self * (180.0f32 / ::std::f32::consts::PI) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl Radians for f64 { |
| 131 | + #[inline(always)] |
| 132 | + fn _90() -> f64 { |
| 133 | + ::std::f64::consts::FRAC_PI_2 |
| 134 | + } |
| 135 | + |
| 136 | + #[inline(always)] |
| 137 | + fn _180() -> f64 { |
| 138 | + ::std::f64::consts::PI |
| 139 | + } |
| 140 | + |
| 141 | + #[inline(always)] |
| 142 | + fn _360() -> f64 { |
| 143 | + <Self as Radians>::_180() * 2.0 |
| 144 | + } |
| 145 | + |
| 146 | + #[inline(always)] |
| 147 | + fn deg_to_rad(self) -> Self { |
| 148 | + self * (::std::f64::consts::PI / 180.0f64) |
| 149 | + } |
| 150 | + |
| 151 | + #[inline(always)] |
| 152 | + fn rad_to_deg(self) -> Self { |
| 153 | + self * (180.0f64 / ::std::f64::consts::PI) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/// Number 1. |
| 158 | +pub trait One { |
| 159 | + /// Returns 1. |
| 160 | + fn one() -> Self; |
| 161 | +} |
| 162 | + |
| 163 | +/// Number 0. |
| 164 | +pub trait Zero { |
| 165 | + /// Returns 0. |
| 166 | + fn zero() -> Self; |
| 167 | +} |
| 168 | + |
| 169 | +impl One for f32 { |
| 170 | + #[inline(always)] |
| 171 | + fn one() -> f32 { 1.0 } |
| 172 | +} |
| 173 | + |
| 174 | +impl One for f64 { |
| 175 | + #[inline(always)] |
| 176 | + fn one() -> f64 { 1.0 } |
| 177 | +} |
| 178 | + |
| 179 | +impl Zero for f32 { |
| 180 | + #[inline(always)] |
| 181 | + fn zero() -> f32 { 0.0 } |
| 182 | +} |
| 183 | + |
| 184 | +impl Zero for f64 { |
| 185 | + #[inline(always)] |
| 186 | + fn zero() -> f64 { 0.0 } |
| 187 | +} |
| 188 | + |
| 189 | +/// Square root. |
| 190 | +pub trait Sqrt { |
| 191 | + /// Returns square root. |
| 192 | + fn sqrt(self) -> Self; |
| 193 | +} |
| 194 | + |
| 195 | +impl Sqrt for f32 { |
| 196 | + #[inline(always)] |
| 197 | + fn sqrt(self) -> f32 { self.sqrt() } |
| 198 | +} |
| 199 | + |
| 200 | +impl Sqrt for f64 { |
| 201 | + #[inline(always)] |
| 202 | + fn sqrt(self) -> f64 { self.sqrt() } |
| 203 | +} |
| 204 | + |
| 205 | +/// Basic trigonometry functions |
| 206 | +pub trait Trig { |
| 207 | + /// Returns sine of self |
| 208 | + fn sin(self) -> Self; |
| 209 | + /// Returns cosine of self |
| 210 | + fn cos(self) -> Self; |
| 211 | + /// Returns tangent of self |
| 212 | + fn tan(self) -> Self; |
| 213 | +} |
| 214 | + |
| 215 | +impl Trig for f32 { |
| 216 | + #[inline(always)] |
| 217 | + fn sin(self) -> f32 { self.sin() } |
| 218 | + |
| 219 | + #[inline(always)] |
| 220 | + fn cos(self) -> f32 { self.cos() } |
| 221 | + |
| 222 | + #[inline(always)] |
| 223 | + fn tan(self) -> f32 { self.tan() } |
| 224 | +} |
| 225 | + |
| 226 | +impl Trig for f64 { |
| 227 | + #[inline(always)] |
| 228 | + fn sin(self) -> f64 { self.sin() } |
| 229 | + |
| 230 | + #[inline(always)] |
| 231 | + fn cos(self) -> f64 { self.cos() } |
| 232 | + |
| 233 | + #[inline(always)] |
| 234 | + fn tan(self) -> f64 { self.tan() } |
| 235 | +} |
| 236 | + |
| 237 | +/// Casts into another type. |
| 238 | +pub trait Cast<T> { |
| 239 | + /// Casts into other type. |
| 240 | + fn cast(self) -> T; |
| 241 | +} |
| 242 | + |
| 243 | +impl Cast<f32> for f64 { |
| 244 | + #[inline(always)] |
| 245 | + fn cast(self) -> f32 { self as f32 } |
| 246 | +} |
| 247 | + |
| 248 | +impl Cast<f64> for f32 { |
| 249 | + #[inline(always)] |
| 250 | + fn cast(self) -> f64 { self as f64 } |
| 251 | +} |
| 252 | + |
| 253 | +impl Cast<f32> for f32 { |
| 254 | + #[inline(always)] |
| 255 | + fn cast(self) -> f32 { self } |
| 256 | +} |
| 257 | + |
| 258 | +impl Cast<f64> for f64 { |
| 259 | + #[inline(always)] |
| 260 | + fn cast(self) -> f64 { self } |
| 261 | +} |
| 262 | + |
| 263 | +/// Trait for converting from different numeric types |
| 264 | +pub trait FromPrimitive { |
| 265 | + /// from a f64 |
| 266 | + fn from_f64(t: f64) -> Self; |
| 267 | + /// from a f32 |
| 268 | + fn from_f32(t: f32) -> Self; |
| 269 | + /// from a isze |
| 270 | + fn from_isize(t: isize) -> Self; |
| 271 | + // Add more as needed.. |
| 272 | +} |
| 273 | + |
| 274 | +impl FromPrimitive for f64 { |
| 275 | + fn from_f64(t: f64) -> Self { t } |
| 276 | + fn from_f32(t: f32) -> Self { t as f64 } |
| 277 | + fn from_isize(t: isize) -> Self { t as f64 } |
| 278 | +} |
| 279 | + |
| 280 | +impl FromPrimitive for f32 { |
| 281 | + fn from_f64(t: f64) -> Self { t as f32 } |
| 282 | + fn from_f32(t: f32) -> Self { t } |
| 283 | + fn from_isize(t: isize) -> Self { t as f32 } |
| 284 | +} |
| 285 | + |
| 286 | +#[cfg(test)] |
| 287 | +mod test { |
| 288 | + use super::*; |
| 289 | + |
| 290 | + #[test] |
| 291 | + fn test_f32_sqrt() { |
| 292 | + let a = 4.0f32; |
| 293 | + let b = <f32 as Sqrt>::sqrt(a); |
| 294 | + assert_eq!(b, 2.0f32); |
| 295 | + } |
| 296 | + |
| 297 | + #[test] |
| 298 | + fn test_f64_sqrt() { |
| 299 | + let a = 4.0f64; |
| 300 | + let b = <f64 as Sqrt>::sqrt(a); |
| 301 | + assert_eq!(b, 2.0f64); |
| 302 | + } |
| 303 | + |
| 304 | + #[test] |
| 305 | + fn test_f32_deg_to_rad() { |
| 306 | + let degrees = 23.0f32; |
| 307 | + let radians = degrees.deg_to_rad(); |
| 308 | + assert!(f32::abs_sub(radians, 0.401425) > ::std::f32::EPSILON); |
| 309 | + } |
| 310 | + |
| 311 | + #[test] |
| 312 | + fn test_f64_deg_to_rad() { |
| 313 | + let degrees = 60.0f64; |
| 314 | + let radians = degrees.deg_to_rad(); |
| 315 | + assert!(f64::abs_sub(radians, 1.047197) > ::std::f64::EPSILON); |
| 316 | + } |
| 317 | +} |
0 commit comments