mathsass icon indicating copy to clipboard operation
mathsass copied to clipboard

Incompatible with units

Open call-a3 opened this issue 11 years ago • 8 comments

Either I'm misunderstanding the usage of these functions, or they are unusable with units such as em or px. I tried the following:

$side: 2em;
width: sqrt(pow($side, 2)/2);

which fails with the following error: node_modules/mathsass/dist/functions/pow:13: error: cannot add or subtract numbers with incompatible units

call-a3 avatar Sep 10 '14 14:09 call-a3

Every argument for math functions must be an unitless number. Except for the trigonometric functions, they allow deg unit as argument.

So you need to:

$side: 2;
width: sqrt(pow($side, 2)/2) * 1em;

terkel avatar Sep 15 '14 01:09 terkel

I can imagine why you would not implement unit compatibility to make things easier. However, every time you want to calculate some derivate of a number with a unit, you would have to strip the unit and then reattach it later on, which is kind of a hassle user-wise. The implementation you suggest has the downside of assuming I will always work with ems, which might change during development.

I know I could do

$side: 2;
$unit: 1em;
width: sqrt(pow($side, 2)/2) * $unit;

but this requires more variables and is therefor imho less clean.

Wouldn't it be nice to be able to pass the unit into the math functions directly? Think of it as a feature request ;)

My particular use case is this: I want to append an element to a div (using :after) and rotate it 45° to make an arrow-point. However, I want the diagonal of the rotated element to be exactly as high as the height of the original div. Therefor, I use pythagoras' theorem to calculate the width and height for the :after element necessary to make it the right height after rotation. The math should check out and resolve to the proper output unit (ems to ems, px to px), but now I have to strip the unit before applying the math and then reapply the unit, taking into account measures for when the unit might change during development...

call-a3 avatar Sep 15 '14 07:09 call-a3

I have to agree. Sass is built around being able to perform calculations with units.

Undistraction avatar Oct 20 '14 07:10 Undistraction

@call-a3 @Undistraction Thanks for your feedback. Let me think about it!

terkel avatar Oct 24 '14 03:10 terkel

I don't think this is possible. Sass can only express units with whole exponents, e.g. 1em * 1em / 5px. There is no way to express sqrt(2em), log(2em) or pow(2em, 0.123). (I think this is part of the reason why these functions are not in Sass itself)

I think it is possible in many cases to restructure the formular so this is actually not required. For example, the original example can be converted to this:

$side: 2em;
width: (abs($side) / sqrt(2));

Also note that the current implementations are focused on floating point numbers. This means that they are good estimations in general, but there is no guarantee that pow(pow(2em, 8), 0.5) == pow(2em, 4). It might be pow(2em, 4.00001).

Finally, I was interested about the current behavior:

  • pow(2em, $x): pow(2, $x) * pow(1em, floor($x))
  • sqrt(2em): crash
  • cos(2em): crash
  • log(2em): log(2)
  • acos(0.5em): crash

xi avatar Jul 13 '17 06:07 xi

btw wrong Result of cos(180)*100 must return -100 but it is returns "74.69988" ??!!!

airqb avatar Oct 13 '19 04:10 airqb

btw wrong Result of cos(180)*100 must return -100 but it is returns "74.69988" ??!!!

If no unit is provided, rad is assumed. cos(180deg)*100 should do the trick.

xi avatar Oct 13 '19 04:10 xi

no unit is provided.

airqb avatar Oct 13 '19 05:10 airqb