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
use codec::{Decode, Encode, MaxEncodedLen};
use core::cmp::{Ord, Ordering, PartialOrd};
use num_traits::Zero;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_arithmetic::helpers_128bit;

/// A rational number represented by a `n`umerator and `d`enominator.
#[derive(Clone, Copy, Default, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Ratio {
    pub n: u128,
    pub d: u128,
}

impl Ratio {
    /// Build from a raw `n/d`. Ensures that `d > 0`.
    pub const fn new(n: u128, d: u128) -> Self {
        // reimplement `.max(1)` so this can be `const`
        let d = if d > 0 { d } else { 1 };
        Self { n, d }
    }

    /// Build from a raw `n/d`. This could lead to / 0 if not properly handled.
    pub const fn new_unchecked(n: u128, d: u128) -> Self {
        Self { n, d }
    }

    pub const fn one() -> Self {
        Self::new_unchecked(1, 1)
    }

    pub const fn is_one(&self) -> bool {
        self.d > 0 && self.n == self.d
    }

    pub const fn zero() -> Self {
        Self::new_unchecked(0, 1)
    }

    pub const fn is_zero(&self) -> bool {
        self.n == 0
    }
}

impl From<Ratio> for (u128, u128) {
    fn from(ratio: Ratio) -> (u128, u128) {
        (ratio.n, ratio.d)
    }
}

#[cfg(test)]
impl From<Ratio> for rug::Rational {
    fn from(ratio: Ratio) -> rug::Rational {
        rug::Rational::from((ratio.n, ratio.d))
    }
}

impl From<u128> for Ratio {
    fn from(n: u128) -> Self {
        Self::new(n, 1)
    }
}

impl From<(u128, u128)> for Ratio {
    fn from((n, d): (u128, u128)) -> Self {
        Self::new(n, d)
    }
}

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

// Taken from Substrate's `Rational128`.
impl Ord for Ratio {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.d == other.d {
            self.n.cmp(&other.n)
        } else if self.d.is_zero() {
            Ordering::Greater
        } else if other.d.is_zero() {
            Ordering::Less
        } else {
            let self_n = helpers_128bit::to_big_uint(self.n) * helpers_128bit::to_big_uint(other.d);
            let other_n = helpers_128bit::to_big_uint(other.n) * helpers_128bit::to_big_uint(self.d);
            self_n.cmp(&other_n)
        }
    }
}

#[cfg(feature = "std")]
impl sp_std::fmt::Debug for Ratio {
    fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
        write!(
            f,
            "Ratio({} / {} ≈ {:.8})",
            self.n,
            self.d,
            self.n as f64 / self.d as f64
        )
    }
}

#[cfg(not(feature = "std"))]
impl sp_std::fmt::Debug for Ratio {
    fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
        write!(f, "Ratio({} / {})", self.n, self.d)
    }
}