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;
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)
}
}
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 {
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);
}
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);
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);
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);
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)
}