From a73a32f2adfc469c238aa6905b6e408ca44d947b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 11 Sep 2024 12:59:33 -0700 Subject: [PATCH] Divide trait --- fixed_wide/src/fixed.rs | 20 ++++++++++++++++++++ linear_ops/src/macros/matrix.rs | 7 +++++++ linear_ops/src/macros/vector.rs | 7 +++++++ ratio_ops/src/ratio.rs | 12 +++++++++--- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/fixed_wide/src/fixed.rs b/fixed_wide/src/fixed.rs index 78885b7..6a86be8 100644 --- a/fixed_wide/src/fixed.rs +++ b/fixed_wide/src/fixed.rs @@ -276,6 +276,16 @@ macro_rules! impl_divide_operator_not_const_generic { } #[cfg(all(not(feature="wide-mul"),not(feature="deferred-division")))] impl_multiplicative_operator_not_const_generic!(($struct, $trait, $method, $output ), $width); + #[cfg(all(not(feature="wide-mul"),feature="deferred-division"))] + impl ratio_ops::ratio::Divide for $struct<$width,F>{ + type Output = $output; + #[inline] + fn divide(self, other: Self) -> Self::Output { + paste::item!{ + self.[](other) + } + } + } }; } @@ -391,6 +401,16 @@ macro_rules! impl_wide_operators{ } } } + #[cfg(feature="deferred-division")] + impl ratio_ops::ratio::Divide> for Fixed<$lhs,{$lhs*32}>{ + type Output=Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>; + #[inline] + fn divide(self, other: Fixed<$rhs,{$rhs*32}>)->Self::Output{ + paste::item!{ + self.[](other) + } + } + } } } diff --git a/linear_ops/src/macros/matrix.rs b/linear_ops/src/macros/matrix.rs index ead9f0a..184b2c9 100644 --- a/linear_ops/src/macros/matrix.rs +++ b/linear_ops/src/macros/matrix.rs @@ -136,6 +136,13 @@ macro_rules! impl_matrix { #[macro_export(local_inner_macros)] macro_rules! impl_matrix_deferred_division { () => { + impl,U:Copy,V> ratio_ops::ratio::Divide for Matrix{ + type Output=Matrix; + #[inline] + fn divide(self,rhs:U)->Self::Output{ + self.map(|t|t.divide(rhs)) + } + } impl core::ops::Div for Matrix{ type Output=ratio_ops::ratio::Ratio,U>; #[inline] diff --git a/linear_ops/src/macros/vector.rs b/linear_ops/src/macros/vector.rs index 2cd63fb..b375509 100644 --- a/linear_ops/src/macros/vector.rs +++ b/linear_ops/src/macros/vector.rs @@ -170,6 +170,13 @@ macro_rules! impl_vector { #[macro_export(local_inner_macros)] macro_rules! impl_vector_deferred_division { () => { + impl,U:Copy,V> ratio_ops::ratio::Divide for Vector{ + type Output=Vector; + #[inline] + fn divide(self,rhs:U)->Self::Output{ + self.map(|t|t.divide(rhs)) + } + } impl core::ops::Div for Vector{ type Output=ratio_ops::ratio::Ratio,U>; #[inline] diff --git a/ratio_ops/src/ratio.rs b/ratio_ops/src/ratio.rs index 7f806da..fdb5f39 100644 --- a/ratio_ops/src/ratio.rs +++ b/ratio_ops/src/ratio.rs @@ -10,13 +10,19 @@ impl Ratio{ } } +/// The actual divide implementation, Div is replaced with a Ratio constructor +pub trait Divide{ + type Output; + fn divide(self,rhs:Rhs)->Self::Output; +} + impl Ratio where - Num:core::ops::Div, + Num:Divide, { #[inline] - pub fn divide(num:Num,den:Den)->>::Output{ - num/den + pub fn divide(self)->>::Output{ + self.num.divide(self.den) } }