-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Result of multiplication & division by % values off by 100× #1366
Comments
Its not specific to 1.4, its always been like that (testing on http://less2css.org/) It is related to the fact we ignore units - I wish it wasn't a breaking change.. because I agree |
Ah. Thanks. I dunno why I'd assumed percentages would be an exception to this. Guess it was just the first time I'd come across an unintuitive result. |
I would like to keep this open.. I think you are right.. |
Could we not add another initialization flag, in the vein of strictUnits? That way, if false (the default to begin with), it would not break any existing code. Additionally, the expression |
We should also weigh in the fact that CSS |
With strictMath, we already side-step that issue somewhat. Although Another few options for getting around this: 1) avoid evaluating expressions, even when stored in variables, until required (such as when being passed into a defined function, which calc is not), or 2) make "expressions" the primary unit of storing and passing arithmetical notation, which would likely require a major rewrite of the internals, but could also simplify something like tree.Dimension such that the unit would be a single string rather than an object containing numerator and denominator lists. |
We're thinking about this incorrectly. "%" is a valid unit designation, meaning that you are multiplying the total amount of units against your multiplier value. 50% * 10 = 500% for LESS, not 5 (sans unit). That's true because % is a CSS unit. Changing this behavior for % as units would break functionality for percentage units. You cannot infer that the user wanted to abandon the units, and wanted to instead interpret % as a math operation. The author may legitimately want to have 5x the percentage value, not reduce value based on a mathematical percentage. In the case of LESS, the % is just an arbitrary unit. Changing the behavior only for % would break consistency of CSS units, whereas to do a math operation, a user only needs to refactor as (0.5 * 10). The way calc() works is different because calc() runs in the context of the browser, so there are context values which are inferred but not visible in the equation. So, for example, this formula:
is really interpreted as:
with x being available width, and y being inherited em. The usages for % are complicated enough that we cannot convert it's value. In the case of LESS, it's not a math symbol, it's a unit value. It only converts to a math operation in the context of a browser, and it does so on an invisible variable. It's similar to viewport units, which are designated only with vh and vw, and similarly, cannot be used to divide another unitless number by 100. So, from my perspective, this is working as it should and always has. I understand why, for some, it's unexpected at first glance, because of the need to treat it as a unit within the context of LESS operations. Agree with @lucasrizoli that this should be closed. |
@matthew-dean: reviewing the CSS3 spec, you're correct, and while percentage arithmetic is fully defined for addition and subtraction, it doesn't seem to be defined for multiplication and division when the other operand has a non-percentage unit. e.g. In an ideal world, there'd be a middle ground where the LESS expression |
The way to think about percentages is as floating point values. 50% is 0.5 and 100% is 1.0. Percentages should always react the same way floats do. For example, 0.5 * 10 = 5.0. I think because percentages are represented as whole numbers rather than decimal fractions, people tend to get confused about their behaviour with other units. |
@Soviut No, that's how one would think about them if they were doing simple math. In the context of being a CSS pre-processor, the way we should think about them is percentage units. We are modifying the unit value. So, as a test of unit math, swap pw (percentage width) in for %.
Now, it's obvious that what the author wants to do is have a row that spans 5 columns which are 10 percentage units wide. It's the same type of math as viewport units:
If we interpreted % as a float and dropped the unit as a result, you'd end up with something completely different:
The result makes no sense in CSS, even though it makes sense in percentage math only if you reswap the type of unit to be represented by a %. If you want to do operations on a float value, use a float value. Don't use a CSS unit value. A % is a valid CSS unit value and must be preserved. |
I didn't mean literally interpret percentages as float units. I mentioned it as a way of letting people sort out potentially confusing percentage equations in their heads. "Think of it as a floating point amount rather than hundreds of something". |
I see. So I'll close then, shall I? |
@matthew-dean, may as well. Calculations with percentages (either in |
Okey dokes. |
I disagree. at least in strictUnits mode I'd like to do this
strictUnits ensures the units are kept mathematically correct.. and the above is mathematically correct. b/c of backwards compat I'd propose leaving non strictUnits |
p.s. I realise the above is not what the original bug said! |
I'd like to re-emphasize that LESS handling multiplication and division involving percent and another unit makes sense (since none of the applicable CSS proposals have defined behavior for this aside from a percent times a unit-less value), but that LESS shouldn't handle addition or subtraction involving percents unless both operands are percents. Thus
|
@lukeapage Your examples are reasonable, but, as you note, that's the opposite of the request made. However, I see what you're wanting to accomplish. If we multiply a percentage by a unit, it's treated as math. If we multiply a percentage by a non-unit value, we assume the % IS the CSS unit, and treat it as unit math, not percentage math. Seems alright, although I wonder if people would be confused by the different behavior. Seems weird to support such "switching", when % is a valid CSS unit. What's wrong with asking people to just use float values? If they use a |
@matthew-dean for one thing it aids compression with producing equivalent results to what the browser would accomplish. In some cases, it can do some things that a browser will not. For example, iirc, a browser is not required to interpret the attribute Other than that, percent is a special-case unit, and LESS doesn't try to protect users from a lack of understanding when it comes to CSS -- if you understand CSS, then you should expect special handling of '%' in any case. |
The reason I would enable it for strictUnits is that at the moment I think |
Can we just have an option to completely disable calculations? |
The option is there for a while. |
My two cents for the topic would be: I think the current Less behaviour for the use-case statements (both |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In LESS 1.4, it seems that the result of multiplying by a % value is 100× what it should be; dividing by a % value, 100× smaller than it should be.
For example,
foo: ( 50% * 256 )
will result infoo: 12800%
, andbar: ( 256 / 100% )
results inbar: 2.56%
.One can work around it with, say,
foo: ( 256 * ( 50% / 100 ) );
but that seems like a complicated way to get a result that, intuitively, seems straight-forward.I cannot figure out if this odd result of multiplying and dividing by percentages in LESS is a known issue or if I'm just being thick—or both.
The text was updated successfully, but these errors were encountered: