From 673149e1841b8822f621d377fafef2bf1e34e93f Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 5 Jun 2021 15:08:07 +1000 Subject: [PATCH] Code clean up --- src/numbers/native.rs | 20 ++++++++--------- src/numbers/rational_num.rs | 44 +++++++++++++++++++------------------ src/numbers/rational_rug.rs | 14 ++++++------ 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/numbers/native.rs b/src/numbers/native.rs index 99632da..5443e43 100644 --- a/src/numbers/native.rs +++ b/src/numbers/native.rs @@ -60,11 +60,11 @@ impl Num for NativeFloat64 { } impl Assign for NativeFloat64 { - fn assign(&mut self, src: Self) { self.0 = src.0 } + fn assign(&mut self, src: Self) { self.0 = src.0; } } impl Assign<&NativeFloat64> for NativeFloat64 { - fn assign(&mut self, src: &NativeFloat64) { self.0 = src.0 } + fn assign(&mut self, src: &NativeFloat64) { self.0 = src.0; } } impl From for NativeFloat64 { @@ -159,21 +159,19 @@ impl ops::Rem<&NativeFloat64> for NativeFloat64 { } impl ops::AddAssign for NativeFloat64 { - fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 } + fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; } } impl ops::SubAssign for NativeFloat64 { - fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0 } + fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; } } impl ops::MulAssign for NativeFloat64 { - fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0 } + fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0; } } impl ops::DivAssign for NativeFloat64 { - fn div_assign(&mut self, rhs: Self) { - self.0 /= &rhs.0; - } + fn div_assign(&mut self, rhs: Self) { self.0 /= &rhs.0; } } impl ops::RemAssign for NativeFloat64 { @@ -183,15 +181,15 @@ impl ops::RemAssign for NativeFloat64 { } impl ops::AddAssign<&NativeFloat64> for NativeFloat64 { - fn add_assign(&mut self, rhs: &NativeFloat64) { self.0 += &rhs.0 } + fn add_assign(&mut self, rhs: &NativeFloat64) { self.0 += &rhs.0; } } impl ops::SubAssign<&NativeFloat64> for NativeFloat64 { - fn sub_assign(&mut self, rhs: &NativeFloat64) { self.0 -= &rhs.0 } + fn sub_assign(&mut self, rhs: &NativeFloat64) { self.0 -= &rhs.0; } } impl ops::MulAssign<&NativeFloat64> for NativeFloat64 { - fn mul_assign(&mut self, rhs: &NativeFloat64) { self.0 *= &rhs.0 } + fn mul_assign(&mut self, rhs: &NativeFloat64) { self.0 *= &rhs.0; } } impl ops::DivAssign<&NativeFloat64> for NativeFloat64 { diff --git a/src/numbers/rational_num.rs b/src/numbers/rational_num.rs index 6dc0a98..d2908f5 100644 --- a/src/numbers/rational_num.rs +++ b/src/numbers/rational_num.rs @@ -17,19 +17,20 @@ use super::{Assign, From, Number}; -use num_bigint::{BigInt, ParseBigIntError}; -use num_rational::BigRational; // TODO: Can we do Ratio and combine with ibig? use num_traits::{Num, One, Signed, Zero}; use std::cmp::{Ord, Ordering, PartialEq, PartialOrd}; use std::fmt; use std::ops; +type RatioBase = num_bigint::BigInt; +type RatioType = num_rational::BigRational; + #[derive(Clone, PartialEq, PartialOrd)] -pub struct Rational(BigRational); +pub struct Rational(RatioType); impl Number for Rational { - fn new() -> Self { Self(BigRational::zero()) } + fn new() -> Self { Self(RatioType::zero()) } fn describe() -> String { "--numbers rational".to_string() } @@ -41,7 +42,7 @@ impl Number for Rational { if dps == 0 { self.0 = self.0.floor(); } else { - let factor = BigRational::from_integer(BigInt::from(10)).pow(dps as i32); + let factor = RatioType::from_integer(RatioBase::from(10)).pow(dps as i32); self.0 *= &factor; self.0 = self.0.floor(); self.0 /= factor; @@ -52,7 +53,7 @@ impl Number for Rational { if dps == 0 { self.0 = self.0.ceil(); } else { - let factor = BigRational::from_integer(BigInt::from(10)).pow(dps as i32); + let factor = RatioType::from_integer(RatioBase::from(10)).pow(dps as i32); self.0 *= &factor; self.0 = self.0.ceil(); self.0 /= factor; @@ -61,10 +62,10 @@ impl Number for Rational { } impl Num for Rational { - type FromStrRadixErr = ParseBigIntError; + type FromStrRadixErr = ::FromStrRadixErr; fn from_str_radix(str: &str, radix: u32) -> Result { - match BigInt::from_str_radix(str, radix) { - Ok(value) => Ok(Self(BigRational::from_integer(value))), + match RatioBase::from_str_radix(str, radix) { + Ok(value) => Ok(Self(RatioType::from_integer(value))), Err(err) => Err(err) } } @@ -79,13 +80,14 @@ impl Assign<&Rational> for Rational { } impl From for Rational { - fn from(n: usize) -> Self { Self(BigRational::from_integer(BigInt::from(n))) } + fn from(n: usize) -> Self { Self(RatioType::from_integer(RatioBase::from(n))) } } impl From for Rational { fn from(n: f64) -> Self { // FIXME: This is very broken! - return Self(BigRational::from_float(n).unwrap() * BigRational::from_integer(BigInt::from(100000)).round() / BigRational::from_integer(BigInt::from(100000))); + //return Self(RatioType::from_float(n).unwrap() * RatioType::from_integer(100000).round() / RatioType::from_integer(100000)); + todo!(); } } @@ -96,10 +98,10 @@ impl fmt::Display for Rational { let result = self.0.round().to_integer().to_string(); return f.write_str(&result); } else { - let base = BigInt::from(10).pow(precision as u32); + let base = RatioBase::from(10).pow(precision as u32); let mut result = (&self.0 * base).abs().round().to_integer().to_string(); - let should_add_minus = (self.0 < BigRational::zero()) && result != "0"; + let should_add_minus = (self.0 < RatioType::zero()) && result != "0"; // Add leading 0s result = format!("{0:0>1$}", result, precision + 1); @@ -121,7 +123,7 @@ impl fmt::Display for Rational { } impl One for Rational { - fn one() -> Self { Self(BigRational::one()) } + fn one() -> Self { Self(RatioType::one()) } } impl Zero for Rational { @@ -204,19 +206,19 @@ impl ops::Rem<&Rational> for Rational { } impl ops::AddAssign for Rational { - fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 } + fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; } } impl ops::SubAssign for Rational { - fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0 } + fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; } } impl ops::MulAssign for Rational { - fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0 } + fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0; } } impl ops::DivAssign for Rational { - fn div_assign(&mut self, rhs: Self) { self.0 /= &rhs.0 } + fn div_assign(&mut self, rhs: Self) { self.0 /= &rhs.0; } } impl ops::RemAssign for Rational { @@ -226,15 +228,15 @@ impl ops::RemAssign for Rational { } impl ops::AddAssign<&Rational> for Rational { - fn add_assign(&mut self, rhs: &Rational) { self.0 += &rhs.0 } + fn add_assign(&mut self, rhs: &Rational) { self.0 += &rhs.0; } } impl ops::SubAssign<&Rational> for Rational { - fn sub_assign(&mut self, rhs: &Rational) { self.0 -= &rhs.0 } + fn sub_assign(&mut self, rhs: &Rational) { self.0 -= &rhs.0; } } impl ops::MulAssign<&Rational> for Rational { - fn mul_assign(&mut self, rhs: &Rational) { self.0 *= &rhs.0 } + fn mul_assign(&mut self, rhs: &Rational) { self.0 *= &rhs.0; } } impl ops::DivAssign<&Rational> for Rational { diff --git a/src/numbers/rational_rug.rs b/src/numbers/rational_rug.rs index 720b8c7..950aba4 100644 --- a/src/numbers/rational_rug.rs +++ b/src/numbers/rational_rug.rs @@ -204,19 +204,19 @@ impl ops::Rem<&Self> for Rational { } impl ops::AddAssign for Rational { - fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 } + fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; } } impl ops::SubAssign for Rational { - fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0 } + fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0; } } impl ops::MulAssign for Rational { - fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0 } + fn mul_assign(&mut self, rhs: Self) { self.0 *= rhs.0; } } impl ops::DivAssign for Rational { - fn div_assign(&mut self, rhs: Self) { self.0 /= rhs.0 } + fn div_assign(&mut self, rhs: Self) { self.0 /= rhs.0; } } impl ops::RemAssign for Rational { @@ -226,15 +226,15 @@ impl ops::RemAssign for Rational { } impl ops::AddAssign<&Self> for Rational { - fn add_assign(&mut self, rhs: &Self) { self.0 += &rhs.0 } + fn add_assign(&mut self, rhs: &Self) { self.0 += &rhs.0; } } impl ops::SubAssign<&Self> for Rational { - fn sub_assign(&mut self, rhs: &Self) { self.0 -= &rhs.0 } + fn sub_assign(&mut self, rhs: &Self) { self.0 -= &rhs.0; } } impl ops::MulAssign<&Self> for Rational { - fn mul_assign(&mut self, rhs: &Self) { self.0 *= &rhs.0 } + fn mul_assign(&mut self, rhs: &Self) { self.0 *= &rhs.0; } } impl ops::DivAssign<&Self> for Rational {