diff --git a/src/builtin/modules/math.rs b/src/builtin/modules/math.rs index 659708b..838ee78 100644 --- a/src/builtin/modules/math.rs +++ b/src/builtin/modules/math.rs @@ -225,7 +225,7 @@ fn log(mut args: CallArgs, _: &mut Parser) -> SassResult { if base.is_zero() { Some(Number::zero()) } else { - (|| Some(number.ln()? / base.ln()?))() + number.log(base) } } else if number.is_negative() { None diff --git a/src/value/number/mod.rs b/src/value/number/mod.rs index 14943ef..f0e7bee 100644 --- a/src/value/number/mod.rs +++ b/src/value/number/mod.rs @@ -29,8 +29,12 @@ 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), 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, } } @@ -135,7 +139,7 @@ impl Number { } #[allow(clippy::cast_precision_loss)] - fn as_float(self) -> Option { + pub fn as_float(self) -> Option { Some(match self { Number::Small(n) => ((*n.numer() as f64) / (*n.denom() as f64)), Number::Big(n) => ((n.numer().to_f64()?) / (n.denom().to_f64()?)), @@ -154,6 +158,12 @@ impl Number { )?))) } + pub fn log(self, base: Number) -> Option { + Some(Number::Big(Box::new(BigRational::from_float( + self.as_float()?.log(base.as_float()?), + )?))) + } + pub fn pow(self, exponent: Self) -> Option { Some(Number::Big(Box::new(BigRational::from_float( self.as_float()?.powf(exponent.as_float()?), diff --git a/tests/math-module.rs b/tests/math-module.rs index 6f3f27a..10d7573 100644 --- a/tests/math-module.rs +++ b/tests/math-module.rs @@ -598,3 +598,12 @@ test!( "@use 'sass:math';\na {\n color: math.div(\"1\",\"2\");\n}\n", "a {\n color: \"1\"/\"2\";\n}\n" ); +test!( + log_returns_whole_number_for_simple_base, + "@use 'sass:math'; + a { + color: math.log(8, 2); + color: math.floor(math.log(8, 2)); + }", + "a {\n color: 3;\n color: 3;\n}\n" +);