1use super::{Bounded, IterScan, One, Zero};
2use std::{
3 cmp::Ordering,
4 convert::TryInto,
5 fmt::Display,
6 num::FpCategory,
7 ops::{Add, Div, Mul, Neg, Rem, Sub},
8 str::FromStr,
9};
10
11pub trait Float:
12 Copy
13 + Default
14 + Display
15 + FromStr
16 + PartialEq
17 + PartialOrd
18 + Zero
19 + One
20 + Bounded
21 + Add<Output = Self>
22 + Sub<Output = Self>
23 + Mul<Output = Self>
24 + Div<Output = Self>
25 + Neg<Output = Self>
26 + Rem<Output = Self>
27{
28 fn floor(self) -> Self;
29 fn ceil(self) -> Self;
30 fn round(self) -> Self;
31 fn trunc(self) -> Self;
32 fn fract(self) -> Self;
33 fn abs(self) -> Self;
34 fn signum(self) -> Self;
35 fn copysign(self, sign: Self) -> Self;
36 fn mul_add(self, a: Self, b: Self) -> Self;
37 fn div_euclid(self, rhs: Self) -> Self;
38 fn rem_euclid(self, rhs: Self) -> Self;
39 fn powi(self, n: i32) -> Self;
40 fn powf(self, n: Self) -> Self;
41 fn sqrt(self) -> Self;
42 fn exp(self) -> Self;
43 fn exp2(self) -> Self;
44 fn ln(self) -> Self;
45 fn log(self, base: Self) -> Self;
46 fn log2(self) -> Self;
47 fn log10(self) -> Self;
48 fn cbrt(self) -> Self;
49 fn hypot(self, other: Self) -> Self;
50 fn sin(self) -> Self;
51 fn cos(self) -> Self;
52 fn tan(self) -> Self;
53 fn asin(self) -> Self;
54 fn acos(self) -> Self;
55 fn atan(self) -> Self;
56 fn atan2(self, other: Self) -> Self;
57 fn sin_cos(self) -> (Self, Self);
58 fn exp_m1(self) -> Self;
59 fn ln_1p(self) -> Self;
60 fn sinh(self) -> Self;
61 fn cosh(self) -> Self;
62 fn tanh(self) -> Self;
63 fn asinh(self) -> Self;
64 fn acosh(self) -> Self;
65 fn atanh(self) -> Self;
66 fn is_nan(self) -> bool;
67 fn is_infinite(self) -> bool;
68 fn is_finite(self) -> bool;
69 fn is_normal(self) -> bool;
70 fn classify(self) -> FpCategory;
71 fn is_sign_positive(self) -> bool;
72 fn is_sign_negative(self) -> bool;
73 fn recip(self) -> Self;
74 fn to_degrees(self) -> Self;
75 fn to_radians(self) -> Self;
76 fn max(self, other: Self) -> Self;
77 fn min(self, other: Self) -> Self;
78 fn to_bits(self) -> u64;
79 fn from_bits(v: u64) -> Self;
80 fn total_cmp(&self, other: &Self) -> Ordering;
81 fn clamp(self, min: Self, max: Self) -> Self;
82 const RADIX: u32;
83 const MANTISSA_DIGITS: u32;
84 const DIGITS: u32;
85 const EPSILON: Self;
86 const MIN: Self;
87 const MIN_POSITIVE: Self;
88 const MAX: Self;
89 const MIN_EXP: i32;
90 const MAX_EXP: i32;
91 const MIN_10_EXP: i32;
92 const MAX_10_EXP: i32;
93 const NAN: Self;
94 const INFINITY: Self;
95 const NEG_INFINITY: Self;
96 const PI: Self;
97 const TAU: Self;
98 const FRAC_PI_2: Self;
99 const FRAC_PI_3: Self;
100 const FRAC_PI_4: Self;
101 const FRAC_PI_6: Self;
102 const FRAC_PI_8: Self;
103 const FRAC_1_PI: Self;
104 const FRAC_2_PI: Self;
105 const FRAC_2_SQRT_PI: Self;
106 const SQRT_2: Self;
107 const FRAC_1_SQRT_2: Self;
108 const E: Self;
109 const LOG2_E: Self;
110 const LOG10_E: Self;
111 const LN_2: Self;
112 const LN_10: Self;
113}
114
115macro_rules! impl_primitive_float {
116 ($({$t:ident $i:ident $u:ident $e:expr})*) => {$(
117 impl Float for $t {
118 fn floor(self) -> Self { $t::floor(self) }
119 fn ceil(self) -> Self { $t::ceil(self) }
120 fn round(self) -> Self { $t::round(self) }
121 fn trunc(self) -> Self { $t::trunc(self) }
122 fn fract(self) -> Self { $t::fract(self) }
123 fn abs(self) -> Self { $t::abs(self) }
124 fn signum(self) -> Self { $t::signum(self) }
125 fn copysign(self, sign: Self) -> Self { $t::copysign(self, sign) }
126 fn mul_add(self, a: Self, b: Self) -> Self { $t::mul_add(self, a, b) }
127 fn div_euclid(self, rhs: Self) -> Self { $t::div_euclid(self, rhs) }
128 fn rem_euclid(self, rhs: Self) -> Self { $t::rem_euclid(self, rhs) }
129 fn powi(self, n: i32) -> Self { $t::powi(self, n) }
130 fn powf(self, n: Self) -> Self { $t::powf(self, n) }
131 fn sqrt(self) -> Self { $t::sqrt(self) }
132 fn exp(self) -> Self { $t::exp(self) }
133 fn exp2(self) -> Self { $t::exp2(self) }
134 fn ln(self) -> Self { $t::ln(self) }
135 fn log(self, base: Self) -> Self { $t::log(self, base) }
136 fn log2(self) -> Self { $t::log2(self) }
137 fn log10(self) -> Self { $t::log10(self) }
138 fn cbrt(self) -> Self { $t::cbrt(self) }
139 fn hypot(self, other: Self) -> Self { $t::hypot(self, other) }
140 fn sin(self) -> Self { $t::sin(self) }
141 fn cos(self) -> Self { $t::cos(self) }
142 fn tan(self) -> Self { $t::tan(self) }
143 fn asin(self) -> Self { $t::asin(self) }
144 fn acos(self) -> Self { $t::acos(self) }
145 fn atan(self) -> Self { $t::atan(self) }
146 fn atan2(self, other: Self) -> Self { $t::atan2(self, other) }
147 fn sin_cos(self) -> (Self, Self) { $t::sin_cos(self) }
148 fn exp_m1(self) -> Self { $t::exp_m1(self) }
149 fn ln_1p(self) -> Self { $t::ln_1p(self) }
150 fn sinh(self) -> Self { $t::sinh(self) }
151 fn cosh(self) -> Self { $t::cosh(self) }
152 fn tanh(self) -> Self { $t::tanh(self) }
153 fn asinh(self) -> Self { $t::asinh(self) }
154 fn acosh(self) -> Self { $t::acosh(self) }
155 fn atanh(self) -> Self { $t::atanh(self) }
156 fn is_nan(self) -> bool { $t::is_nan(self) }
157 fn is_infinite(self) -> bool { $t::is_infinite(self) }
158 fn is_finite(self) -> bool { $t::is_finite(self) }
159 fn is_normal(self) -> bool { $t::is_normal(self) }
160 fn classify(self) -> std::num::FpCategory { $t::classify(self) }
161 fn is_sign_positive(self) -> bool { $t::is_sign_positive(self) }
162 fn is_sign_negative(self) -> bool { $t::is_sign_negative(self) }
163 fn recip(self) -> Self { $t::recip(self) }
164 fn to_degrees(self) -> Self { $t::to_degrees(self) }
165 fn to_radians(self) -> Self { $t::to_radians(self) }
166 fn max(self, other: Self) -> Self { $t::max(self, other) }
167 fn min(self, other: Self) -> Self { $t::min(self, other) }
168 fn to_bits(self) -> u64 { $t::to_bits(self).into() }
169 fn from_bits(v: u64) -> Self { $t::from_bits(v.try_into().unwrap()) }
170 fn total_cmp(&self, other: &Self) -> Ordering { $t::total_cmp(self, other) }
171 fn clamp(self, min: Self, max: Self) -> Self { $t::clamp(self, min, max) }
172 const RADIX: u32 = $t::RADIX;
173 const MANTISSA_DIGITS: u32 = $t::MANTISSA_DIGITS;
174 const DIGITS: u32 = $t::DIGITS;
175 const EPSILON: Self = $t::EPSILON;
176 const MIN: Self = $t::MIN;
177 const MIN_POSITIVE: Self = $t::MIN_POSITIVE;
178 const MAX: Self = $t::MAX;
179 const MIN_EXP: i32 = $t::MIN_EXP;
180 const MAX_EXP: i32 = $t::MAX_EXP;
181 const MIN_10_EXP: i32 = $t::MIN_10_EXP;
182 const MAX_10_EXP: i32 = $t::MAX_10_EXP;
183 const NAN: Self = $t::NAN;
184 const INFINITY: Self = $t::INFINITY;
185 const NEG_INFINITY: Self = $t::NEG_INFINITY;
186 const PI: Self = std::$t::consts::PI;
187 const TAU: Self = std::$t::consts::PI * 2.0;
188 const FRAC_PI_2: Self = std::$t::consts::FRAC_PI_2;
189 const FRAC_PI_3: Self = std::$t::consts::FRAC_PI_3;
190 const FRAC_PI_4: Self = std::$t::consts::FRAC_PI_4;
191 const FRAC_PI_6: Self = std::$t::consts::FRAC_PI_6;
192 const FRAC_PI_8: Self = std::$t::consts::FRAC_PI_8;
193 const FRAC_1_PI: Self = std::$t::consts::FRAC_1_PI;
194 const FRAC_2_PI: Self = std::$t::consts::FRAC_2_PI;
195 const FRAC_2_SQRT_PI: Self = std::$t::consts::FRAC_2_SQRT_PI;
196 const SQRT_2: Self = std::$t::consts::SQRT_2;
197 const FRAC_1_SQRT_2: Self = std::$t::consts::FRAC_1_SQRT_2;
198 const E: Self = std::$t::consts::E;
199 const LOG2_E: Self = std::$t::consts::LOG2_E;
200 const LOG10_E: Self = std::$t::consts::LOG10_E;
201 const LN_2: Self = std::$t::consts::LN_2;
202 const LN_10: Self = std::$t::consts::LN_10;
203 })*
204 };
205}
206impl_primitive_float!({f32 i32 u32 31} {f64 i64 u64 63});
207
208macro_rules! impl_ord_float {
209 ($({$t:ident $n:ident})*) => {$(
210 #[derive(Debug, Copy, Clone, PartialEq, Default)]
211 #[repr(transparent)]
212 pub struct $n(pub $t);
213 impl std::fmt::Display for $n {
214 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215 <$t as std::fmt::Display>::fmt(&self.0, f)
216 }
217 }
218 impl std::str::FromStr for $n {
219 type Err = std::num::ParseFloatError;
220 fn from_str(s: &str) -> Result<Self, Self::Err> {
221 <$t as std::str::FromStr>::from_str(s).map(Self)
222 }
223 }
224 impl From<$t> for $n {
225 fn from(x: $t) -> Self {
226 Self(x)
227 }
228 }
229 impl Zero for $n {
230 fn zero() -> Self {
231 Self(<$t as Zero>::zero())
232 }
233 }
234 impl One for $n {
235 fn one() -> Self {
236 Self(<$t as One>::one())
237 }
238 }
239 impl Bounded for $n {
240 fn maximum() -> Self {
241 Self(<$t as Bounded>::maximum())
242 }
243 fn minimum() -> Self {
244 Self(<$t as Bounded>::minimum())
245 }
246 }
247 impl Add for $n {
248 type Output = Self;
249 fn add(self, rhs: Self) -> Self::Output {
250 Self(<$t as Add>::add(self.0, rhs.0))
251 }
252 }
253 impl Sub for $n {
254 type Output = Self;
255 fn sub(self, rhs: Self) -> Self::Output {
256 Self(<$t as Sub>::sub(self.0, rhs.0))
257 }
258 }
259 impl Mul for $n {
260 type Output = Self;
261 fn mul(self, rhs: Self) -> Self::Output {
262 Self(<$t as Mul>::mul(self.0, rhs.0))
263 }
264 }
265 impl Div for $n {
266 type Output = Self;
267 fn div(self, rhs: Self) -> Self::Output {
268 Self(<$t as Div>::div(self.0, rhs.0))
269 }
270 }
271 impl Neg for $n {
272 type Output = Self;
273 fn neg(self) -> Self::Output {
274 Self(<$t as Neg>::neg(self.0))
275 }
276 }
277 impl Rem for $n {
278 type Output = Self;
279 fn rem(self, rhs: Self) -> Self::Output {
280 Self(<$t as Rem>::rem(self.0, rhs.0))
281 }
282 }
283 impl Eq for $n {}
284 impl PartialOrd for $n {
285 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
286 Some(self.cmp(other))
287 }
288 }
289 impl Ord for $n {
290 fn cmp(&self, other: &Self) -> Ordering {
291 self.total_cmp(other)
292 }
293 }
294 impl IterScan for $n {
295 type Output = Self;
296 fn scan<'a, I: Iterator<Item = &'a str>>(iter: &mut I) -> Option<Self::Output> {
297 <$t as IterScan>::scan(iter).map(Self)
298 }
299 }
300 impl Float for $n {
301 fn floor(self) -> Self { Self(<$t as Float>::floor(self.0)) }
302 fn ceil(self) -> Self { Self(<$t as Float>::ceil(self.0)) }
303 fn round(self) -> Self { Self(<$t as Float>::round(self.0)) }
304 fn trunc(self) -> Self { Self(<$t as Float>::trunc(self.0)) }
305 fn fract(self) -> Self { Self(<$t as Float>::fract(self.0)) }
306 fn abs(self) -> Self { Self(<$t as Float>::abs(self.0)) }
307 fn signum(self) -> Self { Self(<$t as Float>::signum(self.0)) }
308 fn copysign(self, sign: Self) -> Self { Self(<$t as Float>::copysign(self.0, sign.0)) }
309 fn mul_add(self, a: Self, b: Self) -> Self { Self(<$t as Float>::mul_add(self.0, a.0, b.0)) }
310 fn div_euclid(self, rhs: Self) -> Self { Self(<$t as Float>::div_euclid(self.0, rhs.0)) }
311 fn rem_euclid(self, rhs: Self) -> Self { Self(<$t as Float>::rem_euclid(self.0, rhs.0)) }
312 fn powi(self, n: i32) -> Self { Self(<$t as Float>::powi(self.0, n)) }
313 fn powf(self, n: Self) -> Self { Self(<$t as Float>::powf(self.0, n.0)) }
314 fn sqrt(self) -> Self { Self(<$t as Float>::sqrt(self.0)) }
315 fn exp(self) -> Self { Self(<$t as Float>::exp(self.0)) }
316 fn exp2(self) -> Self { Self(<$t as Float>::exp2(self.0)) }
317 fn ln(self) -> Self { Self(<$t as Float>::ln(self.0)) }
318 fn log(self, base: Self) -> Self { Self(<$t as Float>::log(self.0, base.0)) }
319 fn log2(self) -> Self { Self(<$t as Float>::log2(self.0)) }
320 fn log10(self) -> Self { Self(<$t as Float>::log10(self.0)) }
321 fn cbrt(self) -> Self { Self(<$t as Float>::cbrt(self.0)) }
322 fn hypot(self, other: Self) -> Self { Self(<$t as Float>::hypot(self.0, other.0)) }
323 fn sin(self) -> Self { Self(<$t as Float>::sin(self.0)) }
324 fn cos(self) -> Self { Self(<$t as Float>::cos(self.0)) }
325 fn tan(self) -> Self { Self(<$t as Float>::tan(self.0)) }
326 fn asin(self) -> Self { Self(<$t as Float>::asin(self.0)) }
327 fn acos(self) -> Self { Self(<$t as Float>::acos(self.0)) }
328 fn atan(self) -> Self { Self(<$t as Float>::atan(self.0)) }
329 fn atan2(self, other: Self) -> Self { Self(<$t as Float>::atan2(self.0, other.0)) }
330 fn sin_cos(self) -> (Self, Self) { let (sin, cos) = <$t as Float>::sin_cos(self.0); (Self(sin), Self(cos)) }
331 fn exp_m1(self) -> Self { Self(<$t as Float>::exp_m1(self.0)) }
332 fn ln_1p(self) -> Self { Self(<$t as Float>::ln_1p(self.0)) }
333 fn sinh(self) -> Self { Self(<$t as Float>::sinh(self.0)) }
334 fn cosh(self) -> Self { Self(<$t as Float>::cosh(self.0)) }
335 fn tanh(self) -> Self { Self(<$t as Float>::tanh(self.0)) }
336 fn asinh(self) -> Self { Self(<$t as Float>::asinh(self.0)) }
337 fn acosh(self) -> Self { Self(<$t as Float>::acosh(self.0)) }
338 fn atanh(self) -> Self { Self(<$t as Float>::atanh(self.0)) }
339 fn is_nan(self) -> bool { <$t as Float>::is_nan(self.0) }
340 fn is_infinite(self) -> bool { <$t as Float>::is_infinite(self.0) }
341 fn is_finite(self) -> bool { <$t as Float>::is_finite(self.0) }
342 fn is_normal(self) -> bool { <$t as Float>::is_normal(self.0) }
343 fn classify(self) -> std::num::FpCategory { <$t as Float>::classify(self.0) }
344 fn is_sign_positive(self) -> bool { <$t as Float>::is_sign_positive(self.0) }
345 fn is_sign_negative(self) -> bool { <$t as Float>::is_sign_negative(self.0) }
346 fn recip(self) -> Self { Self(<$t as Float>::recip(self.0)) }
347 fn to_degrees(self) -> Self { Self(<$t as Float>::to_degrees(self.0)) }
348 fn to_radians(self) -> Self { Self(<$t as Float>::to_radians(self.0)) }
349 fn max(self, other: Self) -> Self { Self(<$t as Float>::max(self.0, other.0)) }
350 fn min(self, other: Self) -> Self { Self(<$t as Float>::min(self.0, other.0)) }
351 fn to_bits(self) -> u64 { <$t as Float>::to_bits(self.0) }
352 fn from_bits(v: u64) -> Self { Self(<$t as Float>::from_bits(v)) }
353 fn total_cmp(&self, other: &Self) -> Ordering { <$t as Float>::total_cmp(&self.0, &other.0) }
354 fn clamp(self, min: Self, max: Self) -> Self { Self(<$t as Float>::clamp(self.0, min.0, max.0)) }
355 const RADIX: u32 = <$t as Float>::RADIX;
356 const MANTISSA_DIGITS: u32 = <$t as Float>::MANTISSA_DIGITS;
357 const DIGITS: u32 = <$t as Float>::DIGITS;
358 const EPSILON: Self = Self(<$t as Float>::EPSILON);
359 const MIN: Self = Self(<$t as Float>::MIN);
360 const MIN_POSITIVE: Self = Self(<$t as Float>::MIN_POSITIVE);
361 const MAX: Self = Self(<$t as Float>::MAX);
362 const MIN_EXP: i32 = <$t as Float>::MIN_EXP;
363 const MAX_EXP: i32 = <$t as Float>::MAX_EXP;
364 const MIN_10_EXP: i32 = <$t as Float>::MIN_10_EXP;
365 const MAX_10_EXP: i32 = <$t as Float>::MAX_10_EXP;
366 const NAN: Self = Self(<$t as Float>::NAN);
367 const INFINITY: Self = Self(<$t as Float>::INFINITY);
368 const NEG_INFINITY: Self = Self(<$t as Float>::NEG_INFINITY);
369 const PI: Self = Self(<$t as Float>::PI);
370 const TAU: Self = Self(<$t as Float>::TAU);
371 const FRAC_PI_2: Self = Self(<$t as Float>::FRAC_PI_2);
372 const FRAC_PI_3: Self = Self(<$t as Float>::FRAC_PI_3);
373 const FRAC_PI_4: Self = Self(<$t as Float>::FRAC_PI_4);
374 const FRAC_PI_6: Self = Self(<$t as Float>::FRAC_PI_6);
375 const FRAC_PI_8: Self = Self(<$t as Float>::FRAC_PI_8);
376 const FRAC_1_PI: Self = Self(<$t as Float>::FRAC_1_PI);
377 const FRAC_2_PI: Self = Self(<$t as Float>::FRAC_2_PI);
378 const FRAC_2_SQRT_PI: Self = Self(<$t as Float>::FRAC_2_SQRT_PI);
379 const SQRT_2: Self = Self(<$t as Float>::SQRT_2);
380 const FRAC_1_SQRT_2: Self = Self(<$t as Float>::FRAC_1_SQRT_2);
381 const E: Self = Self(<$t as Float>::E);
382 const LOG2_E: Self = Self(<$t as Float>::LOG2_E);
383 const LOG10_E: Self = Self(<$t as Float>::LOG10_E);
384 const LN_2: Self = Self(<$t as Float>::LN_2);
385 const LN_10: Self = Self(<$t as Float>::LN_10);
386 })*
387 };
388}
389impl_ord_float!({f32 Float32} {f64 Float64});