From 009aa0c296d6d23d0085fe4a0adc371b88543a81 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 27 Aug 2024 13:28:53 -0700 Subject: [PATCH] ratio crate --- deferred_division/.gitignore | 1 + deferred_division/Cargo.lock | 14 ++++++++++ deferred_division/Cargo.toml | 11 ++++++++ deferred_division/src/lib.rs | 8 ++++++ deferred_division/src/ratio.rs | 10 +++++++ deferred_division/src/wide.rs | 51 ++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 deferred_division/.gitignore create mode 100644 deferred_division/Cargo.lock create mode 100644 deferred_division/Cargo.toml create mode 100644 deferred_division/src/lib.rs create mode 100644 deferred_division/src/ratio.rs create mode 100644 deferred_division/src/wide.rs 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 new file mode 100644 index 0000000..73eafc4 --- /dev/null +++ b/deferred_division/Cargo.toml @@ -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 } diff --git a/deferred_division/src/lib.rs b/deferred_division/src/lib.rs new file mode 100644 index 0000000..86c9a9d --- /dev/null +++ b/deferred_division/src/lib.rs @@ -0,0 +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 new file mode 100644 index 0000000..a6bd02d --- /dev/null +++ b/deferred_division/src/ratio.rs @@ -0,0 +1,10 @@ +#[derive(Clone,Copy,Debug,Hash)] +pub struct Ratio{ + 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; +} 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), + } + } +}