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
use malachite_base::num::arithmetic::traits::{
    CeilingLogBase2, CeilingLogBasePowerOf2, CheckedLogBase2, CheckedLogBasePowerOf2, DivMod,
    DivRound, FloorLogBase2, FloorLogBasePowerOf2, Sign,
};
use malachite_base::rounding_modes::RoundingMode;
use std::cmp::Ordering;
use Rational;

impl<'a> FloorLogBasePowerOf2<i64> for &'a Rational {
    type Output = i64;

    /// Returns the floor of the base-$2^k$ logarithm of a positive [`Rational`].
    ///
    /// $k$ may be negative.
    ///
    /// $f(x, k) = \lfloor\log_{2^k} x\rfloor$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
    ///
    /// # Panics
    /// Panics if `self` is less than or equal to 0 or `pow` is 0.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::FloorLogBasePowerOf2;
    /// use malachite_q::Rational;
    ///
    /// assert_eq!(Rational::from(100).floor_log_base_power_of_2(2), 3);
    /// assert_eq!(Rational::from(4294967296u64).floor_log_base_power_of_2(8), 4);
    ///
    /// // 4^(-2) < 1/10 < 4^(-1)
    /// assert_eq!(Rational::from_signeds(1, 10).floor_log_base_power_of_2(2), -2);
    /// // (1/4)^2 < 1/10 < (1/4)^1
    /// assert_eq!(Rational::from_signeds(1, 10).floor_log_base_power_of_2(-2), 1);
    /// ```
    fn floor_log_base_power_of_2(self, pow: i64) -> i64 {
        assert!(*self > 0u32);
        match pow.sign() {
            Ordering::Equal => panic!("Cannot take base-1 logarithm"),
            Ordering::Greater => self.floor_log_base_2().div_round(pow, RoundingMode::Floor),
            Ordering::Less => {
                -(self
                    .ceiling_log_base_2()
                    .div_round(-pow, RoundingMode::Ceiling))
            }
        }
    }
}

impl<'a> CeilingLogBasePowerOf2<i64> for &'a Rational {
    type Output = i64;

    /// Returns the ceiling of the base-$2^k$ logarithm of a positive [`Rational`].
    ///
    /// $k$ may be negative.
    ///
    /// $f(x, p) = \lceil\log_{2^p} x\rceil$.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
    ///
    /// # Panics
    /// Panics if `self` is less than or equal to 0 or `pow` is 0.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CeilingLogBasePowerOf2;
    /// use malachite_q::Rational;
    ///
    /// assert_eq!(Rational::from(100).ceiling_log_base_power_of_2(2), 4);
    /// assert_eq!(Rational::from(4294967296u64).ceiling_log_base_power_of_2(8), 4);
    ///
    /// // 4^(-2) < 1/10 < 4^(-1)
    /// assert_eq!(Rational::from_signeds(1, 10).ceiling_log_base_power_of_2(2), -1);
    /// // (1/4)^2 < 1/10 < (1/4)^1
    /// assert_eq!(Rational::from_signeds(1, 10).ceiling_log_base_power_of_2(-2), 2);
    /// ```
    fn ceiling_log_base_power_of_2(self, pow: i64) -> i64 {
        assert!(*self > 0u32);
        match pow.sign() {
            Ordering::Equal => panic!("Cannot take base-1 logarithm"),
            Ordering::Greater => self
                .ceiling_log_base_2()
                .div_round(pow, RoundingMode::Ceiling),
            Ordering::Less => -self.floor_log_base_2().div_round(-pow, RoundingMode::Floor),
        }
    }
}

impl<'a> CheckedLogBasePowerOf2<i64> for &'a Rational {
    type Output = i64;

    /// Returns the base-$2^k$ logarithm of a positive [`Rational`]. If the [`Rational`] is not a
    /// power of $2^k$, then `None` is returned.
    ///
    /// $k$ may be negative.
    ///
    /// $$
    /// f(x, p) = \\begin{cases}
    ///     \operatorname{Some}(\log_{2^p} x) & \text{if} \\quad \log_{2^p} x \in \Z, \\\\
    ///     \operatorname{None} & \textrm{otherwise}.
    /// \\end{cases}
    /// $$
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
    ///
    /// # Panics
    /// Panics if `self` is 0 or `pow` is 0.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::arithmetic::traits::CheckedLogBasePowerOf2;
    /// use malachite_q::Rational;
    ///
    /// assert_eq!(Rational::from(100).checked_log_base_power_of_2(2), None);
    /// assert_eq!(Rational::from(4294967296u64).checked_log_base_power_of_2(8), Some(4));
    ///
    /// // 4^(-2) < 1/10 < 4^(-1)
    /// assert_eq!(Rational::from_signeds(1, 10).checked_log_base_power_of_2(2), None);
    /// assert_eq!(Rational::from_signeds(1, 16).checked_log_base_power_of_2(2), Some(-2));
    /// // (1/4)^2 < 1/10 < (1/4)^1
    /// assert_eq!(Rational::from_signeds(1, 10).checked_log_base_power_of_2(-2), None);
    /// assert_eq!(Rational::from_signeds(1, 16).checked_log_base_power_of_2(-2), Some(2));
    /// ```
    fn checked_log_base_power_of_2(self, pow: i64) -> Option<i64> {
        assert!(*self > 0u32);
        let log_base_2 = self.checked_log_base_2()?;
        let (pow, neg) = match pow.sign() {
            Ordering::Equal => panic!("Cannot take base-1 logarithm"),
            Ordering::Greater => (pow, false),
            Ordering::Less => (-pow, true),
        };
        let (log, rem) = log_base_2.div_mod(pow);
        if rem != 0 {
            None
        } else if neg {
            Some(-log)
        } else {
            Some(log)
        }
    }
}