Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
GreasySlug committed Aug 31, 2024
1 parent 11831cf commit b677a4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
4 changes: 3 additions & 1 deletion crates/erg_compiler/lib/core/_erg_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from _erg_type import MutType
from _erg_float import FloatMut


class Ratio(Fraction):
FRAC_ZERO = Fraction(0)

Expand All @@ -24,7 +25,7 @@ def __new__(cls, fraction):

def try_new(numerator: int, denominator: int):
if isinstance(numerator, int) and isinstance(denominator, int):
return Ratio(numerator, denominator)
return Ratio((numerator, denominator))
else:
return Error("not an integer")

Expand Down Expand Up @@ -82,6 +83,7 @@ def __neg__(self):
def __float__(self):
return super().__float__()


class RatioMut(MutType):
value: Ratio

Expand Down
40 changes: 22 additions & 18 deletions crates/erg_compiler/ty/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ impl TryFrom<&ValueObj> for usize {
match val {
ValueObj::Int(i) => usize::try_from(*i).map_err(|_| ()),
ValueObj::Nat(n) => usize::try_from(*n).map_err(|_| ()),
ValueObj::Ratio(r) => usize::try_from(r.to_int()).map_err(|_| ()),
ValueObj::Float(f) => Ok(*f as usize),
ValueObj::Bool(b) => Ok(if *b { 1 } else { 0 }),
_ => Err(()),
Expand Down Expand Up @@ -1003,14 +1004,27 @@ impl ValueObj {
pub const fn is_num(&self) -> bool {
matches!(
self,
Self::Float(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_) | Self::Inf | Self::NegInf
Self::Float(_)
| Self::Ratio(_)
| Self::Int(_)
| Self::Nat(_)
| Self::Bool(_)
| Self::Inf
| Self::NegInf
)
}

pub const fn is_float(&self) -> bool {
matches!(
self,
Self::Float(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_)
Self::Float(_) | Self::Ratio(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_)
)
}

pub const fn is_ratio(&self) -> bool {
matches!(
self,
Self::Ratio(_) | Self::Int(_) | Self::Nat(_) | Self::Bool(_)
)
}

Expand Down Expand Up @@ -1436,32 +1450,22 @@ impl ValueObj {

pub fn try_div(self, other: Self) -> Option<Self> {
match (self, other) {
(Self::Nat(l), Self::Nat(r)) => Some(Self::Ratio(
Ratio::new(l as i64, 1) / Ratio::new(r as i64, 1),
)),
(Self::Nat(l), Self::Int(r)) => Some(Self::Ratio(
Ratio::new(l as i64, 1) / Ratio::new(r as i64, 1),
)),
(Self::Nat(l), Self::Nat(r)) => Some(Self::Ratio(Ratio::new(l as i64, r as i64))),
(Self::Nat(l), Self::Int(r)) => Some(Self::Ratio(Ratio::new(l as i64, r as i64))),
(Self::Nat(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) / r)),
(Self::Nat(l), Self::Float(r)) => {
Some(Self::Ratio(Ratio::new(l as i64, 1) / Ratio::float_new(r)))
}
(Self::Nat(l), Self::Float(r)) => Some(Self::Float(l as f64 / r)),
(Self::Int(l), Self::Nat(r)) => Some(Self::Ratio(
Ratio::new(l as i64, 1) / Ratio::new(r as i64, 1),
)),
(Self::Int(l), Self::Int(r)) => Some(Self::Ratio(
Ratio::new(l as i64, 1) / Ratio::new(r as i64, 1),
)),
(Self::Int(l), Self::Int(r)) => Some(Self::Ratio(Ratio::new(l as i64, r as i64))),
(Self::Int(l), Self::Ratio(r)) => Some(Self::Ratio(Ratio::new(l as i64, 1) / r)),
(Self::Int(l), Self::Float(r)) => {
Some(Self::Ratio(Ratio::new(l as i64, 1) / Ratio::float_new(r)))
}
(Self::Int(l), Self::Float(r)) => Some(Self::Float(l as f64 / 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::from(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)),
// TODO: x/±Inf = 0
Expand Down

0 comments on commit b677a4f

Please sign in to comment.