Skip to content

Power/exponent expression support #684

Closed

Description

@scottkellum

Thoughts on adding native support for exponents in Sass expressions? While a simple loop can easily calculate positive integers, other values are more difficult to calculate. It can be done but not efficiently in pure Sass.

Preferred syntax would be like Ruby:

5px**2

A function could work as well:

pow(5px, 2)

Activity

  1. robwierzbowski commented on Mar 18, 2013

    @robwierzbowski

    I very much like the ruby syntax. If there's an internal conflict that makes that difficult, most people understand the caret syntax too:

    5px^2
  2. Snugug commented on Mar 18, 2013

    @Snugug

    Would that be 25px or 25pxpx?

    On Mar 18, 2013, at 3:40 PM, Rob Wierzbowski notifications@github.com wrote:

    I very much like the ruby syntax. If there's some internal conflict that makes that difficult, most people understand the carat syntax too:

    5px^2
    —
    Reply to this email directly or view it on GitHub.

  3. robwierzbowski commented on Mar 18, 2013

    @robwierzbowski

    That is how Sass deals with a manual square right now. Obviously the intention would be for it to be 25px.

  4. nex3 commented on Mar 22, 2013

    @nex3
    Contributor

    What's the use case for non-integer exponents? I'm not sure we want to move towards adding a full complement of mathematical operations in core Sass. That seems like a good thing for extensions to do.

    It would be pretty confusing if pow(5px, 2) didn't return a result in square pixels.

  5. scottkellum commented on Mar 22, 2013

    @scottkellum
    Author

    @nex3 I want to remove the need for Ruby in the modular scale extension. While CodeKit, Mixture, and other tools do support extensions, the process is far too complex for a significant portion of that audience. Distributing Ruby extensions via Bower is also problematic.

    to be clear, pow(5px, 2) = 25px and pow(5px, 2.5) = 55.901699437px. Your example would result in square pixels. I have found half units to be useful when setting type on an exponential(modular) scale and the scale moves too quickly in that area.

  6. nex3 commented on Mar 22, 2013

    @nex3
    Contributor

    @nex3 I want to remove the need for Ruby in the modular scale extension. While CodeKit, Mixture, and other tools do support extensions, the process is far too complex for a significant portion of that audience. Distributing Ruby extensions via Bower is also problematic.

    What makes this so difficult? Ruby-based extensions are never going to be completely avoidable for complex operations. I'd like to make them easier to use rather than essentially unusable.

    pow(5px, 2) = 25px

    Your example would result in square pixels.

    These two statements seem contradictory.

    I have found half units to be useful when setting type on an exponential(modular) scale and the scale moves too quickly in that area.

    What do you mean by "half units" and "the scale moves too quickly"?

  7. robwierzbowski commented on Mar 22, 2013

    @robwierzbowski

    Half units == decimal exponents, and "the scale moves to quickly" is behavior specific to the modular scale extension.

    I think the main point is right now we can make an exponent function in an extension and distribute it, but it would be more convenient to have a native-to-the-parser syntax. Exponents are one step up from basic math, and a native syntax would encourage authors to use and experiment with them. So the decision is: should exponents be in Sass, or should they be provided by a function in an extension?

  8. nex3 commented on Mar 22, 2013

    @nex3
    Contributor

    We're definitely not adding syntax support for exponents; the functionality isn't broadly applicable enough. We could potentially add a function for it, but there is a line of niche uses past which we won't add math functions. Should we add root functions? Logarithms? Trigonometry?

    I'd much rather see a math package that encapsulates all of these and keeps them out of the core set of functions. Then they're available for libraries that need them, but they don't clutter up the default namespace with functions that just aren't useful to most users.

  9. robwierzbowski commented on Mar 22, 2013

    @robwierzbowski

    That's reasonable. @scottkellum, want to collab on a Sassy Math package?

  10. Snugug commented on Mar 22, 2013

    @Snugug

    @robwierzbowski Kinda like https://github.com/scottkellum/sassy-math 👅
    If we're missing something, happy to add more.

  11. robwierzbowski commented on Mar 22, 2013

    @robwierzbowski

    Ha, well look at that. Consider it ⭐'d

  12. nyarly commented on Aug 22, 2014

    @nyarly

    If the syntax doesn't support at least exponentiation - how does complex math get dealt with in libsass? Most of the examples that come to mind could be calculated by hand and assigned to a variable, but isn't that true of most Sass numerical operations?

  13. Snugug commented on Sep 23, 2014

    @Snugug

    I recently started to work deeply with color spaces and in order to build the w3c relative luminance function, a decimal power is required. This is non-trivial to calculate by hand and while having a Ruby function would allow this to work, it would do so at the loss of cross-compiler compatibility.

  14. torkiljohnsen commented on Dec 1, 2014

    @torkiljohnsen

    I was just working on the exact same thing @Snugug. At some point an x^2.4 was needed for almost all colors in that calculation. Using the Sass-native pow() function from https://github.com/terkel/mathsass 10 times resulted in a serious slowdown in compile speed. This was the only Sass-implementation I was able to find for decimal exponents.

    My purpose with this was automatic color adjustment to ensure AA-compatible color contrasts. The contrast-calculating function was the one doing the ^2.4.

    I managed to cook up something a bit faster, but still very slow:

    x^2.4 = x^2 * x^0.4 
    x^0.4 = x^4/10 = x^2/5 = (x^1/5)^2
    
    In other words:
    x^2.4 = x^2 * (x^1/5)^2
    

    x^1/5 = the fifth root of x.

    I proceded to implement an nth-root-function: (note the hard-coded $guess!)

    @function nth-root-estimate($number, $guess, $n) {
      @return 1/$n * (($number/pow($guess, $n - 1)) - $guess);
    }
    
    @function nth-root($number, $degree, $precision: 5) {
      $guess: 2.7;
      $previous-guess: 0;
    
      // While precision has not been met, keep guessing
      @while round($previous-guess * pow(10, $precision)) != round($guess * pow(10, $precision)) {
        $previous-guess: $guess;
        $guess: $guess + nth-root-estimate($number, $guess, 5);
      }
    
      @return $guess;
    }
    
  15. torkiljohnsen commented on Dec 1, 2014

    @torkiljohnsen

    This is the first time I have come to the sad conclusion that I should have been using less :(

  16. 32 remaining items

  17. chriseppstein commented on Sep 28, 2015

    @chriseppstein

    Integer exponents on a number with units should always compute to (base ^ exponent)(unit^exponent).

    Decimal exponents with units are strange to me. Not sure whether it should be an error or include a fractional unit (which is unlikely to cancel, therefore is rarely going to be printable as a CSS unit -- another kind of error that's harder to debug).

    If you want the same unit you'll need to do something like: pow($somevalueinpixels / 1px, 3) * 1px

  18. corysimmons commented on Mar 15, 2016

    @corysimmons

    This has been open a few years. Have you had time to consider it and make a decision? 🍕

  19. corysimmons commented on Mar 15, 2016

    @corysimmons

    3 years to come up with a namespace? 👏 💨

    Actually, nevermind. I'll write lib in something else. Have nice day.

  20. tabatkins commented on Mar 16, 2016

    @tabatkins

    @corysimmons No, 3 years to come up with a good, solid spec and implementation of namespacing and modules, while also handling all the other bugs and improvements that a popular living project like Sass generates.

    Being rude can feel a little good in the moment, but it's never polite, and it's a major contributor to project-maintainer burnout. Next time you feel the need to leave a comment while annoyed, please consider waiting a while to cool down, and if you still feel like being a little rude, consider not posting at all.

  21. chris13524 commented on Aug 27, 2017

    @chris13524

    So nothing on this yet? I just had a use for it.

  22. bichotll commented on Sep 20, 2017

    @bichotll

    Just wanted to say I was looking for it too 👍 I can wait.

    ...and seeing the comment on top, just wanted to thank the sass team! The world d not be the same without you! hah

  23. robclancy commented on Aug 6, 2019

    @robclancy
  24. tabatkins commented on Aug 8, 2019

    @tabatkins

    CSS has at this point approved and specced a pow() function (along with some trig and other exponential functions).

    Note that CSS does not allow pow() (or sqrt()) to take a value-with-unit, only plain numbers. There's an explanation in the draft for that.

  25. robclancy commented on Aug 8, 2019

    @robclancy
  26. tabatkins commented on Aug 8, 2019

    @tabatkins

    I'm not part of the Sass project, nor was I replying to you. I was just updating the thread.

    You need to seriously reconsider how you interact with project maintainers. Negativity generally has the exact opposite effect of what you want; it certainly doesn't encourage people to work on the thing you're "asking" about.

  27. robclancy commented on Aug 8, 2019

    @robclancy
  28. nex3 commented on Aug 8, 2019

    @nex3
    Contributor

    @robclancy This is your one and only warning. Keep this up and you'll be blocked from this org.

  29. stof commented on Mar 15, 2022

    @stof
    Contributor

    @nex3 this issue should be closed, as math.pow has already been implemented

  30. nex3 commented on Apr 11, 2023

    @nex3
    Contributor

    Closing this out as @stof suggested

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestunder considerationThe Sass team is debating whether to do this

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions