Skip to content
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

Proposal: Constant expression - Power operator? #14665

Closed
iam3yal opened this issue Oct 21, 2016 · 6 comments
Closed

Proposal: Constant expression - Power operator? #14665

iam3yal opened this issue Oct 21, 2016 · 6 comments

Comments

@iam3yal
Copy link

iam3yal commented Oct 21, 2016

Disclaimer: I don't know if it's worthy of a proposal but it's really a tiny feature and some people might have some interest in it.

Currently, there are three ways to raise a number to a power:

  1. Using left shift but this only works when raising 2 to a power.
  2. Using plain multiplications.
  3. Using Math.Pow(2, 3) or Pow(2, 3) but it's not a constant expression.

So what I'm proposing is a power operator much like it exists in Python or JavaScript ** but this one will be evaluated at compile time unless it can't because the base or the power are non-constant.

The motivation for this is mainly to allow us represent some formulas in their natural/symbolic/mathematical way, I wish we could have constexpr in C# but this is another story. :)

@dsaf
Copy link

dsaf commented Oct 21, 2016

Should there be e.g. square/cubic/... root and absolute value operators as well? Is power operation simply the most common one?

@dharmatech
Copy link
Contributor

@iam3yal
Copy link
Author

iam3yal commented Oct 21, 2016

@dsaf Well, yeah, it is actually common and cubic is formulated by power so power is more primitive.

@MgSam
Copy link

MgSam commented Oct 21, 2016

It seems to me like people ask about this kind of stuff a lot- evaluate math at compile time, strings at compile time. Seems like a general purpose feature that allowed running statically known values through static methods at compile time would be a better idea.

@iam3yal
Copy link
Author

iam3yal commented Oct 21, 2016

@MgSam Yeah well, this isn't really the same, there's two parts to this story, expressiveness of logic and performance, symbols are more succinct by nature and because some popular languages got it like PHP, JavaScript, F# and Python then it makes this operator fairly common.

We can definitely write MathC library that got some deterministic, constant operations if and when something like constexpr is going to be available to us but even then I don't think that this tiny feature will bloat the language.

Finally, I don't know how many years we will wait for something like constexpr to be available but like I said I really wish to have something like it in the language but this requires a separate proposal. :)

@iam3yal iam3yal changed the title Constant expression power operator? Proposal: Constant expression - Power operator? Oct 24, 2016
@iam3yal
Copy link
Author

iam3yal commented Jan 20, 2017

This feature is already discussed at #4594.

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

No branches or pull requests

5 participants