diff --git a/deferred_division/.gitignore b/deferred_division/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/deferred_division/.gitignore @@ -0,0 +1 @@ +/target diff --git a/deferred_division/Cargo.lock b/deferred_division/Cargo.lock new file mode 100644 index 0000000..973fcc1 --- /dev/null +++ b/deferred_division/Cargo.lock @@ -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" diff --git a/deferred_division/Cargo.toml b/deferred_division/Cargo.toml index 4bd8535..73eafc4 100644 --- a/deferred_division/Cargo.toml +++ b/deferred_division/Cargo.toml @@ -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 } diff --git a/deferred_division/src/lib.rs b/deferred_division/src/lib.rs index 6786c4f..86c9a9d 100644 --- a/deferred_division/src/lib.rs +++ b/deferred_division/src/lib.rs @@ -1,4 +1,8 @@ pub mod ratio; + +#[cfg(feature="fixed_wide_traits")] +mod wide; + #[cfg(test)] mod tests; diff --git a/deferred_division/src/ratio.rs b/deferred_division/src/ratio.rs index 6ddd9d7..a6bd02d 100644 --- a/deferred_division/src/ratio.rs +++ b/deferred_division/src/ratio.rs @@ -1,33 +1,10 @@ #[derive(Clone,Copy,Debug,Hash)] pub struct Ratio{ - num:Num, - den:Den, + pub(crate)num:Num, + pub(crate)den:Den, } +//this trait is like a constructor for Ratio pub trait DeferredDiv{ type Output; fn deferred_div(self,rhs:Rhs)->Self::Output; } -impl WideMul for Ratio - where - Num:WideMul, -{ - type Output=Ratio<>::Output,Den>; - fn wide_mul(self,rhs:T)->Ratio<>::Output,Den>{ - Ratio{ - num:self.num.wide_mul(rhs), - den:self.den, - } - } -} -impl WideDiv for Ratio - where - Den:WideMul, -{ - type Output=Ratio>::Output>; - fn wide_div(self,rhs:T)->Ratio>::Output>{ - Ratio{ - num:self.num, - den:self.den.wide_mul(rhs), - } - } -} diff --git a/deferred_division/src/wide.rs b/deferred_division/src/wide.rs new file mode 100644 index 0000000..df7dcae --- /dev/null +++ b/deferred_division/src/wide.rs @@ -0,0 +1,51 @@ +use std::ops::{Add,Mul}; +use crate::ratio::Ratio; +use fixed_wide_traits::wide::{WideMul,WideDiv}; + +impl Ratio +{ + pub fn rational_add(self,rhs:T)->Ratio<>::Output>>::Output,Den> + where + Den:Mul, + Num:Add<>::Output>, + { + Ratio{ + num:self.num+self.den.mul(rhs), + den:self.den, + } + } + pub fn wide_rational_add(self,rhs:T)->Ratio<>::Output>>::Output,Den> + where + Den:WideMul, + Num:Add<>::Output>, + { + Ratio{ + num:self.num+self.den.wide_mul(rhs), + den:self.den, + } + } +} +impl WideMul for Ratio + where + Num:WideMul, +{ + type Output=Ratio<>::Output,Den>; + fn wide_mul(self,rhs:T)->Ratio<>::Output,Den>{ + Ratio{ + num:self.num.wide_mul(rhs), + den:self.den, + } + } +} +impl WideDiv for Ratio + where + Den:WideMul, +{ + type Output=Ratio>::Output>; + fn wide_div(self,rhs:T)->Ratio>::Output>{ + Ratio{ + num:self.num, + den:self.den.wide_mul(rhs), + } + } +}