rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

No Power operator

Open moonheart08 opened this issue 8 years ago • 7 comments

The Power operator is used to raise a number by a power of x. Pony, however, lacks this rather important operator.

moonheart08 avatar Dec 06 '17 16:12 moonheart08

To be clear, we already have a method called pow that does this - it's just that there isn't any syntax sugar for having the compiler translate a binary operator to that method call. I think you already know this, I just wanted to be clear for anyone reading along.

I wouldn't be opposed to such a change.

If we do this, I think the best choice for the operator is probably ** - we can't/shouldn't use ^ because it already has another meaning in Pony (it's the ephemeral type indicator).

jemc avatar Dec 07 '17 03:12 jemc

pow is only defined on FloatingPoint, as well as powi which are implemented using http://llvm.org/docs/LangRef.html#llvm-pow-intrinsic and http://llvm.org/docs/LangRef.html#llvm-powi-intrinsic.

There is no pow in C or C++ as well that operates on integer types. it is only defined on float, double and long double. (This sheds some light on the why: https://stackoverflow.com/questions/2398442/why-isnt-int-powint-base-int-exponent-in-the-standard-c-libraries)

So there is no easy way to implement this without casting, if you don't write it completely in pony.

mfelsche avatar Dec 07 '17 12:12 mfelsche

Maybe, if this is going to be implemented, Exponentiation by Squaring is a good candidate.

mfelsche avatar Dec 07 '17 12:12 mfelsche

I'm fine if POW is float only by necessity. A software implementation of it for integers would not be needed, but it would be nice.

moonheart08 avatar Dec 07 '17 14:12 moonheart08

So, is there an XOR operator, then?

SCKelemen avatar Oct 17 '18 02:10 SCKelemen

@SCKelemen Yes. The syntatic sugar is "xor". Example: https://playground.ponylang.io/?gist=19f5ff394234d6e19ec5bcd1b66ea9d0

EpicEric avatar Oct 17 '18 03:10 EpicEric

I suggest change the current ephemeral type indicator (^) to another like # or whatever

motivation: use of ^ to exponentiation

languages and others that use ^ as exponentiation: Haskell, Julia, Matlab, R, Lua, Google, calculators

igotfr avatar Jun 06 '21 21:06 igotfr