malachite-float 0.9.2

The arbitrary-precision floating-point type Float, with efficient algorithms partially derived from MPFR.
Documentation
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::Float;
use crate::arithmetic::log_base_rational_rational_base::rational_log_base_rational_rational_base;
use crate::test_util::arithmetic::log_base::simplest_dyadic_in;
use crate::test_util::common::rounding_mode_from_rug_round;
use core::cmp::Ordering::{self, *};
use core::cmp::max;
use malachite_base::num::basic::traits::One;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_q::Rational;
use rug::float::Round;
use rug::ops::AssignRound;

// Returns `Some(log_base(x))` if the `Rational` `x` and the rug `Float` `base` (both positive, not
// 1) are commensurable. Reuses `rational_log_base_rational_rational_base` (itself oracle-checked)
// with the `log_b = -log_{1/b}` reduction for a base below 1. Balloon-safe via the same size bound.
fn rug_log_base_rational_float_base_exact(
    x: &Rational,
    base: &rug::Float,
    prec: u64,
) -> Option<Rational> {
    let mb = Float::from(base);
    let bound = prec.saturating_mul(64);
    if x.significant_bits() > bound
        || i64::from(mb.get_exponent()?).unsigned_abs() > bound
        || mb.significant_bits() > bound
    {
        return None;
    }
    let br = Rational::exact_from(&mb);
    if br > 1u32 {
        rational_log_base_rational_rational_base(x, &br, prec)
    } else {
        rational_log_base_rational_rational_base(x, &(Rational::ONE / br), prec).map(|q| -q)
    }
}

// log_base(x) = ln(x) / ln(base) for a `Rational` `x` and a rug `Float` `base`. For inputs outside
// the normal domain (x not positive, or base not finite-positive, or base 1) the result is the IEEE
// quotient of the natural logs -- always exact (0, +-infinity, or NaN) -- computed with rug. For
// the normal domain rug brackets ln(x) (from `x` rounded down and up to a rug `Float`) and ln(base)
// (native), divides the intervals (min/max over the four corners, since a base below 1 makes
// ln(base) negative), short-circuits the commensurable case, and recovers a straddled dyadic result
// past `exact_threshold`.
pub fn rug_log_base_rational_float_base_prec_round(
    x: &Rational,
    base: &rug::Float,
    prec: u64,
    rm: Round,
) -> (rug::Float, Ordering) {
    let target_prec = u32::exact_from(prec);
    let normal = *x > 0u32 && base.is_finite() && *base > 0 && *base != 1;
    if !normal {
        // The result is ln(x)/ln(base) under IEEE: 0, +-infinity, or NaN -- exact.
        let wp = max(target_prec, 64);
        let a = rug::Float::with_val(wp, &rug::Rational::from(x)).ln();
        let b = rug::Float::with_val(wp, base.ln_ref());
        let mut r = rug::Float::with_val(target_prec, 0);
        r.assign_round(&a / &b, rm);
        return (r, Equal);
    }
    // log_base(1) = 0, with the sign of 1 / ln(base).
    if *x == 1u32 {
        let mut r = rug::Float::with_val(target_prec, 0);
        if *base < 1 {
            r = -r;
        }
        return (r, Equal);
    }
    if let Some(q) = rug_log_base_rational_float_base_exact(x, base, prec) {
        let (l, o) = Float::from_rational_prec_round(q, prec, rounding_mode_from_rug_round(rm));
        return (rug::Float::exact_from(&l), o);
    }
    let rug_x = rug::Rational::from(x);
    let mut working_prec = (prec << 1) + 128 + (x.significant_bits() << 1);
    let exact_threshold = (prec << 1) + 512;
    loop {
        let wp = u32::exact_from(working_prec);
        // x_lo <= x <= x_hi, so (monotonic) a_lo <= ln(x) <= a_hi
        let mut x_lo = rug::Float::with_val(wp, 0);
        x_lo.assign_round(&rug_x, Round::Down);
        let mut x_hi = rug::Float::with_val(wp, 0);
        x_hi.assign_round(&rug_x, Round::Up);
        let mut a_lo = rug::Float::with_val(wp, 0);
        a_lo.assign_round(x_lo.ln_ref(), Round::Down);
        let mut a_hi = rug::Float::with_val(wp, 0);
        a_hi.assign_round(x_hi.ln_ref(), Round::Up);
        // b_lo <= ln(base) <= b_hi (same nonzero sign: positive for base > 1, negative for base <
        // 1)
        let mut b_lo = rug::Float::with_val(wp, 0);
        b_lo.assign_round(base.ln_ref(), Round::Down);
        let mut b_hi = rug::Float::with_val(wp, 0);
        b_hi.assign_round(base.ln_ref(), Round::Up);
        // q_lo <= ln(x) / ln(base) <= q_hi, via the min/max of the four corner quotients.
        let mut q_lo: Option<rug::Float> = None;
        let mut q_hi: Option<rug::Float> = None;
        for a in [&a_lo, &a_hi] {
            for b in [&b_lo, &b_hi] {
                let mut lo = rug::Float::with_val(wp, 0);
                lo.assign_round(a / b, Round::Down);
                let mut hi = rug::Float::with_val(wp, 0);
                hi.assign_round(a / b, Round::Up);
                q_lo = Some(match q_lo {
                    Some(q) => {
                        if lo < q {
                            lo
                        } else {
                            q
                        }
                    }
                    None => lo,
                });
                q_hi = Some(match q_hi {
                    Some(q) => {
                        if hi > q {
                            hi
                        } else {
                            q
                        }
                    }
                    None => hi,
                });
            }
        }
        let q_lo = q_lo.unwrap();
        let q_hi = q_hi.unwrap();
        let mut l_lo = rug::Float::with_val(target_prec, 0);
        let mut o_lo = l_lo.assign_round(&q_lo, rm);
        let mut l_hi = rug::Float::with_val(target_prec, 0);
        let mut o_hi = l_hi.assign_round(&q_hi, rm);
        if o_lo == Equal {
            o_lo = o_hi;
        }
        if o_hi == Equal {
            o_hi = o_lo;
        }
        if l_lo == l_hi && o_lo == o_hi {
            return (l_lo, o_lo);
        }
        if working_prec > exact_threshold {
            let lo = Rational::try_from(&Float::from(&q_lo)).unwrap();
            let hi = Rational::try_from(&Float::from(&q_hi)).unwrap();
            let (l, o) = Float::from_rational_prec_round(
                simplest_dyadic_in(&lo, &hi),
                prec,
                rounding_mode_from_rug_round(rm),
            );
            return (rug::Float::exact_from(&l), o);
        }
        working_prec += working_prec >> 1;
    }
}

pub fn rug_log_base_rational_float_base_prec(
    x: &Rational,
    base: &rug::Float,
    prec: u64,
) -> (rug::Float, Ordering) {
    rug_log_base_rational_float_base_prec_round(x, base, prec, Round::Nearest)
}