fixed_wide_vectors/deferred_division/src/wide.rs
2024-08-28 15:33:10 -07:00

59 lines
1.3 KiB
Rust

use std::ops::{Add,Mul};
use crate::ratio::Ratio;
use fixed_wide_traits::wide::{WideMul,WideDiv,WideDot,WideCross};
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,
}
}
}
macro_rules! impl_mul_operator {
($struct:ident,$trait:ident,$method:ident)=>{
impl<Num,Den,Rhs> $trait<Rhs> for $struct<Num,Den>
where
Num:$trait<Rhs>,
{
type Output=$struct<<Num as $trait<Rhs>>::Output,Den>;
fn $method(self,rhs:Rhs)->Self::Output{
$struct{
num:self.num.$method(rhs),
den:self.den,
}
}
}
}
}
impl_mul_operator!(Ratio,WideMul,wide_mul);
impl_mul_operator!(Ratio,WideDot,wide_dot);
impl_mul_operator!(Ratio,WideCross,wide_cross);
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),
}
}
}