May 22, 2008

Numeric Limitations in Erlang

Posted in erlang development tagged , , , , at 4:29 pm by tetontech

The debugging process for the erlang distribution code continues. While debugging the binomial distribution last night I wrote a naive factorial calculator the code for which is common.
%factorial of N

factorial(N) ->
fact_helper(N, 1).
fact_helper(0, Product) ->
Product;
fact_helper(1, Product) ->
Product;
fact_helper(N, Product) ->
fact_helper(N - 1, Product * N).

Out of curiosity I started plugging in large numbers. The largest I have tried is 50,000! It took about 1.5 seconds to calculate and the result is much to large to post here. In fact, I was going to post it here but the number is too large to fit in the clipboard on my machine. Even if the sequence of digits was divided into 6 segments the segments still don’t fit in the clipboard.
This seems to indicate that for integer addition, subtraction, and multiplication the upper numerical bound is limited only by your hardware.

Division is different. It appears to use standard floating point type algorithms. Floating point numbers appear to have a limit similar to other languages and causes an error if the numbers are too large.
For example
sim_dist:factorial(20)/sim_dist:factorial(19).
yeilds 20 as expected.
sim_dist:factorial(200)/sim_dist:factorial(199).
** exception error: bad argument in an arithmetic expression
in operator '/'/2

throws a bad argument exception for the division operator. Floating point numbers therefor must have a maximum size that is less than 199!

An amazing language.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: