[erlang-questions] math:pow(X, Y).

Bob Ippolito <>
Mon Mar 19 18:29:49 CET 2012


In general the math library is for float arithmetic, just like in C. My
guess is that the time when this was written, there was no bignum support.

There's an implementation of an int_pow/2 function in mochinum (currently
part of mochiweb):

https://github.com/mochi/mochiweb/blob/master/src/mochinum.erl#L50

Not the fastest algorithm on the planet, but much better than the naive
O(N) implementation. There might be better libraries out there with faster
algorithms or maybe a GMP library, but my needs were pretty minimal so I
wrote the few functions I needed.

1> mochinum:int_pow(42, 909).
34166852412481[…]

-bob

On Mon, Mar 19, 2012 at 10:17 AM, Yves S. Garret <
> wrote:

> Hi all,
>
>    That's the standard method that I use to raise a value to a power.  I
> compared this to the way Python does raising to a power and this is the
> result that I get:
>
> Erlang:
> 11> math:pow(42, 909).
> ** exception error: bad argument in an arithmetic expression
>      in function  math:pow/2
>         called as math:pow(42,909)
>
> Python:
> http://bin.cakephp.org/view/1006418268
>
> Now.  Why is it in Erlang's case I get a float as a return value?  Is
> there a method that only returns a long?  Or do most erlang coders have a
> custom power function if they want ridiculously large numbers?
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120319/6a5bda43/attachment.html>


More information about the erlang-questions mailing list