Skip to content

Commit

Permalink
feat(ratio): implement mod(%)
Browse files Browse the repository at this point in the history
  • Loading branch information
GreasySlug committed Aug 30, 2024
1 parent 71c274e commit ceb391a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions crates/erg_compiler/lib/core/_erg_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def __sub__(self, other):
def __mul__(self, other):
return then__(super().__mul__(other), Ratio)

def __mod__(self, other):
return then__(super().__mod__(other), Ratio)

def __truediv__(self, other):
return then__(super().__truediv__(other), Ratio)

Expand Down Expand Up @@ -151,6 +154,12 @@ def __mul__(self, other):
else:
return RatioMut(self.value * other)

def __mod__(self, other):
if isinstance(other, MutType):
return RatioMut(self.value % other.value)
else:
return RatioMut(self.value % other)

def __floordiv__(self, other):
if isinstance(other, MutType):
return RatioMut(self.value // other.value)
Expand Down
13 changes: 7 additions & 6 deletions crates/erg_compiler/ty/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,18 +1520,19 @@ impl ValueObj {
match (self, other) {
(Self::Nat(l), Self::Nat(r)) => Some(Self::Nat(l % r)),
(Self::Nat(l), Self::Int(r)) => Some(Self::Int(l as i32 % r)),
// (Self::Nat(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) % r)),
(Self::Nat(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) % r)),
(Self::Nat(l), Self::Float(r)) => Some(Self::Float(l as f64 % r)),
(Self::Int(l), Self::Nat(r)) => Some(Self::Int(l % r as i32)),
(Self::Int(l), Self::Int(r)) => Some(Self::Int(l % r)),
// (Self::Int(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) % r)),
(Self::Int(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) % r)),
(Self::Int(l), Self::Float(r)) => Some(Self::Float(l as f64 % r)),
// (Self::Ratio(l), Self::Nat(r)) => Some(Self::from(l % Ratio::new(r as i64, 1))),
// (Self::Ratio(l), Self::Int(r)) => Some(Self::from(l % Ratio::new(r as i64, 1))),
// (Self::Ratio(l), Self::Ratio(r)) => Some(Self::from(l % r)),
// (Self::Ratio(l), Self::Float(r)) => Some(Self::from(l.to_float() > r)),
(Self::Ratio(l), Self::Nat(r)) => Some(Self::Ratio(l % Ratio::new(r as i64, 1))),
(Self::Ratio(l), Self::Int(r)) => Some(Self::Ratio(l % Ratio::new(r as i64, 1))),
(Self::Ratio(l), Self::Ratio(r)) => Some(Self::Ratio(l % r)),
(Self::Ratio(l), Self::Float(r)) => Some(Self::Float(l.to_float() % r)),
(Self::Float(l), Self::Nat(r)) => Some(Self::Float(l % r as f64)),
(Self::Float(l), Self::Int(r)) => Some(Self::Float(l % r as f64)),
(Self::Float(l), Self::Ratio(r)) => Some(Self::Float(l % r.to_float())),
(Self::Float(l), Self::Float(r)) => Some(Self::Float(l % r)),
_ => None,
}
Expand Down

0 comments on commit ceb391a

Please sign in to comment.