it's distinct enough for a crate

This commit is contained in:
Quaternions 2024-08-27 12:43:22 -07:00
parent 626eb7e9e2
commit cd70967c08
4 changed files with 43 additions and 29 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "deferred_division"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,4 @@
pub mod ratio;
#[cfg(test)]
mod tests;

View File

@ -0,0 +1,33 @@
#[derive(Clone,Copy,Debug,Hash)]
pub struct Ratio<Num,Den>{
num:Num,
den:Den,
}
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),
}
}
}

View File

@ -14,32 +14,3 @@ pub trait WideCross<Rhs=Self>{
type Output;
fn wide_cross(self,rhs:Rhs)->Self::Output;
}
struct Ratio<Num,Den>{
num:Num,
den: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),
}
}
}