manually impl eq for Number, rather than derive

This commit is contained in:
Connor Skees 2021-07-25 16:12:47 -04:00
parent 1a72f06f78
commit 1c553fd8dc
3 changed files with 40 additions and 1 deletions

View File

@ -19,12 +19,25 @@ mod integer;
const PRECISION: usize = 10;
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone)]
pub(crate) enum Number {
Small(Rational64),
Big(Box<BigRational>),
}
impl PartialEq for Number {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Number::Small(val1), Number::Small(val2)) => val1 == val2,
(Number::Big(val1), val2 @ Number::Small(..)) => **val1 == val2.clone().into_big_rational(),
(val1 @ Number::Small(..), Number::Big(val2)) => val1.clone().into_big_rational() == **val2,
(Number::Big(val1), Number::Big(val2)) => val1 == val2,
}
}
}
impl Eq for Number {}
impl Number {
pub const fn new_small(val: Rational64) -> Number {
Number::Small(val)
@ -34,6 +47,17 @@ impl Number {
Number::Big(Box::new(val))
}
fn into_big_rational(self) -> BigRational {
match self {
Number::Small(small) => {
let tuple: (i64, i64) = small.into();
BigRational::new_raw(BigInt::from(tuple.0), BigInt::from(tuple.1))
}
Number::Big(big) => *big,
}
}
pub fn to_integer(&self) -> Integer {
match self {
Self::Small(val) => Integer::Small(val.to_integer()),

View File

@ -85,3 +85,8 @@ test!(
"a {\n color: -99999990000099999999999999 % 2;\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
big_int_result_is_equal_to_small_int,
"a {\n color: (6 % 2) == 0;\n}\n",
"a {\n color: true;\n}\n"
);

View File

@ -179,6 +179,16 @@ test!(
"a {\n color: (999999999999999999 / .1);\n}\n",
"a {\n color: 9999999999999999990;\n}\n"
);
test!(
bigint_is_equal_to_smallint,
"$a: 99999990000099999999999999 - 99999990000099999999999999;
a {
color: $a;
color: $a == 0;
}",
"a {\n color: 0;\n color: true;\n}\n"
);
// we use arbitrary precision, so it is necessary to limit the size of exponents
// in order to prevent hangs
error!(