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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
use std::cmp::Ordering;
use std::fmt;
use std::iter::Peekable;
use std::num::TryFromIntError;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign};

use intentional::Cast;

use crate::primes::{FactorsOf, PRIMES};
use crate::tables::{approximate_via_lookup_table, ARCTAN_SUBDIVISIONS, ARCTAN_TABLE};
use crate::traits::{Abs, Zero};
use crate::Angle;

/// Returns a new fraction.
///
/// This macro has two forms:
///
/// - Whole numbers:
///
///   ```rust
///   use figures::{fraction, Fraction};
///   assert_eq!(fraction!(42), Fraction::new_whole(42));
///   ```
/// - Fractions:
///
///   ```rust
///   use figures::{fraction, Fraction};
///   assert_eq!(fraction!(42/7), Fraction::new(42, 7));
///   ```
#[macro_export]
macro_rules! fraction {
    ($numerator:literal) => {
        $crate::Fraction::new_whole($numerator)
    };
    ($numerator:literal / $denominator:literal) => {
        $crate::Fraction::new($numerator, $denominator)
    };
}

/// A fraction type for predictable integer-based math.
///
/// Internally this type uses 32 bits of data to represent a fraction:
///
/// - 1 bit of data for the positive/negative sign.
/// - 15 bits of data for the numerator
/// - 16 bits of data for the denominator
///
/// Many math operations are performed using temporary 32-bit values for the
/// fraction, simplifing at the end of the operation. This prevents overflows,
/// but does not prevent precision loss. We can see this by purposely buliding
/// fractions that are hard to represent:
///
/// ```rust
/// use figures::fraction;
///
/// assert_eq!(
///     fraction!(1 / 32719) + fraction!(1 / 32749),
///     fraction!(2 / 32749)
/// );
/// ```
///
/// The above example adds fractions that have denominators using two largest
/// primes that fit in 16 bits. The result should be `65,468/1,071,514,531`, but
/// that denominator is clearly too large. Because the fractions being added had
/// prime numbers for their denominators, there is no way to reduce this
/// fraction without losing information. For this particular example, a
/// precision loss of ~`2.8e-8` occurs.
///
/// However, in 2d graphics programming, it's rare to be working with irrational
/// numbers outside of angles represented in radians.
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Fraction {
    #[cfg_attr(feature = "Serde", serde(rename = "n"))]
    numerator: i16,
    #[cfg_attr(feature = "Serde", serde(rename = "d"))]
    denominator: i16,
}

const MIN_VALUE: i16 = -i16::MAX;

impl From<f32> for Fraction {
    #[allow(clippy::cast_possible_truncation)] // truncation desired
    fn from(scale: f32) -> Self {
        if scale < f32::from(MIN_VALUE) {
            Self::MIN
        } else if scale > f32::from(i16::MAX) {
            Self::MAX
        } else {
            let mut best = Fraction {
                numerator: 0,
                denominator: 0,
            };
            let mut best_diff = f32::MAX;
            for denominator in 1..=i16::MAX {
                let numerator = (f32::from(denominator) * scale).round() as i16;
                let ratio = Fraction {
                    numerator,
                    denominator,
                };
                let delta = (ratio.into_f32() - scale).abs();
                if delta < best_diff {
                    best = ratio;
                    best_diff = delta;
                    if delta <= f32::EPSILON {
                        break;
                    }
                }
            }

            best
        }
    }
}

