emath/
numeric.rs

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
/// Implemented for all builtin numeric types
pub trait Numeric: Clone + Copy + PartialEq + PartialOrd + 'static {
    /// Is this an integer type?
    const INTEGRAL: bool;

    /// Smallest finite value
    const MIN: Self;

    /// Largest finite value
    const MAX: Self;

    fn to_f64(self) -> f64;

    fn from_f64(num: f64) -> Self;
}

macro_rules! impl_numeric_float {
    ($t: ident) => {
        impl Numeric for $t {
            const INTEGRAL: bool = false;
            const MIN: Self = std::$t::MIN;
            const MAX: Self = std::$t::MAX;

            #[inline(always)]
            fn to_f64(self) -> f64 {
                #[allow(trivial_numeric_casts)]
                {
                    self as f64
                }
            }

            #[inline(always)]
            fn from_f64(num: f64) -> Self {
                #[allow(trivial_numeric_casts)]
                {
                    num as Self
                }
            }
        }
    };
}

macro_rules! impl_numeric_integer {
    ($t: ident) => {
        impl Numeric for $t {
            const INTEGRAL: bool = true;
            const MIN: Self = std::$t::MIN;
            const MAX: Self = std::$t::MAX;

            #[inline(always)]
            fn to_f64(self) -> f64 {
                self as f64
            }

            #[inline(always)]
            fn from_f64(num: f64) -> Self {
                num as Self
            }
        }
    };
}

macro_rules! impl_numeric_non_zero_unsigned {
    ($t: path) => {
        impl Numeric for $t {
            const INTEGRAL: bool = true;
            const MIN: Self = Self::MIN;
            const MAX: Self = Self::MAX;

            #[inline(always)]
            fn to_f64(self) -> f64 {
                self.get() as f64
            }

            #[inline(always)]
            fn from_f64(num: f64) -> Self {
                Self::new(num.round().max(1.0) as _).unwrap_or(Self::MIN)
            }
        }
    };
}

impl_numeric_float!(f32);
impl_numeric_float!(f64);
impl_numeric_integer!(i8);
impl_numeric_integer!(u8);
impl_numeric_integer!(i16);
impl_numeric_integer!(u16);
impl_numeric_integer!(i32);
impl_numeric_integer!(u32);
impl_numeric_integer!(i64);
impl_numeric_integer!(u64);
impl_numeric_integer!(isize);
impl_numeric_integer!(usize);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU8);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU16);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU32);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU64);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU128);
impl_numeric_non_zero_unsigned!(std::num::NonZeroUsize);