competitive/num/
float.rs

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