Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible with units #4

Open
call-a3 opened this issue Sep 10, 2014 · 8 comments
Open

Incompatible with units #4

call-a3 opened this issue Sep 10, 2014 · 8 comments

Comments

@call-a3
Copy link

call-a3 commented Sep 10, 2014

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

@terkel
Copy link
Owner

terkel commented Sep 15, 2014

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;

@call-a3
Copy link
Author

call-a3 commented Sep 15, 2014

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...

@Undistraction
Copy link

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

@terkel
Copy link
Owner

terkel commented Oct 24, 2014

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

@xi
Copy link
Contributor

xi commented Jul 13, 2017

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

@airqb
Copy link

airqb commented Oct 13, 2019

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

@xi
Copy link
Contributor

xi commented Oct 13, 2019

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.

@airqb
Copy link

airqb commented Oct 13, 2019

no unit is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants