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
use std::cmp::PartialOrd;
use std::ops::{Add, AddAssign, DivAssign, MulAssign, Sub, SubAssign};

use num_traits::{MulAdd, Num};

use super::cast::Cast;
use super::isnone::IsNone;

/// Kahan summation, see https://en.wikipedia.org/wiki/Kahan_summation_algorithm
#[inline]
fn kh_sum<T>(sum: T, v: T, c: &mut T) -> T
where
    T: Add<Output = T> + Sub<Output = T> + Copy,
{
    let y = v - *c;
    let t = sum + y;
    *c = (t - sum) - y;
    t
}

pub trait Number:
    Copy
    // + Clone
    + Send
    + Sync
    + IsNone
    + Sized
    + Default
    + Num
    + AddAssign
    + SubAssign
    + MulAssign
    + DivAssign
    + PartialOrd
    + MulAdd
    + Cast<f64>
    + Cast<f32>
    + Cast<usize>
    + Cast<i32>
    + Cast<i64>
    + 'static
{
    // type Dtype;
    /// return the min value of the data type
    fn min_() -> Self;

    /// return the max value of the data type
    fn max_() -> Self;

    fn abs(self) -> Self;

    #[inline(always)]
    fn ceil(self) -> Self {
        self
    }

    #[inline(always)]
    fn floor(self) -> Self {
        self
    }

    #[inline]
    fn min_with(self, other: Self) -> Self {
        if other < self {
            other
        } else {
            self
        }
    }

    #[inline]
    fn max_with(self, other: Self) -> Self {
        if other > self {
            other
        } else {
            self
        }
    }

    #[inline(always)]
    fn f32(self) -> f32 {
        Cast::<f32>::cast(self)
    }

    #[inline(always)]
    fn f64(self) -> f64 {
        Cast::<f64>::cast(self)
    }

    #[inline(always)]
    fn i32(self) -> i32 {
        Cast::<i32>::cast(self)
    }

    #[inline(always)]
    fn i64(self) -> i64 {
        Cast::<i64>::cast(self)
    }

    #[inline(always)]
    fn usize(self) -> usize {
        Cast::<usize>::cast(self)
    }

    /// create a value of type T using a value of type U using `Cast`
    #[inline(always)]
    fn fromas<U>(v: U) -> Self
    where
        U: Number + Cast<Self>,
        Self: 'static,
    {
        v.to::<Self>()
    }

    /// cast self to another dtype using `Cast`
    #[inline(always)]
    fn to<T: Number>(self) -> T
    where
        Self: Cast<T>,
    {
        Cast::<T>::cast(self)
    }

    #[inline(always)]
    #[must_use]
    fn kh_sum(self, v: Self, c: &mut Self) -> Self {
        kh_sum(self, v, c)
    }

    /// if other is nan, then add other to self and n += 1
    /// else just return self
    #[inline]
    fn n_add(self, other: Self, n: &mut usize) -> Self {
        // note: only check if other is NaN
        // assume that self is not NaN
        if other.not_none() {
            *n += 1;
            self + other
        } else {
            self
        }
    }

    /// if other is nan, then product other to self and n += 1
    /// else just return self
    #[inline]
    fn n_prod(self, other: Self, n: &mut usize) -> Self {
        // note: only check if other is NaN
        // assume that self is not NaN
        if other.not_none() {
            *n += 1;
            self * other
        } else {
            self
        }
    }
}

macro_rules! impl_number {
    (@ base_impl $dtype:ty, $datatype:ident) => {

        #[inline(always)]
        fn min_() -> $dtype {
            <$dtype>::MIN
        }

        #[inline(always)]
        fn max_() -> $dtype {
            <$dtype>::MAX
        }

    };
    // special impl for float
    (float $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);

            #[inline]
            fn ceil(self) -> Self {
                self.ceil()
            }

            #[inline]
            fn floor(self) -> Self {
                self.floor()
            }

            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }

        })*
    };
    // special impl for other type
    (signed $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);
            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }
        })*
    };
    // special impl for other type
    (unsigned $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);
            #[inline]
            fn abs(self) -> Self {
                self
            }
        })*
    };
}

impl_number!(
    float
    f32, F32;
    f64, F64
);

impl_number!(
    signed
    i32, I32;
    i64, I64
);

impl_number!(
    unsigned
    u64, U64;
    usize, Usize
);

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_ceil() {
        fn _ceil<T: Number>(v: T) -> T {
            v.ceil()
        }
        assert_eq!(_ceil(1.23_f64), 2.);
        assert_eq!(_ceil(-1.23_f32), -1.);
        assert_eq!(_ceil(0_usize), 0);
        assert_eq!(_ceil(-3i32), -3);
    }

    #[test]
    fn test_floor() {
        fn _floor<T: Number>(v: T) -> T {
            v.floor()
        }
        assert_eq!(_floor(1.23_f64), 1.);
        assert_eq!(_floor(-1.23_f32), -2.);
        assert_eq!(_floor(0_usize), 0);
        assert_eq!(_floor(-3i32), -3);
    }
}