Compare commits

...

3 Commits

Author SHA1 Message Date
c5fb915a6d div_euclid 2024-10-12 10:01:06 -07:00
a274b6d232 not that important 2024-10-03 12:31:41 -07:00
218a7fbf0f more general PartialEq + PartialOrd 2024-10-03 12:27:38 -07:00
2 changed files with 32 additions and 29 deletions

View File

@ -412,7 +412,7 @@ macro_rules! impl_multiply_operator_not_const_generic {
type Output=Self; type Output=Self;
#[inline] #[inline]
fn divide(self, other: i64)->Self::Output{ fn divide(self, other: i64)->Self::Output{
Self::from_bits(self.bits/BInt::from(other)) Self::from_bits(self.bits.div_euclid(BInt::from(other)))
} }
} }
} }
@ -422,11 +422,11 @@ macro_rules! impl_divide_operator_not_const_generic {
impl<const F:usize> $struct<$width,F>{ impl<const F:usize> $struct<$width,F>{
paste::item!{ paste::item!{
#[inline] #[inline]
pub fn [<fixed_ $method>](self, other: Self) -> Self { pub fn [<fixed_ $method>](self,other:Self)->Self{
//this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!! //this only needs to be $width+F as u32/64+1 but MUH CONST GENERICS!!!!!
let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32); let lhs=self.bits.as_::<BInt::<{$width*2}>>().shl(F as u32);
let rhs=other.bits.as_::<BInt::<{$width*2}>>(); let rhs=other.bits.as_::<BInt::<{$width*2}>>();
Self::from_bits(lhs.div(rhs).as_()) Self::from_bits(lhs.div_euclid(rhs).as_())
} }
} }
} }
@ -446,28 +446,28 @@ macro_rules! impl_divide_operator_not_const_generic {
} }
macro_rules! impl_multiplicative_operator { macro_rules! impl_multiplicative_operator {
( $struct: ident, $trait: ident, $method: ident, $output: ty ) => { ( $struct: ident, $trait: ident, $method: ident, $inner_method: ident, $output: ty ) => {
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F> impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
where where
BInt::<N>:From<U>+core::ops::$trait, BInt::<N>:From<U>+core::ops::$trait,
{ {
type Output = $output; type Output = $output;
#[inline] #[inline]
fn $method(self, other: U) -> Self::Output { fn $method(self,other:U)->Self::Output{
Self::from_bits(self.bits.$method(BInt::<N>::from(other))) Self::from_bits(self.bits.$inner_method(BInt::<N>::from(other)))
} }
} }
}; };
} }
macro_rules! impl_multiplicative_assign_operator { macro_rules! impl_multiplicative_assign_operator {
( $struct: ident, $trait: ident, $method: ident ) => { ( $struct: ident, $trait: ident, $method: ident, $not_assign_method: ident ) => {
impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F> impl<const N:usize,const F:usize,U> core::ops::$trait<U> for $struct<N,F>
where where
BInt::<N>:From<U>+core::ops::$trait, BInt::<N>:From<U>+core::ops::$trait,
{ {
#[inline] #[inline]
fn $method(&mut self, other: U) { fn $method(&mut self,other:U){
self.bits.$method(BInt::<N>::from(other)); self.bits=self.bits.$not_assign_method(BInt::<N>::from(other));
} }
} }
}; };
@ -495,10 +495,10 @@ macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, MulAss
macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) ); macro_16!( impl_multiply_operator_not_const_generic, (Fixed, Mul, mul, Self) );
macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign, div) ); macro_16!( impl_multiplicative_assign_operator_not_const_generic, (Fixed, DivAssign, div_assign, div) );
macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) ); macro_16!( impl_divide_operator_not_const_generic, (Fixed, Div, div, Self) );
impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign ); impl_multiplicative_assign_operator!( Fixed, MulAssign, mul_assign, mul );
impl_multiplicative_operator!( Fixed, Mul, mul, Self ); impl_multiplicative_operator!( Fixed, Mul, mul, mul, Self );
impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign ); impl_multiplicative_assign_operator!( Fixed, DivAssign, div_assign, div_euclid );
impl_multiplicative_operator!( Fixed, Div, div, Self ); impl_multiplicative_operator!( Fixed, Div, div, div_euclid, Self );
#[cfg(feature="deferred-division")] #[cfg(feature="deferred-division")]
impl<const LHS_N:usize,const LHS_F:usize,const RHS_N:usize,const RHS_F:usize> core::ops::Div<Fixed<RHS_N,RHS_F>> for Fixed<LHS_N,LHS_F>{ impl<const LHS_N:usize,const LHS_F:usize,const RHS_N:usize,const RHS_F:usize> core::ops::Div<Fixed<RHS_N,RHS_F>> for Fixed<LHS_N,LHS_F>{
type Output=ratio_ops::ratio::Ratio<Fixed<LHS_N,LHS_F>,Fixed<RHS_N,RHS_F>>; type Output=ratio_ops::ratio::Ratio<Fixed<LHS_N,LHS_F>,Fixed<RHS_N,RHS_F>>;

View File

@ -251,32 +251,35 @@ impl_ratio_assign_operator!(RemAssign,rem_assign);
// Only implement PartialEq<Self> // Only implement PartialEq<Self>
// Rust's operators aren't actually that good // Rust's operators aren't actually that good
impl<Num,Den,T> PartialEq for Ratio<Num,Den> impl<LhsNum,LhsDen,RhsNum,RhsDen,T,U> PartialEq<Ratio<RhsNum,RhsDen>> for Ratio<LhsNum,LhsDen>
where where
Num:Copy, LhsNum:Copy,
Den:Copy, LhsDen:Copy,
Num:core::ops::Mul<Den,Output=T>, RhsNum:Copy,
T:PartialEq, RhsDen:Copy,
LhsNum:core::ops::Mul<RhsDen,Output=T>,
RhsNum:core::ops::Mul<LhsDen,Output=U>,
T:PartialEq<U>,
{ {
#[inline] #[inline]
fn eq(&self,&other:&Self)->bool{ fn eq(&self,other:&Ratio<RhsNum,RhsDen>)->bool{
(self.num*other.den).eq(&(other.num*self.den)) (self.num*other.den).eq(&(other.num*self.den))
} }
} }
impl<Num,Den> Eq for Ratio<Num,Den> impl<Num,Den> Eq for Ratio<Num,Den> where Self:PartialEq{}
where
Ratio<Num,Den>:PartialEq,
{}
impl<Num,Den,T> PartialOrd for Ratio<Num,Den> impl<LhsNum,LhsDen,RhsNum,RhsDen,T,U> PartialOrd<Ratio<RhsNum,RhsDen>> for Ratio<LhsNum,LhsDen>
where where
Num:Copy, LhsNum:Copy,
Den:Copy, LhsDen:Copy,
Num:core::ops::Mul<Den,Output=T>, RhsNum:Copy,
T:PartialOrd, RhsDen:Copy,
LhsNum:core::ops::Mul<RhsDen,Output=T>,
RhsNum:core::ops::Mul<LhsDen,Output=U>,
T:PartialOrd<U>,
{ {
#[inline] #[inline]
fn partial_cmp(&self,&other:&Self)->Option<core::cmp::Ordering>{ fn partial_cmp(&self,other:&Ratio<RhsNum,RhsDen>)->Option<core::cmp::Ordering>{
(self.num*other.den).partial_cmp(&(other.num*self.den)) (self.num*other.den).partial_cmp(&(other.num*self.den))
} }
} }