Skip to content

Conversation

@Qqwy
Copy link
Contributor

@Qqwy Qqwy commented Mar 27, 2016

I've taken the liberty to add a new operator, **, to Elixir. The default implementation in Kernel.**/2 is that of arithmetic exponentiation. In this way, it works similar to the ** in Python, Ruby, Haskell and a multitude of other languages.

When working with integers (of which the power has to be positive), the result will be calculated using the Exponentiation by Squaring algorithm, returning an integer.

When working with floats (or a negative integer as power), it uses erlang's :math.pow/2 function, and returns a float. This second case can also be used to calculate roots (i.e. 2 ** 0.5).

Unfortunately, as :math.pow() is not inside erlang's BIN functions (as well as the integer-implementation being built with functions, not macros), it is impossible to use the Kernel.**/2 in guard clauses.

As there were multiple places in the library and the test suite where :math.pow/2 was called directly, this has been changed to use the new operator.


I seem to have been successful in adding this new operator to the parser. :**'s value is as expected. The only problem that remains is that &Kernel.**/2 is for some reason not dispatching properly. Can someone tell me what's going on here?

Of course, I am very happy with any and all comments on how to make the code cleaner, as well as criticism on the provided functionality.

Happy Easter!

~Qqwy

@Qqwy
Copy link
Contributor Author

Qqwy commented Mar 27, 2016

If anyone can tell me why Travis has trouble with building the code, that would be great as well. I cannot seem to figure out what is going wrong.

@lexmag
Copy link
Member

lexmag commented Mar 27, 2016

Thank you. Could you please move the proposal to the elixir-lang-core mailing list? There more people can discuss it in detail. :bowtie:

@lexmag lexmag closed this Mar 27, 2016
@Qqwy
Copy link
Contributor Author

Qqwy commented Mar 27, 2016

Excuses for going full-steam and creating an implementation before properly discussing it in the mailing list 😅 . A topic to discuss this functionality has been created there.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants