diff --git a/src/value/number/mod.rs b/src/value/number/mod.rs index 1b89e4c..14943ef 100644 --- a/src/value/number/mod.rs +++ b/src/value/number/mod.rs @@ -19,12 +19,25 @@ mod integer; const PRECISION: usize = 10; -#[derive(Clone, Eq, PartialEq)] +#[derive(Clone)] pub(crate) enum Number { Small(Rational64), Big(Box), } +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()), diff --git a/tests/modulo.rs b/tests/modulo.rs index 87ee618..71a30e6 100644 --- a/tests/modulo.rs +++ b/tests/modulo.rs @@ -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" +); diff --git a/tests/number.rs b/tests/number.rs index b36f236..4b93d48 100644 --- a/tests/number.rs +++ b/tests/number.rs @@ -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!(