impl From<Fraction32> for Fraction {
    fn from(
        Fraction32 {
            mut numerator,
            mut denominator,
        }: Fraction32,
    ) -> Self {
        reduce(&mut numerator, &mut denominator);
        if let (Ok(numerator), Ok(denominator)) =
            (i16::try_from(numerator), i16::try_from(denominator))
        {
            if numerator >= MIN_VALUE {
                return Self::new_maybe_reduced(numerator, denominator);
            }
        }

        // Reducing didn't yield a fraction that we can represent perfectly.
        // Hunt for the largest prime divisor that yields a usable fraction
        // and the smallest remainder.
        let mut best_numerator = i16::MAX;
        let mut best_denominator = i16::MAX;
        let mut best_remainder = i32::MAX;
        for prime in PRIMES
            .iter()
            .rev()
            .map(|&prime| i32::from(prime))
            .filter(|&prime| numerator >= prime && denominator >= prime)
        {
            let numerator_remainder = numerator % prime;
            let Ok(numerator) = i16::try_from(numerator / prime) else {
                break;
            };
            if numerator < MIN_VALUE {
                break;
            }
            let denominator_remainder = denominator % prime;
            let Ok(denominator) = i16::try_from(denominator / prime) else {
                break;
            };
            let remainder = numerator_remainder + denominator_remainder;
            if remainder < best_remainder {
                best_numerator = numerator;
                best_denominator = denominator;
                best_remainder = remainder;
                if remainder <= 5 {
                    break;
                }
            }
        }
        Self {
            numerator: best_numerator,
            denominator: best_denominator,
        }
    }
}

impl From<i16> for Fraction {
    fn from(numerator: i16) -> Self {
        Self {
            numerator,
            denominator: 1,
        }
    }
}

impl From<Fraction> for f32 {
    fn from(value: Fraction) -> Self {
        value.into_f32()
    }
}

macro_rules! try_from_int {
    ($type:ident) => {
        impl TryFrom<$type> for Fraction {
            type Error = TryFromIntError;

            fn try_from(value: $type) -> Result<Self, Self::Error> {
                i16::try_from(value).map(Self::from)
            }
        }
    };
}

try_from_int!(i32);
try_from_int!(i64);
try_from_int!(i128);
try_from_int!(isize);
try_from_int!(u16);
try_from_int!(u32);
try_from_int!(u64);
try_from_int!(u128);
try_from_int!(usize);

impl Fraction {
    /// The maximum value representable by this type.
    pub const MAX: Self = Self::new_whole(i16::MAX);
    /// The minimum value representable by this type.
    pub const MIN: Self = Self::new_whole(i16::MIN);
    /// A fraction equivalent to 1.
    pub const ONE: Self = Self::new_whole(1);
    /// A fractional approximation of Pi, accurate to within 2.67e-7.
    pub const PI: Self = Self::new_maybe_reduced(355, 113);
    /// A fraction equivalent to 0.
    pub const ZERO: Self = Self::new_maybe_reduced(0, 1);

    /// Returns a new fraction for a whole number.
    #[must_use]
    pub const fn new_whole(whole_number: i16) -> Self {
        Self {
            numerator: whole_number,
            denominator: 1,
        }
    }

    pub(crate) const fn new_maybe_reduced(mut numerator: i16, mut denominator: i16) -> Self {
        if denominator.is_negative() {
            numerator = numerator.saturating_neg();
            denominator = denominator.saturating_neg();
        }
        Self {
            numerator,
            denominator,
        }
    }

    /// Returns a new fraction using the components provided.
    ///
    /// `denominator` will be limited to the absolute value of `i16::MIN`.
    #[must_use]
    pub fn new(numerator: i16, denominator: i16) -> Self {
        Self::new_maybe_reduced(numerator.max(MIN_VALUE), denominator).reduce()
    }

    /// Returns the numerator of the fraction.
    #[must_use]
    pub const fn numerator(&self) -> i16 {
        self.numerator
    }

    /// Returns the denominator of the fraction.
    #[must_use]
    pub const fn denominator(&self) -> i16 {
        self.denominator
    }

    /// Returns true if the fraction is positive (greater than zero).
    ///
    /// Note: Zero is neither negative nor positive.
    #[must_use]
    pub const fn is_positive(&self) -> bool {
        self.numerator > 0
    }

    /// Returns true if the fraction is zero.
    #[must_use]
    pub const fn is_zero(&self) -> bool {
        self.numerator == 0
    }

    /// Returns true if the fraction is negative (less than zero).
    ///
    /// Note: Zero is neither negative nor positive.
    #[must_use]
    pub const fn is_negative(&self) -> bool {
        self.numerator.is_negative()
    }

