Skip to content

How to handle Maths #1880

Closed
Closed

Description

@lukeapage
  1. We decided on strict maths going forward but there is general unease about forcing () around every calculation
  2. I don't think we want to change things massively or go back to the drawing board

See #1872

Possibility to add another case for calc which like font, with strict mode off, essentially turns strict mode on for a rule ? Too many exceptions in the future?

@seven-phases-max :
Well, there're a lot of other possibilities, e.g. ./ or require parens for division (e.g. 1/2->1/2 but (1/2)->0.5) etc...
Also, the "special cases" (e.g. properties where x/y can appear as shorthand) are not so rare (starting at padding/margin and ending with background/border-radius and eventually there can be more) so we just can't hardcode them all like it's done for font (and because of that I think that the current font "workaround" is just a temporary and quite dirty kludge that ideally should be removed too).

Activity

  1. seven-phases-max commented on Feb 17, 2014

    @seven-phases-max
    Member

    I don't think we want to change things massively or go back to the drawing board

    Yes, I suggested to start this only because if we want some "default" strict math in 3.0 we'll have to invent something less heavy than the current one (and a possibility of fixing all the problems without introducing any new --alt-strict-math option seems to be quite unreal because of hidden issues behind the font-like hardcoding solution...).

  2. lukeapage commented on Feb 17, 2014

    @lukeapage
    MemberAuthor

    I hadn't realised that its growing

    https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius

    :(

    I think at the moment I am in favour of initially expanding strictMaths to be

    • Off
    • Division
    • On

    and then for 2.0.0 setting the default to be Division

    So..

    Media Queries - if off, switch to division for sub nodes
    Font - if off, switch to division for sub node
    calc( - if off or division, switch to on for sub nodes

  3. seven-phases-max commented on Feb 17, 2014

    @seven-phases-max
    Member

    https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius

    Yep, I think they are towards to allowing "shorthand values" (i.e. x/y) in any "shorhand property"...

  4. lukeapage commented on Feb 23, 2014

    @lukeapage
    MemberAuthor

    related: #1480

  5. lukeapage commented on Sep 5, 2014

    @lukeapage
    MemberAuthor

    Another option.. process calc calls but only where the units make it possible e.g
    calc( 1% + 2%) => 3%
    calc(100% - 10 px) => unchanged

  6. seven-phases-max commented on Sep 5, 2014

    @seven-phases-max
    Member

    This will fix calc but won't fix #1627 and related stuff.
    I mean yes, calc(100% - 10 px) => unchanged could be a solution for the calc but this does not cancel the need for a less-heavy-than-parens-everywhere solution.

  7. calvinjuarez commented on Sep 5, 2014

    @calvinjuarez
    Member

    If strict maths is being revisited, I'd like to suggest that Less functions like percentage() not require extra parentheses inside them. With strict maths on, you have to double-wrap the argument. This would greatly reduce the possibility of confusion and tough-to-find bugs, since both percentage(16 / 17) and percentage((16 / 17)) would be valid.

  8. seven-phases-max commented on Sep 5, 2014

    @seven-phases-max
    Member

    @calvinjuarez V2 provides a plugin subsystem where a plugin can add an arbitrary number of functions to the environment and in this sense the core won't be able to decide if such built-in function expects a single value or an expression, i.e. it is allowed to accept 16/17, and then evaluating 16/17 before it is passed to a function would be incorrect (well, roughly speaking - it's a bit more complicated than that internally).

    In that context ./ change seems to be my favourite though I understand it would be very dramatic change (if compared to (/), not counting it is also requires a whitespace before ., e.g. 16 ./17 and not 16./17, hmm).

  9. ponychicken commented on Apr 15, 2015

    @ponychicken

    Another thing where less currently breaks valid css is background shorthand:

    background: url(image.png) 50%/300px no-repeat;
    
  10. matthew-dean commented on Apr 15, 2015

    @matthew-dean
    Member

    I think at the moment I am in favour of initially expanding strictMaths to be

    Off
    Division
    On
    and then for 2.0.0 setting the default to be Division

    Sorry I didn't respond to this before 2.0 was released. I think that's a good instinct. Naked division operators simply conflict with too much of CSS, and will conflict more and more as people use more CSS3 features. And I understand people's pushback to not require parens for all math, since it's not needed in many cases.

    @seven-phases-max

    it is allowed to accept 16/17, and then evaluating 16/17 before it is passed to a function would be incorrect

    Strictly speaking, yes that's absolutely correct. This should not be evaluated BEFORE it reaches the function, especially for a custom one. However, I think it would be smart to allow functions to opt to process arguments like this, if it makes sense. So, percentage would receive 16/17 as a text argument, and then the percentage function could make a call to some helper function to see if the text is math. In the case of percentage, the argument is not ambiguous. A CSS3 declaration is not valid here. So, other user-defined functions could operate the same way: opt to evaluate arguments for valid math equations. Therefore, it's not necessarily true that strict math "forces" a double parentheses in this case. I think to suggest that causes some pushback on the idea of strict math, that it must, as a necessity, be verbose in all cases.

  11. matthew-dean commented on Apr 15, 2015

    @matthew-dean
    Member

    Our --math options could be more like:

    • Always
    • Division (default)
    • Strict

    So, we could deprecate --strict-math=true and make it an alias for --math=strict

  12. seven-phases-max commented on Apr 15, 2015

    @seven-phases-max
    Member

    However, I think it would be smart to allow functions to opt to process arguments like this, if it makes sense.

    They are allowed already (just test how percentage(16 / 17) and percentage((16 / 17)) work with --strict-math=on).
    Though none of functions uses:

    and then the percentage function could make a call to some helper function to see if the text is math.

    simply because this way each function would have to have those ~20 extra lines of that extra-helpers-conversion-arg-checking-smart-arg-handling-stuff while the own function code is just one(!) line in most cases.

  13. matthew-dean commented on Apr 15, 2015

    @matthew-dean
    Member

    Each function has to have 20 extra lines? How do you figure?

    If percentage already works with single parentheses strict math on, then I don't understand @calvinjuarez's issue. You seemed to imply in your response that single parentheses was not achievable.

  14. 84 remaining items

  15. seven-phases-max commented on Feb 11, 2018

    @seven-phases-max
    Member

    With that addressed, does it look like it's working as you would expect?

    Yes, so far I can't imagine any other suspicious things there.

  16. added a commit that references this issue on Feb 11, 2018
  17. thany commented on Feb 11, 2018

    @thany

    @matthew-dean
    Essentially change the division operator in Less. Leading contender in the thread has been ./, which works but is a little strange to look at. \ is an unknown at this point without research [...] @thany, if you or someone else is a fan of this, then this is the work required. The last thing we want is just to move the breaks somewhere else.

    Not at all actually. I suggested the \ operator as a last resort after not getting through, trying to get LESS to do only its own maths. If a new division operator is what it takes... Well, calc() can also do addition, subtraction, and multiplication. New operators for those? I think it's impractical. A new division operator might be a solution for things like font, background, and border-radius, but it's no solution for CSS maths.

    I still think the real solution is for LESS to know about context. It should (yes, here I go again with my "should", what other word must I use?...) know that calc() is a CSS function and it should only evaluate maths that CSS can't do. But floor() doesn't exist in CSS, so that one it would have to evaluate (entirely) as to not spew out invalid CSS. I think this is mentioned before though, in different wording.

  18. matthew-dean commented on Feb 11, 2018

    @matthew-dean
    Member

    I still think the real solution is for LESS to know about context. It should (yes, here I go again with my "should", what other word must I use?...) know that calc() is a CSS function and it should only evaluate maths that CSS can't do. But floor() doesn't exist in CSS, so that one it would have to evaluate (entirely) as to not spew out invalid CSS. I think this is mentioned before though, in different wording.

    Fair enough re: \, but as far as context, based on your other posts, I can guarantee it's a far more complex problem than you're thinking that it is. You really have to boil it down to:

    12px/10px  // Is this division, or not?
    10px/1.5 // is this division, or not? 

    If you want to definitely leave / as a division operator, to mimic calc(), and not have it be ambiguous, then you absolutely need to make sure that the / is in parentheses (other than the calc() exception). That would provide predictable context.

    That suggestion at the beginning of this thread is also still a possibility, and had some strong support. Essentially, that the default setting be that all math is supported outside parentheses EXCEPT division (and again, except calc(), is is now the case from 3.0+). Maybe that is the way to go. That would allow:

    font: 10px/1.5;   // not division
    font: (10px/10);  // division, result is 1px
    font: 10px+15px/1.5;   // addition but not division, result is 25px/1.5
    font: (10px+15px/1.5);  // result is 20px

    Thing is, the result for 10px+15px/1.5 might still be hard for devs to grok, with an expression that seems to get "half-evaluated" unless it's in parentheses. If we had gone ahead and turned strict math on by default, it probably would have been fine. It's even less ambiguous. [shrug] Either way, wrapping math in some kind of context IS the way to eliminate ambiguity, and the only way for division other than changing the division operator.

    The community has to essentially decide the direction. There are viable options in this thread. Each has downsides. Each has pain points. None will have full consensus. But IMO any of them are better than the current scenario. Just gotta swallow that pill.

  19. matthew-dean commented on Jun 24, 2018

    @matthew-dean
    Member

    Last call for a decision

    So in an effort to work the impossible, I'd like to propose we do this (referring back to comments from 3 years ago.)

    Give --strict-math 3 settings

    1. off
    2. division
    3. strict (alias on for back-compat)

    To settle the debate, allow the --strict-math=division switch to do the following:

    1. Do not perform division with just / character outside of parentheses. E.g. border-radius: 55px / 25px; --> no-math (yes, that's valid CSS)
    2. Allow division with two different methods:
      a. . prefix - border-radius: 55px ./ 25px;
      b. parentheses - border-radius: (55px / 25px);
    3. Both forms are valid in parentheses - e.g. border-radius: (55px ./ 25px); would be valid

    So, as a developer, if you feel one version is not your preference, you can use the other. Some may prefer not to use parentheses; some may prefer not to use a modified division operator. Something for everyone. And no more unpleasant surprises for those new to Less using what is now a widespread syntax in CSS, with the / being used in font, background, border-radius, @media queries, CSS Grid properties, and probably more in the future.

    Next steps

    I recommend this goes into a PR as an option, and then, as discussed, gets switched as the default for a future major version

  20. added a commit that references this issue on Jun 24, 2018
  21. rjgotten commented on Jun 24, 2018

    @rjgotten
    Contributor

    @matthew-dean

    I.e. the period acts sort of like the inverse of an escape sequence?
    Not a bad idea, really...

    And all things considered, probably one of the cleanest possible solutions.

  22. matthew-dean commented on Jun 24, 2018

    @matthew-dean
    Member

    @rjgotten Got a start here: https://github.com/matthew-dean/less.js/tree/strict-math-division

    I'm still getting a weird error one of the tests, where it it seems to turn one of the dimension nodes into an operation node (and then throws an error because it can't perform an operation on an operation node. It doesn't seem to be a parsing problem because another test not running under strictMath: division works fine. Want to check it out and see if you can help?

  23. added a commit that references this issue on Jun 27, 2018
  24. matthew-dean commented on Jun 27, 2018

    @matthew-dean
    Member

    What I'd like to do is close this issue and create new issues that handle the remaining math questions. Specifically:

    1. Handling division, and the strictMath: 'division' feature.
    2. Handling math of mixed units. See: How to handle mixed unit math #3047
  25. added a commit that references this issue on Jul 7, 2018
    76c1034
  26. added 3 commits that reference this issue on Jul 11, 2018
    8938e98
    4f20273
    2d43c0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions