use crate::Rational;
use alloc::vec::Vec;
use malachite_base::num::arithmetic::traits::{Abs, Floor, UnsignedAbs};
use malachite_base::num::conversion::traits::PowerOf2Digits;
use malachite_nz::natural::Natural;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RationalPowerOf2Digits {
log_base: u64,
remainder: Rational,
}
impl Iterator for RationalPowerOf2Digits {
type Item = Natural;
fn next(&mut self) -> Option<Natural> {
if self.remainder == 0u32 {
None
} else {
self.remainder <<= self.log_base;
let digit = (&self.remainder).floor().unsigned_abs();
self.remainder -= Rational::from(&digit);
Some(digit)
}
}
}
impl Rational {
pub fn power_of_2_digits(&self, log_base: u64) -> (Vec<Natural>, RationalPowerOf2Digits) {
let mut remainder = self.abs();
let floor = (&remainder).floor().unsigned_abs();
remainder -= Self::from(&floor);
(
floor.to_power_of_2_digits_asc(log_base),
RationalPowerOf2Digits {
log_base,
remainder,
},
)
}
}