    /// Simplifies the fraction into a compound number.
    ///
    /// ```rust
    /// use figures::Fraction;
    ///
    /// assert_eq!(
    ///     Fraction::new(1, 3).into_compound(),
    ///     (0, Fraction::new(1, 3))
    /// );
    /// assert_eq!(
    ///     Fraction::new(4, 3).into_compound(),
    ///     (1, Fraction::new(1, 3))
    /// );
    /// assert_eq!(
    ///     Fraction::new(-4, 3).into_compound(),
    ///     (-1, Fraction::new(-1, 3))
    /// );
    /// ```
    ///
    /// Adding the number and the fraction back together will result in the
    /// original fraction.
    ///
    /// ```rust
    /// use figures::Fraction;
    ///
    /// let improper = Fraction::new(-4, 3);
    /// let (whole, fraction) = improper.into_compound();
    /// assert_eq!(Fraction::from(whole) + fraction, improper);
    /// ```
    #[must_use]
    #[allow(clippy::cast_possible_wrap)]
    pub fn into_compound(self) -> (i16, Fraction) {
        let clamped_denominator = self.denominator;
        let whole = self.numerator / clamped_denominator;
        let numerator = self.numerator % clamped_denominator;
        (
            whole,
            Fraction::new_maybe_reduced(numerator, self.denominator),
        )
    }

    /// Rounds this fraction to the nearest whole number.
    #[must_use]
    pub fn round(self) -> i16 {
        self.round_with_amount().0
    }

    /// Rounds this fraction to the nearest whole number, returning the
    /// fractional component that was rounded away. This fraction can be added
    /// to the whole number to reconstruct the original fraction.
    ///
    /// ```rust
    /// use figures::Fraction;
    ///
    /// let (whole, fraction) = Fraction::new(5, 3).round_with_amount();
    /// assert_eq!(whole, 2);
    /// assert_eq!(fraction, Fraction::new(-1, 3));
    /// assert_eq!(Fraction::new_whole(whole) + fraction, Fraction::new(5, 3));
    ///
    /// let (whole, fraction) = Fraction::new(-5, 3).round_with_amount();
    /// assert_eq!(whole, -2);
    /// assert_eq!(fraction, Fraction::new(1, 3));
    /// assert_eq!(Fraction::new_whole(whole) + fraction, Fraction::new(-5, 3));
    /// ```
    #[must_use]
    pub fn round_with_amount(self) -> (i16, Fraction) {
        let (whole, fraction) = self.into_compound();
        let half_denominator = (fraction.denominator + 1) / 2;
        if fraction.numerator >= half_denominator {
            (whole + 1, -(Fraction::new_whole(1) - fraction))
        } else if fraction.numerator <= -half_denominator {
            (whole - 1, -(Fraction::new_whole(-1) - fraction))
        } else {
            (whole, fraction)
        }
    }

    /// Returns this fraction as a floating point number.
    #[must_use]
    pub fn into_f32(self) -> f32 {
        f32::from(self.numerator) / f32::from(self.denominator)
    }

    /// Returns the inverse of this fraction.
    #[must_use]
    #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
    pub const fn inverse(self) -> Self {
        if self.numerator >= 0 {
            Self {
                numerator: self.denominator,
                denominator: self.numerator,
            }
        } else {
            Self {
                numerator: -self.denominator,
                denominator: -self.numerator,
            }
        }
    }

    fn reduce(mut self) -> Self {
        reduce(&mut self.numerator, &mut self.denominator);
        self
    }

    /// Returns the absolute value of this fraction.
    #[must_use]
    pub const fn abs(self) -> Self {
        if self.numerator >= 0 {
            self
        } else {
            Self {
                numerator: -self.numerator,
                denominator: self.denominator,
            }
        }
    }

    /// Returns the arctangent of this fraction.
    ///
    /// This function is implemented using a lookup table and is an
    /// approximation.
    #[must_use]
    pub fn atan(self) -> Angle {
        if self >= Fraction::new_whole(-1) && self <= Fraction::new_whole(1) {
            self.fast_atan()
        } else {
            let inverse_atan = self.inverse().fast_atan();
            if self.is_negative() {
                Angle::degrees(-90) - inverse_atan
            } else {
                Angle::degrees(90) - inverse_atan
            }
        }
    }

    /// Returns the result of arctan(self/other) while correctly handling
    /// negative numbers.
    #[must_use]
    pub fn atan2(self, other: Self) -> Angle {
        let x = other;
        let y = self;
        let atan = (y / x).atan();
        if x.is_negative() {
            if y.is_negative() {
                -Angle::degrees(180) + atan // 3rd quadrant
            } else {
                Angle::degrees(180) + atan // 2nd quadrant
            }
        } else {
            atan // 1st/4th quadrant
        }
    }

    fn fast_atan(self) -> Angle {
        let index = self * Self::new_whole(ARCTAN_SUBDIVISIONS);
        let result = if index.is_negative() {
            -approximate_via_lookup_table(-index, &ARCTAN_TABLE)
        } else {
            approximate_via_lookup_table(index, &ARCTAN_TABLE)
        };
        Angle::radians(result)
    }
}

#[test]
fn atan() {
    assert_eq!(Fraction::new_whole(1).atan(), Angle::degrees(45));
    assert_eq!(Fraction::new_whole(0).atan(), Angle::degrees(0));
    assert_eq!(Fraction::new_whole(-1).atan(), Angle::degrees(315));
}

#[test]
fn atan2() {
    assert_eq!(
        Fraction::new_whole(1).atan2(Fraction::new_whole(1)),
        Angle::degrees(45)
    );
    assert_eq!(
        Fraction::new_whole(1).atan2(Fraction::new_whole(-1)),
        Angle::degrees(135)
    );
    assert_eq!(
        Fraction::new_whole(-1).atan2(Fraction::new_whole(-1)),
        Angle::degrees(225)
    );
    assert_eq!(
        Fraction::new_whole(-1).atan2(Fraction::new_whole(1)),
        Angle::degrees(315)
    );
}

pub fn reduce<T>(numerator: &mut T, denominator: &mut T)
where
    T: Abs + Zero + Copy + From<i16> + Ord + Rem<Output = T> + DivAssign,
{
    let one = T::from(1);
    if numerator.is_zero() {
        *denominator = one;
    } else if *denominator > one {
        for prime in PRIMES {
            let prime = T::from(prime);
            if prime > numerator.abs() || prime > *denominator {
                break;
            }
            while (*numerator % prime).is_zero() && (*denominator % prime).is_zero() {
                *numerator /= prime;
                *denominator /= prime;
                if *denominator == one {
                    break;
                }
            }
        }
    }
}

impl fmt::Debug for Fraction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Fraction({self})")
    }
}

impl fmt::Display for Fraction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.numerator, self.denominator)
    }
}

impl Ord for Fraction {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.denominator == other.denominator {
            // Denominators match
            self.numerator.cmp(&other.numerator)
        } else if self.numerator == other.numerator {
            // Numerators match. The comparison is the inverse of the result of
            // comparing the denominators.
            other.denominator.cmp(&self.denominator)
        } else {
            // To compare these ratios, we must find the lowest common
            // denominator.
            let (this, other) = LowestCommonDenominator::find(*self, *other);
            debug_assert_eq!(this.denominator, other.denominator);
            this.numerator.cmp(&other.numerator)
        }
    }
}

struct LowestCommonDenominator {
    a: Fraction32,
    b: Fraction32,
    a_factors: Peekable<FactorsOf>,
    b_factors: Peekable<FactorsOf>,
}

#[derive(Clone, Copy, Debug)]
pub struct Fraction32 {
    pub numerator: i32,
    pub denominator: i32,
}

impl Fraction32 {
    pub const fn inverse(self) -> Self {
        Self {
            numerator: self.denominator,
            denominator: self.numerator,
        }
    }
}

impl From<Fraction> for Fraction32 {
    fn from(value: Fraction) -> Self {
        Self {
            numerator: i32::from(value.numerator),
            denominator: i32::from(value.denominator),
        }
    }
}

impl From<i32> for Fraction32 {
    fn from(numerator: i32) -> Self {
        Self {
            numerator,
            denominator: 1,
        }
    }
}

impl Add for Fraction32 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let (mut this, rhs) = LowestCommonDenominator::find32(self, rhs);
        let mut numerator = this.numerator + rhs.numerator;
        reduce(&mut numerator, &mut this.denominator);
        Self {
            numerator,
            denominator: this.denominator,
        }
    }
}

