ratio: PartialEq, Eq, PartialOrd, Ord

This commit is contained in:
Quaternions 2024-09-13 14:00:13 -07:00
parent ec82745c6d
commit 260ed0fd5c

View File

@ -175,3 +175,48 @@ macro_rules! impl_ratio_assign_operator {
impl_ratio_assign_operator!(AddAssign,add_assign);
impl_ratio_assign_operator!(SubAssign,sub_assign);
impl_ratio_assign_operator!(RemAssign,rem_assign);
// Only implement PartialEq<Self>
// Rust's operators aren't actually that good
impl<Num,Den,T> PartialEq for Ratio<Num,Den>
where
Num:Copy,
Den:Copy,
Num:core::ops::Mul<Den,Output=T>,
T:PartialEq,
{
#[inline]
fn eq(&self,&other:&Self)->bool{
(self.num*other.den).eq(&(other.num*self.den))
}
}
impl<Num,Den> Eq for Ratio<Num,Den>
where
Ratio<Num,Den>:PartialEq,
{}
impl<Num,Den,T> PartialOrd for Ratio<Num,Den>
where
Num:Copy,
Den:Copy,
Num:core::ops::Mul<Den,Output=T>,
T:PartialOrd,
{
#[inline]
fn partial_cmp(&self,&other:&Self)->Option<core::cmp::Ordering>{
(self.num*other.den).partial_cmp(&(other.num*self.den))
}
}
impl<Num,Den,T> Ord for Ratio<Num,Den>
where
Num:Copy,
Den:Copy,
Num:core::ops::Mul<Den,Output=T>,
T:Ord,
{
#[inline]
fn cmp(&self,other:&Self)->std::cmp::Ordering{
(self.num*other.den).cmp(&(other.num*self.den))
}
}