wip
This commit is contained in:
parent
cd70967c08
commit
12b825bb15
1
deferred_division/.gitignore
vendored
Normal file
1
deferred_division/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
14
deferred_division/Cargo.lock
generated
Normal file
14
deferred_division/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "deferred_division"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fixed_wide_traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixed_wide_traits"
|
||||
version = "0.1.0"
|
@ -3,4 +3,9 @@ name = "deferred_division"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default=["fixed_wide_traits"]
|
||||
fixed_wide_traits=["dep:fixed_wide_traits"]
|
||||
|
||||
[dependencies]
|
||||
fixed_wide_traits = { version = "0.1.0", path = "../fixed_wide_traits", optional = true }
|
||||
|
@ -1,4 +1,8 @@
|
||||
pub mod ratio;
|
||||
|
||||
|
||||
#[cfg(feature="fixed_wide_traits")]
|
||||
mod wide;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -1,33 +1,10 @@
|
||||
#[derive(Clone,Copy,Debug,Hash)]
|
||||
pub struct Ratio<Num,Den>{
|
||||
num:Num,
|
||||
den:Den,
|
||||
pub(crate)num:Num,
|
||||
pub(crate)den:Den,
|
||||
}
|
||||
//this trait is like a constructor for Ratio
|
||||
pub trait DeferredDiv<Rhs=Self>{
|
||||
type Output;
|
||||
fn deferred_div(self,rhs:Rhs)->Self::Output;
|
||||
}
|
||||
impl<Num,Den,T> WideMul<T> for Ratio<Num,Den>
|
||||
where
|
||||
Num:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<<Num as WideMul<T>>::Output,Den>;
|
||||
fn wide_mul(self,rhs:T)->Ratio<<Num as WideMul<T>>::Output,Den>{
|
||||
Ratio{
|
||||
num:self.num.wide_mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den,T> WideDiv<T> for Ratio<Num,Den>
|
||||
where
|
||||
Den:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<Num,<Den as WideMul<T>>::Output>;
|
||||
fn wide_div(self,rhs:T)->Ratio<Num,<Den as WideMul<T>>::Output>{
|
||||
Ratio{
|
||||
num:self.num,
|
||||
den:self.den.wide_mul(rhs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
51
deferred_division/src/wide.rs
Normal file
51
deferred_division/src/wide.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::ops::{Add,Mul};
|
||||
use crate::ratio::Ratio;
|
||||
use fixed_wide_traits::wide::{WideMul,WideDiv};
|
||||
|
||||
impl<Num,Den:Copy> Ratio<Num,Den>
|
||||
{
|
||||
pub fn rational_add<T>(self,rhs:T)->Ratio<<Num as Add<<Den as Mul<T>>::Output>>::Output,Den>
|
||||
where
|
||||
Den:Mul<T>,
|
||||
Num:Add<<Den as Mul<T>>::Output>,
|
||||
{
|
||||
Ratio{
|
||||
num:self.num+self.den.mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
pub fn wide_rational_add<T>(self,rhs:T)->Ratio<<Num as Add<<Den as WideMul<T>>::Output>>::Output,Den>
|
||||
where
|
||||
Den:WideMul<T>,
|
||||
Num:Add<<Den as WideMul<T>>::Output>,
|
||||
{
|
||||
Ratio{
|
||||
num:self.num+self.den.wide_mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den,T> WideMul<T> for Ratio<Num,Den>
|
||||
where
|
||||
Num:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<<Num as WideMul<T>>::Output,Den>;
|
||||
fn wide_mul(self,rhs:T)->Ratio<<Num as WideMul<T>>::Output,Den>{
|
||||
Ratio{
|
||||
num:self.num.wide_mul(rhs),
|
||||
den:self.den,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Num,Den,T> WideDiv<T> for Ratio<Num,Den>
|
||||
where
|
||||
Den:WideMul<T>,
|
||||
{
|
||||
type Output=Ratio<Num,<Den as WideMul<T>>::Output>;
|
||||
fn wide_div(self,rhs:T)->Ratio<Num,<Den as WideMul<T>>::Output>{
|
||||
Ratio{
|
||||
num:self.num,
|
||||
den:self.den.wide_mul(rhs),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user