malachite_float/float/arithmetic/log_base_rational_rational_base.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::float::arithmetic::log_base_2::extended_log_base_2_of_rational;
10use crate::float::basic::extended::ExtendedFloat;
11use crate::{Float, emulate_rational_to_float_fn};
12use core::cmp::Ordering::{self, *};
13use malachite_base::num::arithmetic::traits::{CeilingLogBase2, CheckedLogBase, Sign};
14use malachite_base::num::basic::floats::PrimitiveFloat;
15use malachite_base::num::basic::integers::PrimitiveInt;
16use malachite_base::num::basic::traits::{NaN, NegativeInfinity, Zero};
17use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
18use malachite_base::num::factorization::traits::ExpressAsPower;
19use malachite_base::rounding_modes::RoundingMode::{self, *};
20use malachite_nz::natural::arithmetic::float_extras::float_can_round;
21use malachite_nz::platform::Limb;
22use malachite_q::Rational;
23
24// Returns `Some(log_base(x))` when it is rational, and `None` when it is irrational (or when the
25// inputs are too large for the check to be worthwhile). `x` must be positive and not equal to 1,
26// and `base` must be greater than 1.
27//
28// `log_base(x)` is rational exactly when `x` and `base` are both powers of a common rational `g`,
29// say `x = g^a` and `base = g^e_base`; then `log_base(x) = a / e_base`. Taking `g` to be the
30// primitive root of `base` (`base.express_as_power()`), this holds iff `x` is an integer power of
31// `g`, found by `Rational::checked_log_base` (which also covers `x < 1`, giving a negative `a`).
32//
33// Detecting these rational results up front is essential, not just an optimization: when the result
34// is exactly representable (for example `log_9(3) = 1/2`), the Ziv loop in
35// `log_base_rational_rational_base_prec_round_normal` would never terminate, because the rounding
36// test can never certify a value sitting exactly on a representable point or tie.
37//
38// `express_as_power` perfect-power-tests `base`'s numerator and denominator, which is infeasible
39// for an astronomically large (for example near-1) base, so the whole check is skipped when either
40// input exceeds `64 * prec` bits. Such an input cannot be a power of `g` with a representable
41// exponent at this precision (the common cases like `log_4(8)` involve tiny operands), so it is
42// left to the Ziv loop. The one residual gap is a representable *fractional* result with a
43// perfect-power base larger than the bound -- which would require multi-hundred-megabyte
44// commensurable `x` and `base` -- where the loop would not terminate; such inputs are far beyond
45// any realistic or testable range.
46pub(crate) fn rational_log_base_rational_rational_base(
47 x: &Rational,
48 base: &Rational,
49) -> Option<Rational> {
50 // No size cutoff: skipping the check when the result is exactly representable would leave the
51 // Ziv loop unable to terminate, and representable results exist at any input size
52 // (`log_{3^k}(3) = 1/k` whenever `k` is a power of 2). Both inputs are already materialized
53 // `Rational`s, so `express_as_power` and `checked_log_base` cost polynomial in the inputs.
54 // `express_as_power` returns `None` when `base` is not a perfect power, in which case `base`
55 // itself is `g` (with exponent 1).
56 let (root, e_base) = base.express_as_power().unwrap_or_else(|| (base.clone(), 1));
57 let a = x.checked_log_base(&root)?;
58 Some(Rational::from_signeds(a, i64::exact_from(e_base)))
59}
60
61// Computes log_base(x) for `Rational` `x` and `base`, by log_base(x) = log_2(x) / log_2(base). The
62// input `x` is positive, and `base` is greater than 1.
63//
64// Both logs are computed in the extended exponent range (see `extended_log_base_2_of_rational`) so
65// that neither operand underflows when `x` or `base` is near 1, and so that their quotient may
66// temporarily leave the representable range. The single conversion back to a `Float`, via
67// `ExtendedFloat::into_float_helper`, performs the one correctly-rounded clamp to an infinity,
68// maximum, zero, or minimum as dictated by the rounding mode. This handles both overflow (a base
69// near 1, so `log_2(base)` is tiny and the quotient is huge) and underflow (an `x` near 1, so
70// `log_2(x)` is tiny).
71fn log_base_rational_rational_base_prec_round_normal(
72 x: &Rational,
73 base: &Rational,
74 prec: u64,
75 rm: RoundingMode,
76) -> (Float, Ordering) {
77 // If x is 1, the result is 0.
78 if *x == 1u32 {
79 return (Float::ZERO, Equal);
80 }
81 // If log_base(x) is rational -- x and base are both powers of a common rational -- compute it
82 // directly. This includes exactly-representable results (which the Ziv loop could never
83 // certify) as well as non-representable rationals (cheaper and exact this way).
84 if let Some(q) = rational_log_base_rational_rational_base(x, base) {
85 return Float::from_rational_prec_round(q, prec, rm);
86 }
87 // The result is irrational, so it is never exactly representable.
88 assert_ne!(rm, Exact, "Inexact log_base_rational_rational_base");
89 // The initial slack keeps working_prec at least 7, so the working_prec - 6 below stays
90 // positive.
91 let mut working_prec = prec + 6 + prec.ceiling_log_base_2();
92 let mut increment = Limb::WIDTH;
93 loop {
94 // log_2(x), extended (finite and nonzero, since x is positive and not 1).
95 let num = extended_log_base_2_of_rational(x, working_prec);
96 // log_2(base) > 0, extended.
97 let den = extended_log_base_2_of_rational(base, working_prec);
98 // log_2(x) / log_2(base) in the extended range; cannot overflow or underflow here.
99 let quotient = num.div_prec_val_ref(&den, working_prec).0;
100 // Each log is accurate to within 2 ulps and the division adds at most 1 more, for at most 5
101 // ulps total; working_prec - 6 correct bits comfortably suffice for the rounding test.
102 if float_can_round(
103 quotient.x.significand_ref().unwrap(),
104 working_prec - 6,
105 prec,
106 rm,
107 ) {
108 // Round the mantissa to prec, then place the extended exponent, clamping once to the
109 // Float range as the rounding mode dictates.
110 let (rounded, o) = Float::from_float_prec_round(quotient.x, prec, rm);
111 let mut result = ExtendedFloat::from(rounded);
112 result.exp = result.exp.checked_add(quotient.exp).unwrap();
113 return result.into_float_helper(prec, rm, o);
114 }
115 // Increase the precision.
116 working_prec += increment;
117 increment = working_prec >> 1;
118 }
119}
120
121impl Float {
122 /// Computes $\log_b x$, where $x$ and the base $b$ are both [`Rational`]s with $b>1$, returning
123 /// a [`Float`] rounded to the specified precision and with the specified rounding mode. Both
124 /// are taken by value. An [`Ordering`] is also returned, indicating whether the rounded value
125 /// is less than, equal to, or greater than the exact value. Although `NaN`s are not comparable
126 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
127 ///
128 /// This computes $\log_2 x / \log_2 b$. Both logarithms are evaluated in an extended exponent
129 /// range, so that an $x$ or $b$ extremely close to 1 (where the logarithm is tiny) does not
130 /// lose accuracy, and the single conversion of the quotient back to a [`Float`] performs the
131 /// one correctly-rounded clamp.
132 ///
133 /// See [`RoundingMode`] for a description of the possible rounding modes.
134 ///
135 /// $$
136 /// f(x,b,p,m) = \log_b x+\varepsilon.
137 /// $$
138 /// - If $\log_b x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
139 /// 0.
140 /// - If $\log_b x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
141 /// 2^{\lfloor\log_2 |\log_b x|\rfloor-p+1}$.
142 /// - If $\log_b x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
143 /// 2^{\lfloor\log_2 |\log_b x|\rfloor-p}$.
144 ///
145 /// If the output has a precision, it is `prec`.
146 ///
147 /// Special cases:
148 /// - $f(0,b,p,m)=-\infty$
149 /// - $f(x,b,p,m)=\text{NaN}$ for $x<0$
150 /// - $f(1,b,p,m)=0$
151 /// - $f(x,b,p,m)=a/e$ when $x=g^a$, where $g$ is the primitive root of $b$ and $b=g^e$, rounded
152 /// to precision $p$; the result is exact if and only if $a/e$ is representable with precision
153 /// $p$ (for example $\log_4 8=3/2$ is exact)
154 ///
155 /// Like a logarithm of a [`Float`] with a [`Rational`] base, this can both overflow (for a base
156 /// near 1) and underflow (for an $x$ near 1).
157 ///
158 /// # Worst-case complexity
159 /// $T(n) = O(n (\log n)^2 \log\log n)$
160 ///
161 /// $M(n) = O(n (\log n)^2)$
162 ///
163 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
164 ///
165 /// # Panics
166 /// Panics if `prec` is zero, if `base` is less than or equal to 1, or if `rm` is `Exact` but
167 /// the result cannot be represented exactly with the given precision.
168 ///
169 /// # Examples
170 /// ```
171 /// use malachite_base::rounding_modes::RoundingMode::*;
172 /// use malachite_float::Float;
173 /// use malachite_q::Rational;
174 /// use std::cmp::Ordering::*;
175 ///
176 /// let (log, o) = Float::log_base_rational_rational_base_prec_round(
177 /// Rational::from(8),
178 /// Rational::from(4),
179 /// 10,
180 /// Exact,
181 /// );
182 /// assert_eq!(log.to_string(), "1.5000"); // log_4(8) = 3/2
183 /// assert_eq!(o, Equal);
184 ///
185 /// let (log, o) = Float::log_base_rational_rational_base_prec_round(
186 /// Rational::from(2),
187 /// Rational::from(3),
188 /// 10,
189 /// Floor,
190 /// );
191 /// assert_eq!(log.to_string(), "0.63086"); // log_3(2) = 0.6309...
192 /// assert_eq!(o, Less);
193 /// ```
194 #[allow(clippy::needless_pass_by_value)]
195 #[inline]
196 pub fn log_base_rational_rational_base_prec_round(
197 x: Rational,
198 base: Rational,
199 prec: u64,
200 rm: RoundingMode,
201 ) -> (Self, Ordering) {
202 Self::log_base_rational_rational_base_prec_round_ref(&x, &base, prec, rm)
203 }
204
205 /// Computes $\log_b x$, where $x$ and the base $b$ are both [`Rational`]s with $b>1$, returning
206 /// a [`Float`] rounded to the specified precision and with the specified rounding mode. Both
207 /// are taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
208 /// value is less than, equal to, or greater than the exact value.
209 ///
210 /// See [`Float::log_base_rational_rational_base_prec_round`] for details, special cases, and a
211 /// description of the rounding behavior.
212 ///
213 /// # Worst-case complexity
214 /// $T(n) = O(n (\log n)^2 \log\log n)$
215 ///
216 /// $M(n) = O(n (\log n)^2)$
217 ///
218 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
219 ///
220 /// # Panics
221 /// Panics if `prec` is zero, if `base` is less than or equal to 1, or if `rm` is `Exact` but
222 /// the result cannot be represented exactly with the given precision.
223 ///
224 /// # Examples
225 /// ```
226 /// use malachite_base::rounding_modes::RoundingMode::*;
227 /// use malachite_float::Float;
228 /// use malachite_q::Rational;
229 /// use std::cmp::Ordering::*;
230 ///
231 /// let (log, o) = Float::log_base_rational_rational_base_prec_round_ref(
232 /// &Rational::from(9),
233 /// &Rational::from(3),
234 /// 10,
235 /// Exact,
236 /// );
237 /// assert_eq!(log.to_string(), "2.0000"); // log_3(9) = 2
238 /// assert_eq!(o, Equal);
239 ///
240 /// let (log, o) = Float::log_base_rational_rational_base_prec_round_ref(
241 /// &Rational::from_signeds(1, 8),
242 /// &Rational::from(2),
243 /// 10,
244 /// Exact,
245 /// );
246 /// assert_eq!(log.to_string(), "-3.0000"); // log_2(1/8) = -3
247 /// assert_eq!(o, Equal);
248 /// ```
249 pub fn log_base_rational_rational_base_prec_round_ref(
250 x: &Rational,
251 base: &Rational,
252 prec: u64,
253 rm: RoundingMode,
254 ) -> (Self, Ordering) {
255 assert_ne!(prec, 0);
256 assert!(*base > 1u32, "Logarithm base must be greater than 1");
257 match x.sign() {
258 Less => (Self::NAN, Equal),
259 Equal => (Self::NEGATIVE_INFINITY, Equal),
260 Greater => log_base_rational_rational_base_prec_round_normal(x, base, prec, rm),
261 }
262 }
263
264 /// Computes $\log_b x$, where $x$ and the base $b$ are both [`Rational`]s with $b>1$, returning
265 /// a [`Float`] rounded to the nearest value of the specified precision. Both are taken by
266 /// value. An [`Ordering`] is also returned.
267 ///
268 /// See [`Float::log_base_rational_rational_base_prec_round`] for details and special cases.
269 ///
270 /// # Worst-case complexity
271 /// $T(n) = O(n (\log n)^2 \log\log n)$
272 ///
273 /// $M(n) = O(n (\log n)^2)$
274 ///
275 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
276 ///
277 /// # Panics
278 /// Panics if `prec` is zero or if `base` is less than or equal to 1.
279 ///
280 /// # Examples
281 /// ```
282 /// use malachite_float::Float;
283 /// use malachite_q::Rational;
284 /// use std::cmp::Ordering::*;
285 ///
286 /// let (log, o) =
287 /// Float::log_base_rational_rational_base_prec(Rational::from(8), Rational::from(4), 10);
288 /// assert_eq!(log.to_string(), "1.5000"); // log_4(8) = 3/2
289 /// assert_eq!(o, Equal);
290 /// ```
291 #[allow(clippy::needless_pass_by_value)]
292 #[inline]
293 pub fn log_base_rational_rational_base_prec(
294 x: Rational,
295 base: Rational,
296 prec: u64,
297 ) -> (Self, Ordering) {
298 Self::log_base_rational_rational_base_prec_round_ref(&x, &base, prec, Nearest)
299 }
300
301 /// Computes $\log_b x$, where $x$ and the base $b$ are both [`Rational`]s with $b>1$, returning
302 /// a [`Float`] rounded to the nearest value of the specified precision. Both are taken by
303 /// reference. An [`Ordering`] is also returned.
304 ///
305 /// See [`Float::log_base_rational_rational_base_prec_round`] for details and special cases.
306 ///
307 /// # Worst-case complexity
308 /// $T(n) = O(n (\log n)^2 \log\log n)$
309 ///
310 /// $M(n) = O(n (\log n)^2)$
311 ///
312 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
313 ///
314 /// # Panics
315 /// Panics if `prec` is zero or if `base` is less than or equal to 1.
316 ///
317 /// # Examples
318 /// ```
319 /// use malachite_float::Float;
320 /// use malachite_q::Rational;
321 /// use std::cmp::Ordering::*;
322 ///
323 /// let (log, o) = Float::log_base_rational_rational_base_prec_ref(
324 /// &Rational::from(9),
325 /// &Rational::from(3),
326 /// 10,
327 /// );
328 /// assert_eq!(log.to_string(), "2.0000"); // log_3(9) = 2
329 /// assert_eq!(o, Equal);
330 /// ```
331 #[inline]
332 pub fn log_base_rational_rational_base_prec_ref(
333 x: &Rational,
334 base: &Rational,
335 prec: u64,
336 ) -> (Self, Ordering) {
337 Self::log_base_rational_rational_base_prec_round_ref(x, base, prec, Nearest)
338 }
339}
340
341/// Computes $\log_b x$, the base-$b$ logarithm of a [`Rational`], where the base $b$ is also a
342/// [`Rational`] greater than 1, returning a primitive float result. Using this function is more
343/// accurate than computing the logarithm using the standard library, whose logarithm functions are
344/// not always correctly rounded.
345///
346/// If the logarithm is equidistant from two primitive floats, the primitive float with fewer 1s in
347/// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest` rounding
348/// mode.
349///
350/// The base-$b$ logarithm of any negative number is `NaN`.
351///
352/// $$
353/// f(x,b) = \log_b x+\varepsilon.
354/// $$
355/// - If $\log_b x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
356/// - If $\log_b x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |\log_b
357/// x|\rfloor-p}$, where $p$ is precision of the output (typically 24 if `T` is a [`f32`] and 53
358/// if `T` is a [`f64`], but less if the output is subnormal).
359///
360/// Special cases:
361/// - $f(0,b)=-\infty$
362/// - $f(x,b)=\text{NaN}$ for $x<0$
363/// - $f(1,b)=0.0$
364///
365/// Unlike a logarithm with an integer base, this function can both overflow (for a base near 1) and
366/// underflow (for an $x$ near 1).
367///
368/// # Worst-case complexity
369/// Constant time and additional memory.
370///
371/// # Panics
372/// Panics if `base` is less than or equal to 1.
373///
374/// # Examples
375/// ```
376/// use malachite_base::num::basic::traits::{NegativeInfinity, Zero};
377/// use malachite_base::num::float::NiceFloat;
378/// use malachite_float::float::arithmetic::log_base_rational_rational_base::*;
379/// use malachite_q::Rational;
380///
381/// assert_eq!(
382/// NiceFloat(primitive_float_log_base_rational_rational_base::<f32>(
383/// &Rational::ZERO,
384/// &Rational::from(10)
385/// )),
386/// NiceFloat(f32::NEGATIVE_INFINITY)
387/// );
388/// // log_4(8) = 3/2
389/// assert_eq!(
390/// NiceFloat(primitive_float_log_base_rational_rational_base::<f32>(
391/// &Rational::from(8),
392/// &Rational::from(4)
393/// )),
394/// NiceFloat(1.5)
395/// );
396/// // log_(3/2)(9/4) = 2
397/// assert_eq!(
398/// NiceFloat(primitive_float_log_base_rational_rational_base::<f32>(
399/// &Rational::from_unsigneds(9u8, 4),
400/// &Rational::from_unsigneds(3u8, 2)
401/// )),
402/// NiceFloat(2.0)
403/// );
404/// // log_10(50)
405/// assert_eq!(
406/// NiceFloat(primitive_float_log_base_rational_rational_base::<f32>(
407/// &Rational::from(50),
408/// &Rational::from(10)
409/// )),
410/// NiceFloat(1.69897)
411/// );
412/// assert_eq!(
413/// NiceFloat(primitive_float_log_base_rational_rational_base::<f32>(
414/// &Rational::from(-1000),
415/// &Rational::from(10)
416/// )),
417/// NiceFloat(f32::NAN)
418/// );
419/// ```
420#[inline]
421#[allow(clippy::type_repetition_in_bounds)]
422pub fn primitive_float_log_base_rational_rational_base<T: PrimitiveFloat>(
423 x: &Rational,
424 base: &Rational,
425) -> T
426where
427 Float: PartialOrd<T>,
428 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
429{
430 emulate_rational_to_float_fn(
431 |x, prec| Float::log_base_rational_rational_base_prec_ref(x, base, prec),
432 x,
433 )
434}