Code clean up
This commit is contained in:
parent
830362416d
commit
673149e184
@ -60,11 +60,11 @@ impl Num for NativeFloat64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Assign 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 {
|
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<usize> for NativeFloat64 {
|
impl From<usize> for NativeFloat64 {
|
||||||
@ -159,21 +159,19 @@ impl ops::Rem<&NativeFloat64> for NativeFloat64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign 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 {
|
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 {
|
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 {
|
impl ops::DivAssign for NativeFloat64 {
|
||||||
fn div_assign(&mut self, rhs: Self) {
|
fn div_assign(&mut self, rhs: Self) { self.0 /= &rhs.0; }
|
||||||
self.0 /= &rhs.0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::RemAssign for NativeFloat64 {
|
impl ops::RemAssign for NativeFloat64 {
|
||||||
@ -183,15 +181,15 @@ impl ops::RemAssign for NativeFloat64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign<&NativeFloat64> 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 {
|
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 {
|
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 {
|
impl ops::DivAssign<&NativeFloat64> for NativeFloat64 {
|
||||||
|
@ -17,19 +17,20 @@
|
|||||||
|
|
||||||
use super::{Assign, From, Number};
|
use super::{Assign, From, Number};
|
||||||
|
|
||||||
use num_bigint::{BigInt, ParseBigIntError};
|
|
||||||
use num_rational::BigRational; // TODO: Can we do Ratio<IBig> and combine with ibig?
|
|
||||||
use num_traits::{Num, One, Signed, Zero};
|
use num_traits::{Num, One, Signed, Zero};
|
||||||
|
|
||||||
use std::cmp::{Ord, Ordering, PartialEq, PartialOrd};
|
use std::cmp::{Ord, Ordering, PartialEq, PartialOrd};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops;
|
use std::ops;
|
||||||
|
|
||||||
|
type RatioBase = num_bigint::BigInt;
|
||||||
|
type RatioType = num_rational::BigRational;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, PartialOrd)]
|
#[derive(Clone, PartialEq, PartialOrd)]
|
||||||
pub struct Rational(BigRational);
|
pub struct Rational(RatioType);
|
||||||
|
|
||||||
impl Number for Rational {
|
impl Number for Rational {
|
||||||
fn new() -> Self { Self(BigRational::zero()) }
|
fn new() -> Self { Self(RatioType::zero()) }
|
||||||
|
|
||||||
fn describe() -> String { "--numbers rational".to_string() }
|
fn describe() -> String { "--numbers rational".to_string() }
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ impl Number for Rational {
|
|||||||
if dps == 0 {
|
if dps == 0 {
|
||||||
self.0 = self.0.floor();
|
self.0 = self.0.floor();
|
||||||
} else {
|
} 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 *= &factor;
|
||||||
self.0 = self.0.floor();
|
self.0 = self.0.floor();
|
||||||
self.0 /= factor;
|
self.0 /= factor;
|
||||||
@ -52,7 +53,7 @@ impl Number for Rational {
|
|||||||
if dps == 0 {
|
if dps == 0 {
|
||||||
self.0 = self.0.ceil();
|
self.0 = self.0.ceil();
|
||||||
} else {
|
} 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 *= &factor;
|
||||||
self.0 = self.0.ceil();
|
self.0 = self.0.ceil();
|
||||||
self.0 /= factor;
|
self.0 /= factor;
|
||||||
@ -61,10 +62,10 @@ impl Number for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Num for Rational {
|
impl Num for Rational {
|
||||||
type FromStrRadixErr = ParseBigIntError;
|
type FromStrRadixErr = <RatioBase as Num>::FromStrRadixErr;
|
||||||
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
|
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
|
||||||
match BigInt::from_str_radix(str, radix) {
|
match RatioBase::from_str_radix(str, radix) {
|
||||||
Ok(value) => Ok(Self(BigRational::from_integer(value))),
|
Ok(value) => Ok(Self(RatioType::from_integer(value))),
|
||||||
Err(err) => Err(err)
|
Err(err) => Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,13 +80,14 @@ impl Assign<&Rational> for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<usize> for Rational {
|
impl From<usize> 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<f64> for Rational {
|
impl From<f64> for Rational {
|
||||||
fn from(n: f64) -> Self {
|
fn from(n: f64) -> Self {
|
||||||
// FIXME: This is very broken!
|
// 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();
|
let result = self.0.round().to_integer().to_string();
|
||||||
return f.write_str(&result);
|
return f.write_str(&result);
|
||||||
} else {
|
} 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 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
|
// Add leading 0s
|
||||||
result = format!("{0:0>1$}", result, precision + 1);
|
result = format!("{0:0>1$}", result, precision + 1);
|
||||||
@ -121,7 +123,7 @@ impl fmt::Display for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl One for Rational {
|
impl One for Rational {
|
||||||
fn one() -> Self { Self(BigRational::one()) }
|
fn one() -> Self { Self(RatioType::one()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Zero for Rational {
|
impl Zero for Rational {
|
||||||
@ -204,19 +206,19 @@ impl ops::Rem<&Rational> for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign 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 {
|
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 {
|
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 {
|
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 {
|
impl ops::RemAssign for Rational {
|
||||||
@ -226,15 +228,15 @@ impl ops::RemAssign for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign<&Rational> 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 {
|
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 {
|
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 {
|
impl ops::DivAssign<&Rational> for Rational {
|
||||||
|
@ -204,19 +204,19 @@ impl ops::Rem<&Self> for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign 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 {
|
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 {
|
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 {
|
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 {
|
impl ops::RemAssign for Rational {
|
||||||
@ -226,15 +226,15 @@ impl ops::RemAssign for Rational {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ops::AddAssign<&Self> 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 {
|
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 {
|
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 {
|
impl ops::DivAssign<&Self> for Rational {
|
||||||
|
Loading…
Reference in New Issue
Block a user