ratio crate
This commit is contained in:
parent
2d87e4b5cc
commit
009aa0c296
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"
|
11
deferred_division/Cargo.toml
Normal file
11
deferred_division/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
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 }
|
8
deferred_division/src/lib.rs
Normal file
8
deferred_division/src/lib.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
pub mod ratio;
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(feature="fixed_wide_traits")]
|
||||||
|
mod wide;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
10
deferred_division/src/ratio.rs
Normal file
10
deferred_division/src/ratio.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#[derive(Clone,Copy,Debug,Hash)]
|
||||||
|
pub struct Ratio<Num,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;
|
||||||
|
}
|
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