Compare commits

...

1 Commits

Author SHA1 Message Date
b2820ac05a wip 2025-02-21 14:17:21 -08:00

@ -68,54 +68,57 @@ impl_ratio_method!(Add,add,add_ratio);
impl_ratio_method!(Sub,sub,sub_ratio); impl_ratio_method!(Sub,sub,sub_ratio);
impl_ratio_method!(Rem,rem,rem_ratio); impl_ratio_method!(Rem,rem,rem_ratio);
/// Comparing two ratios needs to know the parity of the denominators /// The denominator cannot be negative
/// For signed integers this can be implemented with is_negative() /// otherwise cross-multiplying changes the comparison
pub trait Parity{ pub trait ToUnsigned{
fn parity(&self)->bool; type Unsigned;
fn to_unsigned(self)->(bool,Self::Unsigned);
} }
macro_rules! impl_parity_unsigned{ macro_rules! impl_to_unsigned_unsigned{
($($type:ty),*)=>{ ($($type:ty),*)=>{
$( $(
impl Parity for $type{ impl ToUnsigned for $type{
fn parity(&self)->bool{ type Unsigned=Self;
false fn to_unsigned(self)->(bool,Self::Unsigned){
(false,self)
} }
} }
)* )*
}; };
} }
macro_rules! impl_parity_signed{ macro_rules! impl_to_unsigned_signed{
($($type:ty),*)=>{ ($(($type:ty,$unsigned:ty)),*)=>{
$( $(
impl Parity for $type{ impl ToUnsigned for $type{
fn parity(&self)->bool{ type Unsigned=$unsigned;
self.is_negative() fn to_unsigned(self)->(bool,Self::Unsigned){
(self.is_negative(),self.unsigned_abs())
} }
} }
)* )*
}; };
} }
macro_rules! impl_parity_float{ macro_rules! impl_to_unsigned_float{
($($type:ty),*)=>{ ($($type:ty),*)=>{
$( $(
impl Parity for $type{ impl ToUnsigned for $type{
fn parity(&self)->bool{ type Unsigned=Self;
self.is_sign_negative() fn to_unsigned(self)->(bool,Self::Unsigned){
(self.is_sign_negative(),-self)
} }
} }
)* )*
}; };
} }
impl_to_unsigned_unsigned!(u8,u16,u32,u64,u128,usize);
impl_parity_unsigned!(u8,u16,u32,u64,u128,usize); impl_to_unsigned_signed!((i8,u8),(i16,u16),(i32,u32),(i64,u64),(i128,u128),(isize,usize));
impl_parity_signed!(i8,i16,i32,i64,i128,isize); impl_to_unsigned_float!(f32,f64);
impl_parity_float!(f32,f64);
macro_rules! impl_ratio_ord_method{ macro_rules! impl_ratio_ord_method{
($method:ident, $ratio_method:ident, $output:ty)=>{ ($method:ident, $ratio_method:ident, $output:ty)=>{
impl<LhsNum,LhsDen:Parity> Ratio<LhsNum,LhsDen>{ impl<LhsNum,LhsDen:ToUnsigned> Ratio<LhsNum,LhsDen>{
#[inline] #[inline]
pub fn $ratio_method<RhsNum,RhsDen:Parity,T>(self,rhs:Ratio<RhsNum,RhsDen>)->$output pub fn $ratio_method<RhsNum,RhsDen:ToUnsigned,T>(self,rhs:Ratio<RhsNum,RhsDen>)->$output
where where
LhsNum:core::ops::Mul<RhsDen,Output=T>, LhsNum:core::ops::Mul<RhsDen,Output=T>,
LhsDen:core::ops::Mul<RhsNum,Output=T>, LhsDen:core::ops::Mul<RhsNum,Output=T>,
@ -273,9 +276,9 @@ impl<Num,Den> Eq for Ratio<Num,Den> where Self:PartialEq{}
impl<LhsNum,LhsDen,RhsNum,RhsDen,T,U> PartialOrd<Ratio<RhsNum,RhsDen>> for Ratio<LhsNum,LhsDen> impl<LhsNum,LhsDen,RhsNum,RhsDen,T,U> PartialOrd<Ratio<RhsNum,RhsDen>> for Ratio<LhsNum,LhsDen>
where where
LhsNum:Copy, LhsNum:Copy,
LhsDen:Copy+Parity, LhsDen:Copy+ToUnsigned,
RhsNum:Copy, RhsNum:Copy,
RhsDen:Copy+Parity, RhsDen:Copy+ToUnsigned,
LhsNum:core::ops::Mul<RhsDen,Output=T>, LhsNum:core::ops::Mul<RhsDen,Output=T>,
LhsDen:core::ops::Mul<RhsNum,Output=T>, LhsDen:core::ops::Mul<RhsNum,Output=T>,
RhsNum:core::ops::Mul<LhsDen,Output=U>, RhsNum:core::ops::Mul<LhsDen,Output=U>,
@ -290,7 +293,7 @@ impl<LhsNum,LhsDen,RhsNum,RhsDen,T,U> PartialOrd<Ratio<RhsNum,RhsDen>> for Ratio
impl<Num,Den,T> Ord for Ratio<Num,Den> impl<Num,Den,T> Ord for Ratio<Num,Den>
where where
Num:Copy, Num:Copy,
Den:Copy+Parity, Den:Copy+ToUnsigned,
Num:core::ops::Mul<Den,Output=T>, Num:core::ops::Mul<Den,Output=T>,
Den:core::ops::Mul<Num,Output=T>, Den:core::ops::Mul<Num,Output=T>,
T:Ord, T:Ord,