Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

arithmetic exponentiation operator proposal #1818

Closed
bindsdev opened this issue Jul 29, 2022 · 6 comments
Closed

arithmetic exponentiation operator proposal #1818

bindsdev opened this issue Jul 29, 2022 · 6 comments

Comments

@bindsdev
Copy link

I propose that syntax and functionality for exponentiation is implemented. Currently, if you want to raise some number x to a power of 2, you must do x * x. This is fine if you only want to raise x to itself once. But, if there is ever a case where you want to raise x to a power greater than 2, it could get ugly quickly. The operator would be like Python's exponentiation operator, which is **. Operator overloading for this could be implemented using Raise and RaiseWith(U) or Exponent and ExponentWith(U) interfaces.

With this operator, raising x to a power of 3 could look like x ** 3 instead of x * x * x.

@nigeltao
Copy link

nigeltao commented Jul 29, 2022

Be careful, though, that x**y isn't ambiguous with the existing x * *y syntax where the first star is multiplication (a binary operator) and the second star is a pointer dereference (a unary operator).

Related: the deref p syntax idea from #1454 (which is admittedly different from C++ syntax).

@bindsdev
Copy link
Author

bindsdev commented Jul 29, 2022

Be careful, though, that x**y isn't ambiguous with the existing x * *y syntax where the first star is multiplication (a binary operator) and the second star is a pointer dereference (a unary operator).

Related: the deref p syntax idea from #1454 (which is admittedly different from C++ syntax).

Indeed. With this concern, the operator could be potentially modified to ^+. I do not see any ambiguity with this as + itself is not an existing unary operator from my knowledge. My first thought was ^* but x ^ *y (XOR and dereference) could become ambiguous with x^*y.

@OlaFosheimGrostad
Copy link

x^^n

@tkoeppe
Copy link
Contributor

tkoeppe commented Jul 29, 2022

Does this really need to be an operator, rather than, say, a standard library function? Is this operation common enough to warrant a dedicated syntax (and requiring everyone to learn that syntax)?

@bindsdev
Copy link
Author

Does this really need to be an operator, rather than, say, a standard library function? Is this operation common enough to warrant a dedicated syntax (and requiring everyone to learn that syntax)?

This is a fair question. But personally, I would much rather have an operator than a function call, mainly to not have to append .pow or .raise to every number I want to exponentiate. If unnecessary syntax is a concern though, I suppose a function would be fine

@chandlerc
Copy link
Contributor

FYI, moving from an issue to a discussion.

@carbon-language carbon-language locked and limited conversation to collaborators Jul 30, 2022
@chandlerc chandlerc converted this issue into discussion #1835 Jul 30, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants