Started working on a new module - num, that will replace the current external num crate

This commit is contained in:
Egor 2024-05-27 01:20:45 +03:00
parent 7dfef8b718
commit 182f73db27
3 changed files with 76 additions and 0 deletions

View file

@ -1 +1,2 @@
pub mod num;
pub mod matrix;

40
src/num.rs Normal file
View file

@ -0,0 +1,40 @@
mod bigint;
use std::ops::*;
pub trait Signed: Neg {}
pub trait NumOps<Rhs = Self>: Sized + Add<Rhs> + Sub<Rhs> + Mul<Rhs> + Div<Rhs> {}
pub trait NumAssignOps<Rhs = Self>: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> {}
pub trait Num: PartialEq + Default + NumOps + NumAssignOps {}
macro_rules! impl_num {
($t:ty) => {
impl NumOps for $t {}
impl NumAssignOps for $t {}
impl Num for $t {}
};
}
macro_rules! impl_sign_num {
($t:ty) => {
impl_num!($t);
impl Signed for $t {}
};
}
impl_num!(u8);
impl_num!(u16);
impl_num!(u32);
impl_num!(u64);
impl_num!(u128);
impl_num!(usize);
impl_sign_num!(i8);
impl_sign_num!(i16);
impl_sign_num!(i32);
impl_sign_num!(i64);
impl_sign_num!(i128);
impl_sign_num!(isize);
impl_sign_num!(f32);
impl_sign_num!(f64);

35
src/num/bigint.rs Normal file
View file

@ -0,0 +1,35 @@
pub struct BigInt {
negative: bool,
num: BigUInt
}
pub struct BigUInt {
data: Vec<u64>
}
impl BigUInt {
pub fn new() -> Self {
Self::new_from(0)
}
pub fn new_from(num: u64) -> Self {
Self { data: vec![num] }
}
}
impl BigInt {
pub fn new() -> Self {
Self { negative: false, num: BigUInt::new() }
}
pub fn new_from(num: i64) -> Self {
let (negative, num) = if num < 0 { (true, -num as u64) }
else { (false, num as u64) };
Self { negative, num: BigUInt::new_from(num) }
}
}
#[cfg(test)]
mod tests {
}