num-order 0.1.2

Numerically consistent `Eq`, `Ord` and `Hash` implementations for various `num` types (`u32`, `f64`, `num_bigint::BigInt`, etc.)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
use crate::NumOrd;
use core::convert::TryFrom;
use core::cmp::Ordering;

// Case0: swap operand, this introduces overhead so only used for non-primitive types
#[allow(unused_macros)]
macro_rules! impl_ord_by_swap {
    ($($t1:ty | $t2:ty;)*) => ($(
        impl NumOrd<$t2> for $t1 {
            #[inline]
            fn num_partial_cmp(&self, other: &$t2) -> Option<Ordering> {
                other.num_partial_cmp(self).map(Ordering::reverse)
            }
        }
    )*);
}

// Case1: forward to builtin operator for same types
macro_rules! impl_ord_equal_types {
    ($($t:ty)*) => ($(
        impl NumOrd<$t> for $t {
            #[inline]
            fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                self.partial_cmp(&other)
            }
        }
    )*);
}

mod _primitive {
    use super::*;

    impl_ord_equal_types! {
        u8 u16 u32 u64 u128 usize
        i8 i16 i32 i64 i128 isize
        f32 f64
    }

    // Case2: forward to same types by safe casting
    macro_rules! impl_ord_by_casting {
        ($($small:ty => $big:ty;)*) => ($(
            impl NumOrd<$small> for $big {
                #[inline]
                fn num_partial_cmp(&self, other: &$small) -> Option<Ordering> {
                    self.partial_cmp(&<$big>::from(*other))
                }
            }
            impl NumOrd<$big> for $small {
                #[inline]
                fn num_partial_cmp(&self, other: &$big) -> Option<Ordering> {
                    <$big>::from(*self).partial_cmp(other)
                }
            }
        )*);
    }

    impl_ord_by_casting! {
        // uN, uM for N < M
        u8  => u128; u8  => u64; u8  => u32; u8 => u16;
        u16 => u128; u16 => u64; u16 => u32;
        u32 => u128; u32 => u64;
        u64 => u128;

        // iN, iM for N > M
        i8  => i128; i8  => i64; i8  => i32; i8 => i16;
        i16 => i128; i16 => i64; i16 => i32;
        i32 => i128; i32 => i64;
        i64 => i128;

        // iN, uM for N > M
        u8  => i128; u8  => i64; u8  => i32; u8 => i16;
        u16 => i128; u16 => i64; u16 => i32;
        u32 => i128; u32 => i64;
        u64 => i128;

        // fN, fM for N > M
        f32 => f64;

        // f32, uM for 24 >= M, since f32 can exactly represent all integers (-2^24,2^24)
        // f64, uM for 53 >= M, since f64 can exactly represent all integers (-2^53,2^53)
        u8 => f32; u16 => f32;
        u8 => f64; u16 => f64; u32 => f64;

        // f32, iM for 24 >= M
        // f64, iM for 53 >= M
        // since iM's range [-2^(M-1),2^(M-1)) includes -2^(M-1), bounds do not change
        i8 => f32; i16 => f32;
        i8 => f64; i16 => f64; i32 => f64;
    }

    // Case3: trivial logic for comparing signed and unsigned integers
    macro_rules! impl_ord_between_diff_sign {
        ($($signed:ty => $unsigned:ty;)*) => ($(
            impl NumOrd<$signed> for $unsigned {
                #[inline]
                fn num_partial_cmp(&self, other: &$signed) -> Option<Ordering> {
                    if other < &0 {
                        Some(Ordering::Greater)
                    } else {
                        self.partial_cmp(&<$unsigned>::try_from(*other).unwrap())
                    }
                }
            }
            impl NumOrd<$unsigned> for $signed {
                #[inline]
                fn num_partial_cmp(&self, other: &$unsigned) -> Option<Ordering> {
                    if self < &0 {
                        Some(Ordering::Less)
                    } else {
                        <$unsigned>::try_from(*self).unwrap().partial_cmp(other)
                    }
                }
            }
        )*);
    }

    impl_ord_between_diff_sign! {
        i8   => u128; i8  => u64; i8  => u32 ; i8  => u16; i8 => u8;
        i16  => u128; i16 => u64; i16 => u32 ; i16 => u16;
        i32  => u128; i32 => u64; i32 => u32 ;
        i64  => u128; i64 => u64;
        i128 => u128; isize => usize;
    }

