[erlang-questions] math:pow(10, 1000). ===> exception error: bad argument in an arithmetic expression

Kostis Sagonas kostis@REDACTED
Fri Apr 10 09:46:49 CEST 2009


Charles Cui wrote:
> math:pow(10,1000).
> ** exception error: bad argument in an arithmetic expression
>      in function  math:pow/2
>         called as math:pow(10,1000)
> 
> I want to do bigint compute,such as math:pow(a,b).
> Can Erlang do this?

Sure it can.  It's a Turing complete language after all...
You just have to define it yourself.
One possible definition appears below:

-------------------------------------------------------
-module(m).
-export([pow/2]).

-spec pow(integer(), non_neg_integer()) -> integer()
        ; (float(), non_neg_integer()) -> float().

pow(X, N) when is_integer(N), N >= 0 -> pow(X, N, 1).

pow(_, 0, P) -> P;
pow(X, N, A) -> pow(X, N-1, A*X).

---------------------------------------------------------
Eshell V5.7  (abort with ^G)
1> c(m).
{ok,m}
2> m:pow(10, 100).
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Kostis



More information about the erlang-questions mailing list