Erlang's :math.pow has some limitations, for example it will not allow really high integer exponents:
iex(10)> :math.pow(2, 10000)
** (ArithmeticError) bad argument in arithmetic expression
You can easily reimplement a fast algorithm for computing exponentials that will work with the arbitrarily large integers provided by the runtime:
defmodule Pow do
require Integer
def pow(_, 0), do: 1
def pow(x, n) when Integer.is_odd(n), do: x * pow(x, n - 1)
def pow(x, n) do
result = pow(x, div(n, 2))
result * result
end
end
iex(9)> Pow.pow(2, 10000)
19950631168807583848837421626835850838234968318861924548520089498529438830...