lib 0.0.3-0

LIB: Math and container utilities for Rust. Notice: study purpose, not production ready.
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use crate::math::{IsAddId, NaN, NegInf, NegMulId, PosInf, GCD};
use crate::math::num_theory::gcd_euclid_iterative;
use crate::math::traits::{Abs, AddId, HasPartialSign,IsNaN, Signum,MulId};

#[derive(Debug, Copy, Clone, Hash)]
pub struct Fraction<T> {
    p: T, //numerator
    q: T, //denominator
}
impl<T> Fraction<T> {
    #[inline]
    pub fn numerator(&self) -> &T {
        &self.p
    }
    #[inline]
    pub fn denominator(&self) -> &T {
        &self.q
    }
    /** Using this function may make calculations slower significantly since it does not simplify the fraction.*/
    pub const fn raw_new(numerator: T, denominator: T) -> Self {
        Self { p:numerator, q:denominator }
    }
}
impl<T:AddId+Neg<Output=T>+Abs<Output=T>+PartialOrd+Rem<Output = T>+Clone+Div<Output = T> + HasPartialSign+Signum+GCD>
Fraction<T> {
    pub fn new(numerator: T, denominator: T) -> Self {
        if denominator.is_zero() {
            // Follow floating-like semantics:
            // - non-zero / 0 => +/-Inf (sign comes from numerator)
            // - 0 / 0 => NaN
            // any argument is NaN => NaN
            return Self { p: numerator.signum(), q: denominator};
        }
        let gcd = gcd_euclid_iterative(numerator.clone().abs(), denominator.clone().abs());
        let mut p:T = numerator / gcd.clone();
        let mut q:T = denominator / gcd;

        if q.is_negative() {
            p = -p;
            q = -q;
        }
        Self { p, q }
    }
}
impl<T: Abs<Output = T>>
Abs for Fraction<T> {
    type Output = Self;

    fn abs(self) -> Self::Output {
        Self {
            p: self.p.abs(),
            q: self.q,
        }
    }
}
impl<T:AddId+Neg<Output=T>+Abs<Output=T>+PartialOrd+Rem<Output = T>+Clone+Div<Output = T> + HasPartialSign+Mul<Output=T>+Add<Output=T>+Signum+GCD>
Add for Fraction<T> {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        if(self.is_nan() || other.is_nan()){
            return Self::NAN;
        }
        if(self.q==other.q){
            return Self::new(self.p+other.p,self.q);
        }
        let num = self.p * other.q.clone() + other.p * self.q.clone();
        let den = self.q * other.q;
        Self::new(num, den)
    }
}

impl<T:Neg<Output = T>>
Neg for Fraction<T> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self {
            p: -self.p,
            q: self.q,
        }
    }
}
impl<T:AddId+Neg<Output=T>+Abs<Output=T>+PartialOrd+Rem<Output = T>+Clone+Div<Output = T> + HasPartialSign+Add<Output=T>+Mul<Output=T>+Signum+GCD>
Sub for Fraction<T> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        self.add(-other)
    }
}
impl<T:AddId+Neg<Output=T>+Abs<Output=T>+PartialOrd+Rem<Output = T>+Clone+Div<Output = T> + HasPartialSign+Mul<Output=T>+Signum+GCD>
Mul for Fraction<T> {
    type Output = Self;
    fn mul(self, other: Self) -> Self::Output {
        let num = self.p * other.p;
        let den = self.q * other.q;
        Self::new(num, den)
    }
}
impl<T:AddId+Neg<Output=T>+Abs<Output=T>+PartialOrd+Rem<Output = T>+Clone+Div<Output = T> + HasPartialSign+Mul<Output=T>+PartialEq+Signum+GCD>
Div for Fraction<T> {
    type Output = Self;
    fn div(self, other: Self) -> Self::Output {
        self * Self::new(other.q, other.p)
    }
}

impl<T: PartialEq + IsAddId>
PartialEq<Self> for Fraction<T> {
    fn eq(&self, other: &Self) -> bool {
        // NaN semantics: NaN != NaN
        if self.is_nan() || other.is_nan() {
            return false;
        }
        self.p == other.p && self.q == other.q
    }
}
impl<T: PartialOrd + Mul<Output = T> + Clone + HasPartialSign>
PartialOrd for Fraction<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if(self.q.is_zero()||other.q.is_zero()){
            // NaN is unordered with everything.
            if (self.q.is_zero() && self.p.is_zero()) || (other.q.is_zero() && other.p.is_zero()) {
                return None;
            }
            // Infinity ordering.
            if self.q.is_zero() && other.q.is_zero() {
                return self.p.partial_cmp(&other.p);
            }
            if other.q.is_zero() {
                return if other.p.is_positive() {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                };
            }
        }
        let left = self.p.clone() * other.q.clone();
        let right = other.p.clone() * self.q.clone();
        left.partial_cmp(&right)
    }
}

#[cfg(not(feature = "specialization"))]
impl<T> HasPartialSign for Fraction<T>
where
    Fraction<T>: PartialOrd + IsAddId,
{}

impl<T:AddId+MulId> AddId for Fraction<T> {
    const ZERO: Self =Self { p: T::ZERO, q: T::ONE };
}
impl<T:AddId+MulId+PartialEq+IsAddId> IsAddId for Fraction<T> {
    fn is_zero(&self) -> bool {
        self.p.is_zero() && self.q.not_zero()
    }
}
impl<T:MulId> MulId for Fraction<T> {
    const ONE: Self =Self { p: T::ONE, q: T::ONE };
}
impl<T:AddId> NaN for Fraction<T> {
    const NAN: Self = Self{p: T::ZERO, q: T::ZERO};
}
impl<T:PartialEq+IsAddId> IsNaN for Fraction<T> {
    fn is_nan(&self) -> bool {
        self.q.is_zero() && self.p.is_zero()
    }
}
impl<T:AddId+MulId> PosInf for Fraction<T> {
    const POS_INF: Self = Self{p: T::ONE, q: T::ZERO};
}
impl<T:AddId+MulId+NegMulId> NegInf for Fraction<T> {
    const NEG_INF: Self = Self{p: T::NEG_ONE, q: T::ZERO};
}

#[cfg(not(feature = "specialization"))]
impl<T> crate::math::IsPosInf for Fraction<T>
where
    Fraction<T>: PartialEq + PosInf,
{
    fn is_pos_inf(&self) -> bool {
        self == &Self::POS_INF
    }
}

#[cfg(not(feature = "specialization"))]
impl<T> crate::math::IsNegInf for Fraction<T>
where
    Fraction<T>: PartialEq + NegInf,
{
    fn is_neg_inf(&self) -> bool {
        self == &Self::NEG_INF
    }
}

#[cfg(not(feature = "specialization"))]
impl<T> crate::math::IsInf for Fraction<T>
where
    Fraction<T>: crate::math::IsPosInf + crate::math::IsNegInf,
{
}

impl<T:Display> Display for Fraction<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}/{}", self.p, self.q))
    }
}
impl Fraction<i64> {
    /// Java `constructFromDouble` style conversion:
    /// decompose IEEE-754 bits, keep binary structure, and bound denominator shifts.
    pub fn from_f64_binary(m: f64) -> Self {
        if m == 0.0 {
            return Self::raw_new(0, 1);
        }
        if !m.is_finite() {
            panic!("Input used to construct Fraction<i64> from f64 must be finite. input={m}");
        }

        let bits = m.to_bits();
        let sign_neg = (bits >> 63) != 0;
        let exp_bits = ((bits >> 52) & 0x7ff) as i32;
        let frac_bits = bits & ((1u64 << 52) - 1);

        // significand (includes implicit leading 1 for normal numbers)
        let mut p = if exp_bits == 0 {
            frac_bits as i64
        } else {
            ((1u64 << 52) | frac_bits) as i64
        };
        if p == 0 {
            return Self::raw_new(0, 1);
        }

        // remove trailing binary zeros from significand
        p >>= p.trailing_zeros();

        // unbiased exponent
        let log2_result = if exp_bits == 0 { -1022 } else { exp_bits - 1023 };
        let n = 64 - p.unsigned_abs().leading_zeros() as i32;
        let mut shift = log2_result - n + 1;
        let mut q: i64 = 1;

        if shift > 0 {
            // guard numerator overflow
            if shift > 63 - n {
                panic!("Input too large for Fraction<i64> numerator. input={m}");
            }
            p <<= shift;
        } else if shift < 0 {
            // bound denominator to 2^62; for tiny numbers, sacrifice precision like Java code
            if shift < -62 {
                p >>= -shift - 62;
                if p == 0 {
                    return Self::raw_new(0, 1);
                }
                shift = -62;
            }
            q <<= -shift;
        }

        if sign_neg {
            p = -p;
        }
        Self::new(p, q)
    }
}

impl From<f64> for Fraction<i64> {
    fn from(value: f64) -> Self {
        Fraction::<i64>::from_f64_binary(value)
    }
}

#[macro_export]
macro_rules! impl_fraction_into_primitive {
    ($($to:ty),* $(,)?) => {
        $(
            impl<T: Into<$to>> From<Fraction<T>> for $to {
                fn from(value: Fraction<T>) -> Self {
                    Into::<$to>::into(value.p) / Into::<$to>::into(value.q)
                }
            }

            impl<T: Into<$to> + Copy> From<&Fraction<T>> for $to {
                fn from(value: &Fraction<T>) -> Self {
                    Into::<$to>::into(value.p) / Into::<$to>::into(value.q)
                }
            }
        )*
    };
}
impl_fraction_into_primitive!(f64,f32);
#[cfg(test)]
mod test{
    use std::f64::NAN;
    use crate::math::{Fraction, HasPartialSign, HasSign, IsInf, NegInf, PosInf};

    const TEST_VALUE_F64:[f64;6]=[f64::NEG_INF,-1.0,0.0,1.0,f64::POS_INF,NAN];
    const TEST_VALUE_FRACTION_I64:[Fraction<i64>;6]=
        [Fraction::raw_new(-1,0),Fraction::raw_new(-1,1),Fraction::raw_new(0,1),
        Fraction::raw_new(1,1),Fraction::raw_new(1,0),Fraction::raw_new(0,0)];
    fn check_equivalent(n:f64,f:Fraction<i64>)->bool{
        n.partial_sign()==f.partial_sign()
        &&n.is_infinite()==f.is_inf()
    }
    #[test]
    fn test_fraction_floatiod_add_behavior() {
        for i in 0..6 {
            for j in 0..6 {
                let n1=TEST_VALUE_F64[i];
                let n2=TEST_VALUE_F64[j];
                let f1=TEST_VALUE_FRACTION_I64[i];
                let f2=TEST_VALUE_FRACTION_I64[j];

                let n_add_result=n1+n2;
                let f_add_result=f1+f2;

                assert_eq!(
                    (n_add_result.partial_sign(), n_add_result.is_infinite()),
                    (f_add_result.partial_sign(), f_add_result.is_inf()),
                    "add behavior mismatch: n1={}, n2={}, f1={}, f2={}, n1+n2={}, f1+f2={}, n_sign={:?}, f_sign={:?}, n_inf={}, f_inf={}",
                    n1,n2,f1,f2,n_add_result,f_add_result,
                    n_add_result.partial_sign(),f_add_result.partial_sign(),
                    n_add_result.is_infinite(),f_add_result.is_inf()
                );
            }
        }
    }

    #[test]
    fn test_fraction_floatiod_sub_behavior() {
        for i in 0..6 {
            for j in 0..6 {
                let n1=TEST_VALUE_F64[i];
                let n2=TEST_VALUE_F64[j];
                let f1=TEST_VALUE_FRACTION_I64[i];
                let f2=TEST_VALUE_FRACTION_I64[j];

                let n_sub_result=n1-n2;
                let f_sub_result=f1-f2;

                assert_eq!(
                    (n_sub_result.partial_sign(), n_sub_result.is_infinite()),
                    (f_sub_result.partial_sign(), f_sub_result.is_inf()),
                    "sub behavior mismatch: n1={}, n2={}, f1={}, f2={}, n1-n2={}, f1-f2={}, n_sign={:?}, f_sign={:?}, n_inf={}, f_inf={}",
                    n1,n2,f1,f2,n_sub_result,f_sub_result,
                    n_sub_result.partial_sign(),f_sub_result.partial_sign(),
                    n_sub_result.is_infinite(),f_sub_result.is_inf()
                );
            }
        }
    }

    #[test]
    fn test_fraction_floatiod_mul_behavior() {
        for i in 0..6 {
            for j in 0..6 {
                let n1=TEST_VALUE_F64[i];
                let n2=TEST_VALUE_F64[j];
                let f1=TEST_VALUE_FRACTION_I64[i];
                let f2=TEST_VALUE_FRACTION_I64[j];

                let n_mult_result=n1*n2;
                let f_mult_result=f1*f2;

                assert_eq!(
                    (n_mult_result.partial_sign(), n_mult_result.is_infinite()),
                    (f_mult_result.partial_sign(), f_mult_result.is_inf()),
                    "mul behavior mismatch: n1={}, n2={}, f1={}, f2={}, n1*n2={}, f1*f2={}, n_sign={:?}, f_sign={:?}, n_inf={}, f_inf={}",
                    n1,n2,f1,f2,n_mult_result,f_mult_result,
                    n_mult_result.partial_sign(),f_mult_result.partial_sign(),
                    n_mult_result.is_infinite(),f_mult_result.is_inf()
                );
            }
        }
    }

    #[test]
    fn test_fraction_floatiod_div_behavior() {
        for i in 0..6 {
            for j in 0..6 {
                let n1=TEST_VALUE_F64[i];
                let n2=TEST_VALUE_F64[j];
                let f1=TEST_VALUE_FRACTION_I64[i];
                let f2=TEST_VALUE_FRACTION_I64[j];

                let n_div_result=n1/n2;
                let f_div_result=f1/f2;

                assert_eq!(
                    (n_div_result.partial_sign(), n_div_result.is_infinite()),
                    (f_div_result.partial_sign(), f_div_result.is_inf()),
                    "div behavior mismatch: n1={}, n2={}, f1={}, f2={}, n1/n2={}, f1/f2={}, n_sign={:?}, f_sign={:?}, n_inf={}, f_inf={}",
                    n1,n2,f1,f2,n_div_result,f_div_result,
                    n_div_result.partial_sign(),f_div_result.partial_sign(),
                    n_div_result.is_infinite(),f_div_result.is_inf()
                );
            }
        }
    }

    #[test]
    fn test_fraction_floatiod_cmp_behavior() {
        for i in 0..6 {
            for j in 0..6 {
                let n1=TEST_VALUE_F64[i];
                let n2=TEST_VALUE_F64[j];
                let f1=TEST_VALUE_FRACTION_I64[i];
                let f2=TEST_VALUE_FRACTION_I64[j];

                assert_eq!(
                    n1.partial_cmp(&n2),
                    f1.partial_cmp(&f2),
                    "partial_cmp mismatch: n1={}, n2={}, f1={}, f2={}, n_cmp={:?}, f_cmp={:?}",
                    n1,n2,f1,f2,n1.partial_cmp(&n2),f1.partial_cmp(&f2)
                );
            }
        }
    }
    #[test]
    fn test_fraction_behave_like_double(){
        const EPSILON: f64 = 1e-12;

        fn to_f64(fraction: Fraction<i64>) -> f64 {
            *fraction.numerator() as f64 / *fraction.denominator() as f64
        }

        fn float_eq(left: f64, right: f64) -> bool {
            (left - right).abs() <= EPSILON
        }

        let fractions = [
            Fraction::new(-7_i64, 3),
            Fraction::new(-5_i64, 2),
            Fraction::new(-2_i64, 5),
            Fraction::new(0_i64, 1),
            Fraction::new(1_i64, 3),
            Fraction::new(1_i64, 2),
            Fraction::new(2_i64, 5),
            Fraction::new(7_i64, 10),
            Fraction::new(4_i64, 3),
            Fraction::new(9_i64, 4),
        ];

        fn assert_same_as_double(
            lhs: Fraction<i64>,
            rhs: Fraction<i64>,
            op_name: &str,
            fraction_result: Fraction<i64>,
            double_result: f64,
        ) {
            let fraction_then_double = to_f64(fraction_result);
            assert!(
                float_eq(fraction_then_double, double_result),
                "{} mismatch: lhs={}, rhs={}, (lhs {} rhs) as f64={}, lhs_f64 {} rhs_f64={}",
                op_name, lhs, rhs, op_name, fraction_then_double, op_name, double_result
            );
        }

        for lhs in fractions {
            for rhs in fractions {
                let lhs_f64 = to_f64(lhs);
                let rhs_f64 = to_f64(rhs);

                assert_same_as_double(lhs, rhs, "+", lhs + rhs, lhs_f64 + rhs_f64);
                assert_same_as_double(lhs, rhs, "-", lhs - rhs, lhs_f64 - rhs_f64);
                assert_same_as_double(lhs, rhs, "*", lhs * rhs, lhs_f64 * rhs_f64);

                if rhs != Fraction::new(0_i64, 1) {
                    assert_same_as_double(lhs, rhs, "/", lhs / rhs, lhs_f64 / rhs_f64);
                }
            }
        }
    }
}