avoid creating new rational when multiplying num by small integer

This commit is contained in:
ConnorSkees 2020-05-31 05:16:31 -04:00
parent ccf33d2cfb
commit 72f62ce711

View File

@ -241,10 +241,10 @@ impl Display for Number {
let mut whole = self.to_integer().abs();
let has_decimal = self.is_decimal();
let mut frac = self.abs().fract();
let mut dec = String::with_capacity(if has_decimal { PRECISION + 1 } else { 0 });
let mut dec = String::with_capacity(if has_decimal { PRECISION } else { 0 });
if has_decimal {
for _ in 0..(PRECISION - 1) {
frac *= Self::from(10);
frac *= 10_i64;
write!(dec, "{}", frac.to_integer())?;
frac = frac.fract();
if frac.is_zero() {
@ -252,7 +252,7 @@ impl Display for Number {
}
}
if !frac.is_zero() {
let end = (frac * Self::from(10)).round().to_integer();
let end = (frac * 10_i64).round().to_integer();
if end.is_ten() {
loop {
match dec.pop() {
@ -495,6 +495,24 @@ impl Mul for Number {
}
}
impl Mul<i64> for Number {
type Output = Self;
fn mul(self, other: i64) -> Self {
match self {
Self::Machine(val1) => Self::Machine(val1 * other),
Self::Big(val1) => Self::Big(val1 * BigInt::from(other)),
}
}
}
impl MulAssign<i64> for Number {
fn mul_assign(&mut self, other: i64) {
let tmp = mem::take(self);
*self = tmp * other;
}
}
impl MulAssign for Number {
fn mul_assign(&mut self, other: Self) {
let tmp = mem::take(self);