2021-05-28 11:58:40 +02:00
|
|
|
/* OpenTally: Open-source election vote counting
|
|
|
|
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use num_traits::{Num, NumAssignRef, NumRef, One, Zero};
|
|
|
|
use rug::{self, Assign, ops::Pow, rational::ParseRationalError};
|
|
|
|
|
|
|
|
use std::cmp::{Ordering, PartialEq, PartialOrd};
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::fmt;
|
|
|
|
use std::ops;
|
|
|
|
|
|
|
|
//pub trait Number: NumRef + NumAssignRef + PartialOrd + Assign + Clone + fmt::Display where for<'a> &'a Self: RefNum<&'a Self> {
|
|
|
|
pub trait Number: NumRef + NumAssignRef + ops::Neg<Output=Self> + PartialOrd + Assign + Clone + fmt::Display where for<'a> Self: Assign<&'a Self>{
|
|
|
|
fn new() -> Self;
|
|
|
|
fn from(n: usize) -> Self;
|
|
|
|
|
|
|
|
fn floor_mut(&mut self);
|
|
|
|
|
|
|
|
fn parse(s: &str) -> Self {
|
|
|
|
if let Ok(value) = Self::from_str_radix(s, 10) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
panic!("Syntax Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Rational(rug::Rational);
|
|
|
|
|
|
|
|
impl Number for Rational {
|
|
|
|
fn new() -> Self { Self(rug::Rational::new()) }
|
|
|
|
|
|
|
|
fn from(n: usize) -> Self { Self(rug::Rational::from(n)) }
|
|
|
|
|
|
|
|
fn floor_mut(&mut self) { self.0.floor_mut() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Num for Rational {
|
|
|
|
type FromStrRadixErr = ParseRationalError;
|
|
|
|
|
|
|
|
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
|
|
|
|
match rug::Rational::parse_radix(str, radix.try_into().unwrap()) {
|
|
|
|
Ok(value) => Ok(Self(rug::Rational::from(value))),
|
|
|
|
Err(err) => Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Assign for Rational {
|
|
|
|
fn assign(&mut self, src: Self) { self.0.assign(src.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Assign<&Rational> for Rational {
|
|
|
|
fn assign(&mut self, src: &Rational) { self.0.assign(&src.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Rational {
|
|
|
|
fn clone(&self) -> Self { Self(self.0.clone()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Rational {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
if let Some(precision) = f.precision() {
|
|
|
|
if precision == 0 {
|
|
|
|
let result = rug::Integer::from((&self.0).round_ref()).to_string();
|
|
|
|
return f.write_str(&result);
|
|
|
|
} else {
|
|
|
|
let base = rug::Rational::from(10).pow(precision as u32);
|
|
|
|
let mut result = rug::Integer::from((&self.0 * base).round_ref()).to_string();
|
|
|
|
|
|
|
|
// Add leading 0s
|
|
|
|
result = format!("{0:0>1$}", result, precision + 1);
|
|
|
|
|
|
|
|
// Add the decimal point
|
|
|
|
result.insert(result.len() - precision, '.');
|
|
|
|
|
|
|
|
return f.write_str(&result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return self.0.fmt(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl One for Rational {
|
|
|
|
fn one() -> Self { Rational(rug::Rational::from(1)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Zero for Rational {
|
|
|
|
fn zero() -> Self { Self::new() }
|
|
|
|
fn is_zero(&self) -> bool { self.0 == rug::Rational::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Rational {
|
|
|
|
fn eq(&self, _other: &Self) -> bool {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Rational {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.0.partial_cmp(&other.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Neg for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn neg(self) -> Self::Output { Rational(-self.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn add(self, _rhs: Self) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn sub(self, _rhs: Self) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn mul(self, _rhs: Self) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Div for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn div(self, _rhs: Self) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Rem for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
|
|
|
|
fn rem(self, _rhs: Self) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<&Rational> for Rational {
|
|
|
|
type Output = Rational;
|
2021-05-28 14:37:18 +02:00
|
|
|
fn add(self, rhs: &Rational) -> Self::Output { Rational(self.0 + &rhs.0) }
|
2021-05-28 11:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<&Rational> for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn sub(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<&Rational> for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn mul(self, rhs: &Rational) -> Self::Output { Rational(self.0 * &rhs.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Div<&Rational> for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn div(self, rhs: &Rational) -> Self::Output { Rational(self.0 / &rhs.0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Rem<&Rational> for Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn rem(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::AddAssign for Rational {
|
|
|
|
fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::SubAssign for Rational {
|
|
|
|
fn sub_assign(&mut self, _rhs: Self) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::MulAssign for Rational {
|
|
|
|
fn mul_assign(&mut self, _rhs: Self) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::DivAssign for Rational {
|
|
|
|
fn div_assign(&mut self, rhs: Self) {
|
|
|
|
self.0 /= &rhs.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::RemAssign for Rational {
|
|
|
|
fn rem_assign(&mut self, _rhs: Self) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::AddAssign<&Rational> for Rational {
|
|
|
|
fn add_assign(&mut self, rhs: &Rational) {
|
|
|
|
self.0 += &rhs.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::SubAssign<&Rational> for Rational {
|
|
|
|
fn sub_assign(&mut self, _rhs: &Rational) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::MulAssign<&Rational> for Rational {
|
|
|
|
fn mul_assign(&mut self, _rhs: &Rational) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::DivAssign<&Rational> for Rational {
|
|
|
|
fn div_assign(&mut self, _rhs: &Rational) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::RemAssign<&Rational> for Rational {
|
|
|
|
fn rem_assign(&mut self, _rhs: &Rational) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<&Rational> for &Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn add(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<&Rational> for &Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn sub(self, rhs: &Rational) -> Self::Output {
|
|
|
|
return Rational(rug::Rational::from(&self.0 - &rhs.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<&Rational> for &Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn mul(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Div<&Rational> for &Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn div(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Rem<&Rational> for &Rational {
|
|
|
|
type Output = Rational;
|
|
|
|
fn rem(self, _rhs: &Rational) -> Self::Output {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
impl ops::Add<&&Rational> for &Rational {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<&&Rational> for &Rational {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<&&Rational> for &Rational {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Div<&&Rational> for &Rational {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Rem<&&Rational> for &Rational {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub type NumType = Rational;
|