Powershell: Suggestion: implement an exponentiation (power-of) operator

Created on 20 Apr 2017  路  13Comments  路  Source: PowerShell/PowerShell

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
Issue-Discussion Issue-Enhancement Resolution-Won't Fix WG-Language

Most helpful comment

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.

All 13 comments

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.

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.

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()

@HumanEquivalentUnit:

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

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

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.

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

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

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

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

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

Where is Issue for use static? :-)

Opened #5099 for discussion/tracking of use static

Was this page helpful?
0 / 5 - 0 ratings