    // Case4: special handling for comparing float and integer types
    // Note: if `a` is an integer, `a cmp b` equals to `(a, trunc(b)) cmp (trunc(b), b)` (lexicographically)
    #[cfg(feature = "libm")]
    trait Trunc {
        fn trunc(self) -> Self;
    }
    #[cfg(feature = "libm")]
    impl Trunc for f32 {
        fn trunc(self) -> Self {
            libm::truncf(self)
        }
    }
    #[cfg(feature = "libm")]
    impl Trunc for f64 {
        fn trunc(self) -> Self {
            libm::trunc(self)
        }
    }

    macro_rules! impl_ord_between_int_float {
        ($($float:ty | $int:ty;)*) => ($(
            impl NumOrd<$float> for $int {
                #[inline]
                fn num_partial_cmp(&self, other: &$float) -> Option<Ordering> {
                    if other.is_nan() {
                        None
                    } else if other < &(<$int>::MIN as $float) { // integer min is on binary boundary
                        Some(Ordering::Greater)
                    } else if other >= &(<$int>::MAX as $float) { // integer max is not on binary boundary
                        Some(Ordering::Less)
                    } else {
                        let trunc = other.trunc();
                        (self, &trunc).partial_cmp(&(&(trunc as $int), other))
                    }
                }
            }
            impl NumOrd<$int> for $float {
                #[inline]
                fn num_partial_cmp(&self, other: &$int) -> Option<Ordering> {
                    if self.is_nan() {
                        None
                    } else if self < &(<$int>::MIN as $float) { // integer min is on binary boundary
                        Some(Ordering::Less)
                    } else if self >= &(<$int>::MAX as $float) { // integer max is not on binary boundary
                        Some(Ordering::Greater)
                    } else {
                        let trunc = self.trunc();
                        (&(trunc as $int), self).partial_cmp(&(other, &trunc))
                    }
                }
            }
        )*);
    }

    impl_ord_between_int_float! {
        f32|u128; f32|i128; f32|u64; f32|i64; f32|u32; f32|i32;
        f64|u128; f64|i128; f64|u64; f64|i64;
    }

    // Case5: forward size integers to corresponding concrete types
    macro_rules! impl_ord_with_size_types {
        ($($t:ty)*) => ($(
            impl NumOrd<$t> for usize {
                #[inline]
                fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    { (*self as u32).num_partial_cmp(other) }
                    #[cfg(target_pointer_width = "64")]
                    { (*self as u64).num_partial_cmp(other) }
                }
            }
            impl NumOrd<usize> for $t {
                #[inline]
                fn num_partial_cmp(&self, other: &usize) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    { self.num_partial_cmp(&(*other as u32)) }
                    #[cfg(target_pointer_width = "64")]
                    { self.num_partial_cmp(&(*other as u64)) }
                }
            }
            impl NumOrd<$t> for isize {
                #[inline]
                fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    { (*self as i32).num_partial_cmp(other) }
                    #[cfg(target_pointer_width = "64")]
                    { (*self as i64).num_partial_cmp(other) }
                }
            }
            impl NumOrd<isize> for $t {
                #[inline]
                fn num_partial_cmp(&self, other: &isize) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    { self.num_partial_cmp(&(*other as i32)) }
                    #[cfg(target_pointer_width = "64")]
                    { self.num_partial_cmp(&(*other as i64)) }
                }
            }
        )*);
    }

    impl_ord_with_size_types!(u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64);
}

// Case6: separate handling for special types
#[cfg(feature = "num-bigint")]
mod _num_bigint {
    use super::*;
    use num_bigint::{BigInt, BigUint};
    use num_traits::{FromPrimitive, Signed};

    impl_ord_equal_types!(BigInt BigUint);
    impl_ord_by_casting! {
        u8 => BigUint; u16 => BigUint; u32 => BigUint; u64 => BigUint; u128 => BigUint;
        i8 => BigInt; i16 => BigInt; i32 => BigInt; i64 => BigInt; i128 => BigInt;
        u8 => BigInt; u16 => BigInt; u32 => BigInt; u64 => BigInt; u128 => BigInt;
    }
    impl_ord_between_diff_sign! {
        i8 => BigUint; i16 => BigUint; i32 => BigUint; i64 => BigUint; i128 => BigUint;
    }
    impl_ord_with_size_types!(BigInt BigUint);

    // specialized implementations
    impl NumOrd<f32> for BigUint {
        #[inline]
        fn num_partial_cmp(&self, other: &f32) -> Option<Ordering> {
            if other.is_nan() {
                None
            } else if other < &0. {
                Some(Ordering::Greater)
            } else if other.is_infinite() && other.is_sign_positive() {
                Some(Ordering::Less)
            } else {
                let trunc = other.trunc();
                (self, &trunc).partial_cmp(&(&BigUint::from_f32(trunc).unwrap(), other))
            }
        }
    }
    impl NumOrd<f64> for BigUint {
        #[inline]
        fn num_partial_cmp(&self, other: &f64) -> Option<Ordering> {
            if other.is_nan() {
                None
            } else if other < &0. {
                Some(Ordering::Greater)
            } else if other.is_infinite() && other.is_sign_positive() {
                Some(Ordering::Less)
            } else {
                let trunc = other.trunc();
                (self, &trunc).partial_cmp(&(&BigUint::from_f64(trunc).unwrap(), other))
            }
        }
    }
    impl NumOrd<f32> for BigInt {
        #[inline]
        fn num_partial_cmp(&self, other: &f32) -> Option<Ordering> {
            if other.is_nan() {
                None
            } else if other.is_infinite() {
                if other.is_sign_positive() {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            } else {
                let trunc = other.trunc();
                (self, &trunc).partial_cmp(&(&BigInt::from_f32(trunc).unwrap(), other))
            }
        }
    }
    impl NumOrd<f64> for BigInt {
        #[inline]
        fn num_partial_cmp(&self, other: &f64) -> Option<Ordering> {
            if other.is_nan() {
                None
            } else if other.is_infinite() {
                if other.is_sign_positive() {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                }
            } else {
                let trunc = other.trunc();
                (self, &trunc).partial_cmp(&(&BigInt::from_f64(trunc).unwrap(), other))
            }
        }
    }
    impl NumOrd<BigInt> for BigUint {
        #[inline]
        fn num_partial_cmp(&self, other: &BigInt) -> Option<Ordering> {
            if other.is_negative() {
                Some(Ordering::Greater)
            } else {
                self.partial_cmp(other.magnitude())
            }
        }
    }
    impl_ord_by_swap!{ f32|BigInt; f32|BigUint; f64|BigInt; f64|BigUint; BigInt|BigUint; }
}

// FIXME: Implementations for templated numeric types are directly specialized, because there is no
// negative impl or specialization support yet in rust. We could have a generalized way to implement
// the comparsion if the specialization is supported.

#[cfg(feature = "num-rational")]
mod _num_rational {
    use super::*;
    use num_rational::Ratio;
    use num_traits::{float::FloatCore, Signed, Zero};

    impl_ord_equal_types!(
        Ratio<i8> Ratio<i16> Ratio<i32> Ratio<i64> Ratio<i128> Ratio<isize>
        Ratio<u8> Ratio<u16> Ratio<u32> Ratio<u64> Ratio<u128> Ratio<usize>
    );

    macro_rules! impl_ratio_ord_with_int {
        ($($t:ty)*) => ($(
            impl NumOrd<Ratio<$t>> for $t {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$t>) -> Option<Ordering> {
                    (self * other.denom()).partial_cmp(other.numer())
                }
            }
            impl NumOrd<$t> for Ratio<$t> {
                #[inline]
                fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                    self.numer().partial_cmp(&(*other * self.denom()))
                }
            }
        )*);
    }
    impl_ratio_ord_with_int!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);

    macro_rules! impl_ratio_ord_by_casting {
        ($($small:ty => $big:ty;)*) => ($(
            // between ratios
            impl NumOrd<Ratio<$small>> for Ratio<$big> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$small>) -> Option<Ordering> {
                    let r = Ratio::new(*other.numer() as $big, *other.denom() as $big);
                    self.partial_cmp(&r)
                }
            }
            impl NumOrd<Ratio<$big>> for Ratio<$small> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$big>) -> Option<Ordering> {
                    let r = Ratio::new(*self.numer() as $big, *self.denom() as $big);
                    r.partial_cmp(other)
                }
            }

            // between ratio and ints
            impl NumOrd<$small> for Ratio<$big> {
                #[inline]
                fn num_partial_cmp(&self, other: &$small) -> Option<Ordering> {
                    if let Some(prod) = self.denom().checked_mul(*other as $big) {
                        self.numer().partial_cmp(&prod)
                    } else {
                        Some(Ordering::Less)
                    }
                }
            }
            impl NumOrd<Ratio<$big>> for $small {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$big>) -> Option<Ordering> {
                    if let Some(prod) = other.denom().checked_mul(*self as $big) {
                        prod.partial_cmp(other.numer())
                    } else {
                        Some(Ordering::Greater)
                    }
                }
            }
            impl NumOrd<$big> for Ratio<$small> {
                #[inline]
                fn num_partial_cmp(&self, other: &$big) -> Option<Ordering> {
                    if let Some(prod) = other.checked_mul(*self.denom() as $big) {
                        (*self.numer() as $big).partial_cmp(&prod)    
                    } else {
                        Some(Ordering::Less)
                    }
                }
            }
            impl NumOrd<Ratio<$small>> for $big {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$small>) -> Option<Ordering> {
                    if let Some(prod) = self.checked_mul(*other.denom() as $big) {
                        prod.partial_cmp(&(*other.numer() as $big))
                    } else {
                        Some(Ordering::Greater)
                    }
                }
            }
        )*);
    }
    impl_ratio_ord_by_casting! {
        // uN, uM for N < M
        u8  => u128; u8  => u64; u8  => u32; u8 => u16;
        u16 => u128; u16 => u64; u16 => u32;
        u32 => u128; u32 => u64;
        u64 => u128;
    
        // iN, iM for N > M
        i8  => i128; i8  => i64; i8  => i32; i8 => i16;
        i16 => i128; i16 => i64; i16 => i32;
        i32 => i128; i32 => i64;
        i64 => i128;
    
        // iN, uM for N > M
        u8  => i128; u8  => i64; u8  => i32; u8 => i16;
        u16 => i128; u16 => i64; u16 => i32;
        u32 => i128; u32 => i64;
        u64 => i128;
    }

    // cast unsigned integers for comparison
    macro_rules! impl_ratio_ord_between_diff_sign {
        ($($int:ty => $uint:ty;)*) => ($(
            // between ratios
            impl NumOrd<Ratio<$uint>> for Ratio<$int> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$uint>) -> Option<Ordering> {
                    if self.is_negative() {
                        Some(Ordering::Less)
                    } else {
                        let r = Ratio::<$uint>::new(*self.numer() as $uint, *self.denom() as $uint);
                        r.partial_cmp(other)
                    }
                }
            }
            impl NumOrd<Ratio<$int>> for Ratio<$uint> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$int>) -> Option<Ordering> {
                    if other.is_negative() {
                        Some(Ordering::Greater)
                    } else {
                        let r = Ratio::<$uint>::new(*other.numer() as $uint, *other.denom() as $uint);
                        self.partial_cmp(&r)
                    }
                }
            }

            // between ratio and integers
            impl NumOrd<$uint> for Ratio<$int> {
                #[inline]
                fn num_partial_cmp(&self, other: &$uint) -> Option<Ordering> {
                    if self.is_negative() {
                        Some(Ordering::Less)
                    } else {
                        (*self.numer() as $uint).partial_cmp(&(*self.denom() as $uint * other))
                    }
                }
            }
            impl NumOrd<Ratio<$int>> for $uint {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$int>) -> Option<Ordering> {
                    if other.is_negative() {
                        Some(Ordering::Greater)
                    } else {
                        (*other.denom() as $uint * self).partial_cmp(&(*other.numer() as $uint))
                    }
                }
            }
            impl NumOrd<$int> for Ratio<$uint> {
                #[inline]
                fn num_partial_cmp(&self, other: &$int) -> Option<Ordering> {
                    if other.is_negative() {
                        Some(Ordering::Greater)
                    } else {
                        self.numer().partial_cmp(&(*other as $uint * self.denom()))
                    }
                }
            }
            impl NumOrd<Ratio<$uint>> for $int {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$uint>) -> Option<Ordering> {
                    if self.is_negative() {
                        Some(Ordering::Less)
                    } else {
                        (*self as $uint * other.denom()).partial_cmp(other.numer())
                    }
                }
            }
        )*);
    }
    impl_ratio_ord_between_diff_sign! {
        i8  => u128; i8  => u64; i8  => u32; i8  => u16; i8 => u8;
        i16 => u128; i16 => u64; i16 => u32; i16 => u16;
        i32 => u128; i32 => u64; i32 => u32;
        i64 => u128; i64 => u64;
        i128 => u128; isize => usize;
    }

    // special handling for f64 against u64 and i64
    // FIXME: comparing Ratio<u128> or Ratio<i128> against floats is not supported yet since we need double width multiplication.
    // We can implement it efficiently with carrying_mul implemented (rust#85532)
    impl NumOrd<f64> for Ratio<u64> {
        fn num_partial_cmp(&self, other: &f64) -> Option<Ordering> {
            // shortcut for comparing zeros
            if self.is_zero() {
                return 0f64.partial_cmp(other)
            }
            if other.is_zero() {
                return self.numer().partial_cmp(&0)
            }
            
            // shortcut for nan and inf
            if other.is_nan() {
                return None
            } else if other.is_infinite() {
                if other.is_sign_positive() {
                    return Some(Ordering::Less)
                } else { // negative
                    return Some(Ordering::Greater)
                }
            }
            
            // other = sign * man * 2^exp
            let (man, exp, sign) = other.integer_decode();
            if sign < 0 {
                return Some(Ordering::Greater)
            }

            // self = a / b
            let a = *self.numer();
            let b = *self.denom();

            let result = if exp >= 0 {
                // r / f = a / (man * 2^exp * b) if exp >= 0
                if let Some(num) = || -> Option<_> { 1u64.checked_shl(exp as u32)?.checked_mul(man) }() {
                    a.partial_cmp(&num).unwrap()
                } else {
                    Ordering::Less
                }
            } else {
                // r / f = (a * 2^(-exp)) / (man * b) if exp < 0
                let den = man as u128 * b as u128;
                if let Some(num) = || -> Option<_> { 1u128.checked_shl((-exp) as u32)?.checked_mul(a as u128) } () {
                    num.partial_cmp(&den).unwrap()
                } else {
                    Ordering::Greater
                }
            };
            Some(result)
        }
    }
    impl NumOrd<f64> for Ratio<i64> {
        fn num_partial_cmp(&self, other: &f64) -> Option<Ordering> {
            // shortcut for comparing zeros
            if self.is_zero() {
                return 0f64.partial_cmp(other)
            }
            if other.is_zero() {
                return self.numer().partial_cmp(&0)
            }

            // shortcut for nan and inf
            if other.is_nan() {
                return None
            } else if other.is_infinite() {
                if other.is_sign_positive() {
                    return Some(Ordering::Less)
                } else { // negative
                    return Some(Ordering::Greater)
                }
            }
            
            // other = sign * man * 2^exp
            let (man, exp, sign) = other.integer_decode();
            let reverse = match (!self.is_negative(), sign >= 0) {
                (true, false) => return Some(Ordering::Greater),
                (false, true) => return Some(Ordering::Less),
                (true, true) => false,
                (false, false) => true,
            };

            // self = a / b, using safe absolute operation
            let a = if self.numer() < &0 { (*self.numer() as u64).wrapping_neg() } else { *self.numer() as u64 };
            let b = if self.denom() < &0 { (*self.denom() as u64).wrapping_neg() } else { *self.denom() as u64 };

            let result = if exp >= 0 {
                // r / f = a / (man * 2^exp * b) if exp >= 0
                if let Some(num) = || -> Option<_> { 1u64.checked_shl(exp as u32)?.checked_mul(man) }() {
                    a.partial_cmp(&num).unwrap()
                } else {
                    Ordering::Less
                }
            } else {
                // r / f = (a * 2^(-exp)) / (man * b) if exp < 0
                let den = man as u128 * b as u128;
                if let Some(num) = || -> Option<_> { 1u128.checked_shl((-exp) as u32)?.checked_mul(a as u128) } () {
                    num.partial_cmp(&den).unwrap()
                } else {
                    Ordering::Greater
                }
            };

            if reverse {
                Some(result.reverse())
            } else {
                Some(result)
            }
        }
    }
    impl_ord_by_swap!(f64|Ratio<i64>; f64|Ratio<u64>;);

    // cast to f64 and i64 for comparison
    macro_rules! impl_ratio_ord_with_floats_by_casting {
        ($($float:ty => $bfloat:ty | $int:ty => $bint:ty;)*) => ($(
            impl NumOrd<$float> for Ratio<$int> {
                #[inline]
                fn num_partial_cmp(&self, other: &$float) -> Option<Ordering> {
                    let bratio = Ratio::<$bint>::new(*self.numer() as $bint, *self.denom() as $bint);
                    bratio.num_partial_cmp(&(*other as $bfloat))
                }
            }
            impl NumOrd<Ratio<$int>> for $float {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$int>) -> Option<Ordering> {
                    let bratio = Ratio::<$bint>::new(*other.numer() as $bint, *other.denom() as $bint);
                    (*self as $bfloat).num_partial_cmp(&bratio)
                }
            }
        )*);
    }
    impl_ratio_ord_with_floats_by_casting! {
        f32 => f64|i8 => i64; f32 => f64|i16 => i64; f32 => f64|i32 => i64; f32 => f64|i64 => i64;
        f64 => f64|i8 => i64; f64 => f64|i16 => i64; f64 => f64|i32 => i64;
        f32 => f64|u8 => u64; f32 => f64|u16 => u64; f32 => f64|u32 => u64; f32 => f64|u64 => u64;
        f64 => f64|u8 => u64; f64 => f64|u16 => u64; f64 => f64|u32 => u64;
    }

    // deal with size types
    macro_rules! impl_ratio_with_size_types_ord {
        ($($t:ty)*) => ($(
            impl NumOrd<$t> for Ratio<isize> {
                #[inline]
                fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    let r = Ratio::<i32>::new(*self.numer() as i32, *self.denom() as i32);
                    #[cfg(target_pointer_width = "64")]
                    let r = Ratio::<i64>::new(*self.numer() as i64, *self.denom() as i64);

                    r.num_partial_cmp(other)
                }
            }
            impl NumOrd<$t> for Ratio<usize> {
                #[inline]
                fn num_partial_cmp(&self, other: &$t) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    let r = Ratio::<u32>::new(*self.numer() as u32, *self.denom() as u32);
                    #[cfg(target_pointer_width = "64")]
                    let r = Ratio::<u64>::new(*self.numer() as u64, *self.denom() as u64);

                    r.num_partial_cmp(other)
                }
            }
            impl_ord_by_swap!($t|Ratio<isize>; $t|Ratio<usize>;);
        )*);
    }
    impl_ratio_with_size_types_ord!(i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64);
    
    macro_rules! impl_ratio_ord_with_size_types {
        ($($t:ty)*) => ($(
            impl NumOrd<Ratio<$t>> for isize {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$t>) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    return (*self as i32).num_partial_cmp(other);
                    #[cfg(target_pointer_width = "64")]
                    return (*self as i64).num_partial_cmp(other);
                }
            }
            impl NumOrd<Ratio<$t>> for usize {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$t>) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    return (*self as u32).num_partial_cmp(other);
                    #[cfg(target_pointer_width = "64")]
                    return (*self as u64).num_partial_cmp(other);
                }
            }
            impl NumOrd<Ratio<$t>> for Ratio<isize> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$t>) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    let r = Ratio::<i32>::new(*self.numer() as i32, *self.denom() as i32);
                    #[cfg(target_pointer_width = "64")]
                    let r = Ratio::<i64>::new(*self.numer() as i64, *self.denom() as i64);

                    r.num_partial_cmp(other)
                }
            }
            impl NumOrd<Ratio<$t>> for Ratio<usize> {
                #[inline]
                fn num_partial_cmp(&self, other: &Ratio<$t>) -> Option<Ordering> {
                    #[cfg(target_pointer_width = "32")]
                    let r = Ratio::<u32>::new(*self.numer() as u32, *self.denom() as u32);
                    #[cfg(target_pointer_width = "64")]
                    let r = Ratio::<u64>::new(*self.numer() as u64, *self.denom() as u64);

                    r.num_partial_cmp(other)
                }
            }
            impl_ord_by_swap!(Ratio<$t>|isize; Ratio<$t>|usize; Ratio<$t>|Ratio<isize>; Ratio<$t>|Ratio<usize>;);
        )*);
    }
    impl_ratio_ord_with_size_types!(i8 i16 i32 i64 u8 u16 u32 u64);
}

#[cfg(feature = "num-complex")]
mod _num_complex {

}