1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
use crate::*; pub trait Real: Num + Copy { const PI: Self; fn signum(self) -> Self; fn floor(self) -> Self; fn ceil(self) -> Self; fn sqrt(self) -> Self; fn tan(self) -> Self; fn sin(self) -> Self; fn cos(self) -> Self; fn sin_cos(self) -> (Self, Self) { (self.sin(), self.cos()) } fn atan2(y: Self, x: Self) -> Self; fn as_f32(self) -> f32; } impl<T: Real> Float for T { const PI: Self = <Self as Real>::PI; fn signum(self) -> Self { Real::signum(self) } fn floor(self) -> Self { Real::floor(self) } fn ceil(self) -> Self { Real::ceil(self) } fn sqrt(self) -> Self { Real::sqrt(self) } fn tan(self) -> Self { Real::tan(self) } fn sin(self) -> Self { Real::sin(self) } fn cos(self) -> Self { Real::cos(self) } fn sin_cos(self) -> (Self, Self) { Real::sin_cos(self) } fn atan2(y: Self, x: Self) -> Self { Real::atan2(y, x) } fn is_finite(self) -> bool { true } fn as_f32(self) -> f32 { Real::as_f32(self) } } #[derive(Copy, Clone, PartialEq, PartialOrd, Schematic, Serialize)] #[repr(transparent)] pub struct RealImpl<T: Float>(T); impl<T: Float + Trans> Trans for RealImpl<T> { fn read_from(reader: &mut dyn std::io::Read) -> Result<Self, std::io::Error> { let value = T::read_from(reader)?; if value.is_finite() { Ok(Self::new_unchecked(value)) } else { Err(std::io::Error::new( std::io::ErrorKind::Other, "Value must be finite", )) } } fn write_to(&self, writer: &mut dyn std::io::Write) -> Result<(), std::io::Error> { self.0.write_to(writer) } } macro_rules! impl_for { ($t:ty) => { impl<'de> Deserialize<'de> for RealImpl<$t> { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de>, { <$t as Deserialize>::deserialize(deserializer)? .try_into() .map_err(serde::de::Error::custom) } } impl TryFrom<$t> for RealImpl<$t> { type Error = &'static str; fn try_from(value: $t) -> Result<Self, Self::Error> { if value.is_finite() { Ok(Self::new_unchecked(value)) } else { Err("Value must be finite") } } } }; } impl_for!(f32); impl_for!(f64); impl<T: Float> Debug for RealImpl<T> { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { <T as std::fmt::Debug>::fmt(&self.0, fmt) } } impl<T: Float> Display for RealImpl<T> { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { <T as std::fmt::Display>::fmt(&self.0, fmt) } } impl<T: Float> RealImpl<T> { pub fn new(value: T) -> Self { assert!(value.is_finite()); Self { 0: value } } pub fn new_unchecked(value: T) -> Self { Self { 0: value } } pub fn raw(self) -> T { self.0 } } impl<T: Float> Eq for RealImpl<T> {} impl<T: Float> Ord for RealImpl<T> { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap() } } macro_rules! impl_op { ($($op:ident: $fn:ident,)*) => { $( impl<T: Float> $op for RealImpl<T> { type Output = Self; fn $fn(self, rhs: Self) -> Self { Self::new(self.0 .$fn(rhs.0)) } } )* }; } macro_rules! impl_op_assign { ($($op:ident: $fn:ident,)*) => { $( impl<T: Float> $op for RealImpl<T> { fn $fn(&mut self, rhs: Self) { self.0 .$fn(rhs.0); } } )* }; } impl_op! { Add: add, Sub: sub, Mul: mul, Div: div, } impl_op_assign! { AddAssign: add_assign, SubAssign: sub_assign, MulAssign: mul_assign, DivAssign: div_assign, } impl<T: Float> Neg for RealImpl<T> { type Output = Self; fn neg(self) -> Self { Self::new(-self.0) } } impl<T: Float> UNum for RealImpl<T> { const ZERO: Self = Self { 0: T::ZERO }; const ONE: Self = Self { 0: T::ONE }; } impl<T: Float> RealImpl<T> { pub const PI: Self = <Self as Real>::PI; pub fn signum(self) -> Self { Real::signum(self) } pub fn floor(self) -> Self { Real::floor(self) } pub fn ceil(self) -> Self { Real::ceil(self) } pub fn sqrt(self) -> Self { Real::sqrt(self) } pub fn tan(self) -> Self { Real::tan(self) } pub fn sin(self) -> Self { Real::sin(self) } pub fn cos(self) -> Self { Real::cos(self) } pub fn sin_cos(self) -> (Self, Self) { Real::sin_cos(self) } pub fn atan2(y: Self, x: Self) -> Self { Real::atan2(y, x) } pub fn as_f32(self) -> f32 { Real::as_f32(self) } } impl<T: Float> Real for RealImpl<T> { const PI: Self = Self(T::PI); fn signum(self) -> Self { Self::new(self.0.signum()) } fn floor(self) -> Self { Self::new(self.0.floor()) } fn ceil(self) -> Self { Self::new(self.0.ceil()) } fn sqrt(self) -> Self { Self::new(self.0.sqrt()) } fn tan(self) -> Self { Self::new(self.0.tan()) } fn sin(self) -> Self { Self::new(self.0.sin()) } fn cos(self) -> Self { Self::new(self.0.cos()) } fn sin_cos(self) -> (Self, Self) { let (sin, cos) = self.0.sin_cos(); (Self::new(sin), Self::new(cos)) } fn atan2(y: Self, x: Self) -> Self { Self::new(T::atan2(y.0, x.0)) } fn as_f32(self) -> f32 { self.0.as_f32() } } pub type R32 = RealImpl<f32>; pub fn r32(value: f32) -> R32 { R32::new(value) } pub type R64 = RealImpl<f64>; pub fn r64(value: f64) -> R64 { R64::new(value) } #[test] fn test_reals() { let a = r64(3.0); let b = r64(2.0); println!("a = {:?}, b = {:?}", a, b); println!("a + b = {:?}", a + b); println!("a - b = {:?}", a - b); println!("a * b = {:?}", a * b); println!("a / b = {:?}", a / b); println!("sin_cos(a) = {:?}", a.sin_cos()); } #[test] #[should_panic] fn test_reals_fail() { println!("0 / 0 = {:?}", R64::ZERO / R64::ZERO); }