impl Sub for Fraction32 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let (mut this, rhs) = LowestCommonDenominator::find32(self, rhs);
        let mut numerator = this.numerator - rhs.numerator;
        reduce(&mut numerator, &mut this.denominator);
        Self {
            numerator,
            denominator: this.denominator,
        }
    }
}

impl Mul for Fraction32 {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let mut numerator = self.numerator * rhs.numerator;
        let mut denominator = self.denominator * rhs.denominator;
        reduce(&mut numerator, &mut denominator);
        Self {
            numerator,
            denominator,
        }
    }
}

impl Div for Fraction32 {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.inverse()
    }
}

impl LowestCommonDenominator {
    pub fn find(a: Fraction, b: Fraction) -> (Fraction32, Fraction32) {
        Self {
            a_factors: FactorsOf::new(a.denominator).peekable(),
            b_factors: FactorsOf::new(b.denominator).peekable(),
            a: a.into(),
            b: b.into(),
        }
        .compute()
    }

    pub fn find32(a: Fraction32, b: Fraction32) -> (Fraction32, Fraction32) {
        if a.denominator == b.denominator {
            (a, b)
        } else {
            Self {
                a_factors: FactorsOf::new(a.denominator).peekable(),
                b_factors: FactorsOf::new(b.denominator).peekable(),
                a,
                b,
            }
            .compute()
        }
    }

    fn compute(mut self) -> (Fraction32, Fraction32) {
        loop {
            match (self.a_factors.peek(), self.b_factors.peek()) {
                (Some(a_factor), Some(b_factor)) => {
                    match a_factor.cmp(b_factor) {
                        Ordering::Less => self.apply_a_factor(),
                        Ordering::Equal => {
                            // Factor is already in both.
                            self.a_factors.next();
                            self.b_factors.next();
                        }
                        Ordering::Greater => self.apply_b_factor(),
                    }
                }
                (None, Some(_)) => self.apply_b_factor(),
                (Some(_), None) => self.apply_a_factor(),
                (None, None) => break,
            }
        }

        (self.a, self.b)
    }

    fn apply_a_factor(&mut self) {
        let a_factor = self.a_factors.next().expect("just peeked");
        self.b.denominator *= i32::from(a_factor);
        self.b.numerator *= i32::from(a_factor);
    }

    fn apply_b_factor(&mut self) {
        let b_factor = self.b_factors.next().expect("just peeked");
        self.a.denominator *= i32::from(b_factor);
        self.a.numerator *= i32::from(b_factor);
    }
}

impl PartialOrd for Fraction {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Neg for Fraction {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self {
            numerator: -self.numerator,
            denominator: self.denominator,
        }
    }
}

impl Add for Fraction {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

impl AddAssign for Fraction {
    fn add_assign(&mut self, rhs: Self) {
        let (this, rhs) = LowestCommonDenominator::find(*self, rhs);
        *self = Self::from(this + rhs);
    }
}

impl Sub for Fraction {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let (this, rhs) = LowestCommonDenominator::find(self, rhs);
        Self::from(this - rhs)
    }
}

impl SubAssign for Fraction {
    fn sub_assign(&mut self, rhs: Self) {
        let (this, rhs) = LowestCommonDenominator::find(*self, rhs);
        *self = Self::from(this - rhs);
    }
}

impl Mul for Fraction {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        // Expand to 32-bits for the multiplication, then reduce.
        let numerator = i32::from(self.numerator) * i32::from(rhs.numerator);
        let denominator = i32::from(self.denominator) * i32::from(rhs.denominator);
        Self::from(Fraction32 {
            numerator,
            denominator,
        })
    }
}

impl MulAssign for Fraction {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl Mul<Fraction> for i32 {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn mul(self, rhs: Fraction) -> Self::Output {
        let temporary = i64::from(self) * i64::from(rhs.numerator) / i64::from(rhs.denominator);
        temporary
            .clamp(i64::from(i32::MIN), i64::from(i32::MAX))
            .cast()
    }
}

impl DivAssign for Fraction {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl Mul<Fraction> for u32 {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn mul(self, rhs: Fraction) -> Self::Output {
        if let (Ok(numerator), Ok(denominator)) =
            (u32::try_from(rhs.numerator), u32::try_from(rhs.denominator))
        {
            let temporary = u64::from(self) * u64::from(numerator) / u64::from(denominator);
            temporary.min(u64::from(u32::MAX)).cast()
        } else {
            0
        }
    }
}

impl Div for Fraction {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)] // I guess it is suspicious, lol.
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.inverse()
    }
}

