Skip to content

Suggestion: implement an exponentiation (power-of) operator #3603

Closed

Description

Currently, to get exponentiation ($x to the power of $n), you must use the .NET framework:

E.g., to calculate 2 to the power of 8:

> [Math]::Pow(2, 8)
256

It would be nice to have a dedicated operator, such as **, as in other languages (e.g., Python, Ruby, JavaScript, Perl):

 > 2 ** 8
 256

Activity

  1. added
    WG-Language
    Issue-Enhancement
    on Apr 20, 2017
  2. added
    Issue-Discussion
    and removed
    Issue-Enhancement
    on Apr 20, 2017
  3. lzybkr commented on Apr 20, 2017

    @lzybkr
    Contributor

    I'm disinclined to support this operator. My thinking:

    • C# doesn't have it, so does PowerShell really need it?
    • An operator is potentially confusing - different languages use different tokens (is it ** or ^)
    • The operator isn't used all that often, especially in typical PowerShell scripts
    • [Math]::Pow(2, 8) is really readable.
  4. mklement0 commented on Apr 20, 2017

    @mklement0
    ContributorAuthor

    Thanks for the feedback.

    C# doesn't have it, so does PowerShell really need it?
    The operator isn't used all that often, especially in typical PowerShell scripts

    I can't speak to the real-world need, only that I keep missing it personally. Perhaps others can comment.

    Frankly, I find the absence of an exponentiation operator from C# puzzling; this blog post from 2004 explains that it wasn't included because it is "fairly rare", which wasn't too well-received in the comments.

    Aside from that, PowerShell already has operators that C# doesn't have, and it is a sufficiently different beast so that I wouldn't rule it out just based on that.

    Perhaps PowerShell can lead by example and down the road we'll see an operator added to C# as well? :)

    An operator is potentially confusing - different languages use different tokens (is it ** or ^)

    ^ indeed invites confusion, because it has different semantics in some languages.

    **, by contrast, while not used by all languages, seems to have no other meaning, and, as stated, is used in several popular languages: Python, Ruby, JavaScript, Perl

    [Math]::Pow(2, 8) is really readable.

    It is readable, but neither easy to remember nor to type.

    To me, a dedicated exponentiation operator seems like a natural complement to the arithmetic operators.

  5. HumanEquivalentUnit commented on Sep 1, 2017

    @HumanEquivalentUnit
    Contributor

    I have missed the Python ** power operator as well, sometimes. Adding a suggestion that this operator could be in the form:

    2 -pow 8
    

    Which is perhaps more clear, more discoverable, and inviting less confusion than ^ or **. That would be different to the arithmetic syntax (+, -, *, /), but consistent with -shl syntax.

    PowerShell tends to like full words, so 2 -power 8 is another option, but -pow has the advantage of matching the name of [math]::Pow()

  6. mklement0 commented on Sep 1, 2017

    @mklement0
    ContributorAuthor

    HumanEquivalentUnit:

    I can see both sides of the argument, and I'd be happy with either syntax.

  7. added
    Issue-Enhancement
    Up-for-Grabs
    on Sep 2, 2017
  8. added
    Hacktoberfest
    on Sep 30, 2017
  9. SteveL-MSFT commented on Oct 11, 2017

    @SteveL-MSFT
    Member

    My personal opinion is that I would be fine adding a power operator. It would certainly not be high on my priority list, but since Xavier Hahn (@Gimly) is already offering to contribute the work, I don't see any major downside to enabling this capability. For the syntax, I think I prefer 2 -pow 8 only because it enables adding more operators in the future without running out of symbols or remember what which ones means what.

  10. rkeithhill commented on Oct 11, 2017

    @rkeithhill
    Collaborator

    I dunno about using -pow. If we went this way, I'd expect the modulo operator to be -mod but instead it is %. I don't see a strong need for this operator but if it gets implemented, I'd prefer syntax inline with Python and JavaScript (**) given that there is no equivalent operator in C#.

    One other option is to consider doing what C# did to allow folks to use "simpler" method invocations e.g.: pow(), sin(), cos() etc. C# does this by allowing users to add a using static System.Math.

  11. SteveL-MSFT commented on Oct 11, 2017

    @SteveL-MSFT
    Member

    using static might be a better general solution that works well with the math functions

  12. iSazonov commented on Oct 11, 2017

    @iSazonov
    Collaborator

    I vote for using static - it looks very power. We need to explore this deeper.

  13. iSazonov commented on Oct 12, 2017

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) I thinnk we should close the Issue. If we want discuss/implement static it is better open new Issue.

  14. added
    Resolution-Won't Fix
    and removed
    Hacktoberfest
    Up-for-Grabs
    on Oct 12, 2017
  15. mklement0 commented on Oct 12, 2017

    @mklement0
    ContributorAuthor

    using static definitely sounds like an interesting addition worth exploring separately, but it doesn't directly address what was proposed here: something that's available as built-in functionality, preferably as an operator.

    (Making the static members of [math] available by default would be problematic, given that it contains such generically named members such as Log).

  16. markekraus commented on Oct 12, 2017

    @markekraus
    Contributor

    I've been following this one on the periphery, so I might as well chime in.

    I'm not sure how useful a native power operator in PowerShell really is. I believe anyone who is using it would also be using the other Math statics already and I definitely don't see a need to add those as native operators. I like the use static idea, so long as it can be implemented at the user's discretion for the reason of Michael Klement (@mklement0) point about Log().

    If use static were introduced as a user discretion feature, it may be confusing if this also couldn't be used on user defined PowerShell classes.

  17. iSazonov commented on Oct 12, 2017

    @iSazonov
    Collaborator

    Where is Issue for use static? :-)

  18. markekraus commented on Oct 12, 2017

    @markekraus
    Contributor

    Opened #5099 for discussion/tracking of use static

  19. dzmitry-lahoda commented on Feb 3, 2022

    @dzmitry-lahoda

    I still would find it usefull. pwsh has nice numbers (like python/lisp). and cannot use fsharp for as it is too typed. but for simple and small formulas in console it is great. so having 2.pow(512) or 2**512 would be great.

  20. rapettif commented on Aug 21, 2024

    @rapettif

    Most programming languages have an exponentiation operator.
    It's odd that Powershell does not have one, considering it's fairly powerful.
    I was trying to convert a number in bytes to TB, and i assumed that $MyVariable/(1024^4) would work. That's when i realized that Powershell doesn't have any exponentiation operator. Fortunately, i could substitute that by $MyVariable/1TB , but what if you need exponentiation for other uses?

  21. alexandrehtrb commented on Aug 26, 2025

    @alexandrehtrb

    +1 for this

    2**4 is better than [Math]::Pow(2,4)

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

    Issue-DiscussionIssue-EnhancementResolution-Won't FixWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions