OpenTally/src/numbers/native.rs

265 lines
5.7 KiB
Rust

/* 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 super::Number;
use num_traits::{Num, One, Zero};
use rug::Assign;
use std::cmp::{Ordering, PartialEq, PartialOrd};
use std::num::ParseIntError;
use std::fmt;
use std::ops;
pub struct NativeFloat(f32);
impl Number for NativeFloat {
fn new() -> Self { Self(0.0) }
fn from(n: usize) -> Self { Self(n as f32) }
fn floor_mut(&mut self) { self.0 = self.0.floor() }
}
impl Num for NativeFloat {
type FromStrRadixErr = ParseIntError;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
match i32::from_str_radix(str, radix) {
Ok(value) => Ok(Self(value as f32)),
Err(err) => Err(err)
}
}
}
impl Assign for NativeFloat {
fn assign(&mut self, src: Self) { self.0 = src.0 }
}
impl Assign<&NativeFloat> for NativeFloat {
fn assign(&mut self, src: &NativeFloat) { self.0 = src.0 }
}
impl Clone for NativeFloat {
fn clone(&self) -> Self { Self(self.0) }
}
impl fmt::Display for NativeFloat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) }
}
impl One for NativeFloat {
fn one() -> Self { Self(1.0) }
}
impl Zero for NativeFloat {
fn zero() -> Self { Self::new() }
fn is_zero(&self) -> bool { self.0.is_zero() }
}
impl PartialEq for NativeFloat {
fn eq(&self, _other: &Self) -> bool {
todo!()
}
}
impl PartialOrd for NativeFloat {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.0.partial_cmp(&other.0) }
}
impl ops::Neg for NativeFloat {
type Output = NativeFloat;
fn neg(self) -> Self::Output { Self(-self.0) }
}
impl ops::Add for NativeFloat {
type Output = NativeFloat;
fn add(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Sub for NativeFloat {
type Output = NativeFloat;
fn sub(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Mul for NativeFloat {
type Output = NativeFloat;
fn mul(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Div for NativeFloat {
type Output = NativeFloat;
fn div(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Rem for NativeFloat {
type Output = NativeFloat;
fn rem(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl ops::Add<&NativeFloat> for NativeFloat {
type Output = NativeFloat;
fn add(self, rhs: &NativeFloat) -> Self::Output { Self(self.0 + &rhs.0) }
}
impl ops::Sub<&NativeFloat> for NativeFloat {
type Output = NativeFloat;
fn sub(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
impl ops::Mul<&NativeFloat> for NativeFloat {
type Output = NativeFloat;
fn mul(self, rhs: &NativeFloat) -> Self::Output { NativeFloat(self.0 * &rhs.0) }
}
impl ops::Div<&NativeFloat> for NativeFloat {
type Output = NativeFloat;
fn div(self, rhs: &NativeFloat) -> Self::Output { NativeFloat(self.0 / &rhs.0) }
}
impl ops::Rem<&NativeFloat> for NativeFloat {
type Output = NativeFloat;
fn rem(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
impl ops::AddAssign for NativeFloat {
fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0 }
}
impl ops::SubAssign for NativeFloat {
fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.0 }
}
impl ops::MulAssign for NativeFloat {
fn mul_assign(&mut self, _rhs: Self) {
todo!()
}
}
impl ops::DivAssign for NativeFloat {
fn div_assign(&mut self, rhs: Self) {
self.0 /= &rhs.0;
}
}
impl ops::RemAssign for NativeFloat {
fn rem_assign(&mut self, _rhs: Self) {
todo!()
}
}
impl ops::AddAssign<&NativeFloat> for NativeFloat {
fn add_assign(&mut self, rhs: &NativeFloat) { self.0 += &rhs.0 }
}
impl ops::SubAssign<&NativeFloat> for NativeFloat {
fn sub_assign(&mut self, rhs: &NativeFloat) { self.0 -= &rhs.0 }
}
impl ops::MulAssign<&NativeFloat> for NativeFloat {
fn mul_assign(&mut self, _rhs: &NativeFloat) {
todo!()
}
}
impl ops::DivAssign<&NativeFloat> for NativeFloat {
fn div_assign(&mut self, _rhs: &NativeFloat) {
todo!()
}
}
impl ops::RemAssign<&NativeFloat> for NativeFloat {
fn rem_assign(&mut self, _rhs: &NativeFloat) {
todo!()
}
}
impl ops::Neg for &NativeFloat {
type Output = NativeFloat;
fn neg(self) -> Self::Output { NativeFloat(-&self.0) }
}
impl ops::Add<&NativeFloat> for &NativeFloat {
type Output = NativeFloat;
fn add(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
impl ops::Sub<&NativeFloat> for &NativeFloat {
type Output = NativeFloat;
fn sub(self, rhs: &NativeFloat) -> Self::Output { NativeFloat(&self.0 - &rhs.0) }
}
impl ops::Mul<&NativeFloat> for &NativeFloat {
type Output = NativeFloat;
fn mul(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
impl ops::Div<&NativeFloat> for &NativeFloat {
type Output = NativeFloat;
fn div(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
impl ops::Rem<&NativeFloat> for &NativeFloat {
type Output = NativeFloat;
fn rem(self, _rhs: &NativeFloat) -> Self::Output {
todo!()
}
}
/*
impl ops::Add<&&NativeFloat> for &NativeFloat {
}
impl ops::Sub<&&NativeFloat> for &NativeFloat {
}
impl ops::Mul<&&NativeFloat> for &NativeFloat {
}
impl ops::Div<&&NativeFloat> for &NativeFloat {
}
impl ops::Rem<&&NativeFloat> for &NativeFloat {
}
*/