impl Div<Fraction> for i32 {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)] // I guess it is suspicious, lol.
    fn div(self, rhs: Fraction) -> Self::Output {
        self * rhs.inverse()
    }
}

impl Div<Fraction> for u32 {
    type Output = Self;

    #[allow(clippy::suspicious_arithmetic_impl)] // I guess it is suspicious, lol.
    fn div(self, rhs: Fraction) -> Self::Output {
        self * rhs.inverse()
    }
}

macro_rules! impl_math_ops_for_std_type {
    ($type:ident) => {
        impl Add<$type> for Fraction {
            type Output = Fraction;

            fn add(self, rhs: $type) -> Self::Output {
                self + Self::from(rhs)
            }
        }

        impl AddAssign<$type> for Fraction {
            fn add_assign(&mut self, rhs: $type) {
                *self += Self::from(rhs);
            }
        }

        impl Sub<$type> for Fraction {
            type Output = Fraction;

            fn sub(self, rhs: $type) -> Self::Output {
                self - Self::from(rhs)
            }
        }

        impl SubAssign<$type> for Fraction {
            fn sub_assign(&mut self, rhs: $type) {
                *self -= Self::from(rhs);
            }
        }

        impl Div<$type> for Fraction {
            type Output = Fraction;

            fn div(self, rhs: $type) -> Self::Output {
                self / Self::from(rhs)
            }
        }

        impl DivAssign<$type> for Fraction {
            fn div_assign(&mut self, rhs: $type) {
                *self /= Self::from(rhs);
            }
        }

        impl Mul<$type> for Fraction {
            type Output = Fraction;

            fn mul(self, rhs: $type) -> Self::Output {
                self * Self::from(rhs)
            }
        }

        impl MulAssign<$type> for Fraction {
            fn mul_assign(&mut self, rhs: $type) {
                *self *= Self::from(rhs);
            }
        }
    };
}

impl_math_ops_for_std_type!(f32);
impl_math_ops_for_std_type!(i16);

#[test]
fn ratio_ord() {
    // Test denominators matching.
    assert!(Fraction::new(1, 3) < Fraction::new(2, 3));
    // Test mismatch in denominators but matching numerator.
    assert!(Fraction::new(1, 3) < Fraction::new(1, 2));

    // Test incompatible fractions
    assert!(Fraction::new(2, 3) > Fraction::new(1, 2));
}

#[test]
fn ratio_debug() {
    assert_eq!(format!("{:?}", Fraction::from(1. / 3.)), "Fraction(1/3)");
}

#[test]
fn pi() {
    assert_eq!(Fraction::from(std::f32::consts::PI), Fraction::PI);
}

#[test]
fn math() {
    assert_eq!(
        Fraction::new(2, 3) * Fraction::new(3, 4),
        Fraction::new(1, 2)
    );
    assert_eq!(
        Fraction::new(2, 3) / Fraction::new(2, 1),
        Fraction::new(1, 3)
    );
}
#[test]
fn lossy_simplification() {
    assert_eq!(
        fraction!(1 / 32_767) + fraction!(1 / 32_767),
        fraction!(2 / 32_767)
    );
}

#[test]
fn compound_signs() {
    assert_eq!(fraction!(-1 / 3).into_compound(), (0, Fraction::new(-1, 3)));
    assert_eq!(
        fraction!(-4 / 3).into_compound(),
        (-1, Fraction::new(-1, 3))
    );
    assert_eq!(fraction!(4 / 3).into_compound(), (1, Fraction::new(1, 3)));
}

#[test]
fn negative_denominator() {
    assert_eq!(
        fraction!(1 / -3),
        Fraction {
            numerator: -1,
            denominator: 3
        }
    );
    assert_eq!(
        fraction!(-1 / -3),
        Fraction {
            numerator: 1,
            denominator: 3
        }
    );
}