4

I have been tinkering with a factorial module as follows:

-module(factorial).
-export([factorial/1]).

factorial(0) ->
    1;
factorial(Val)->
    Val * factorial(Val-1).

If I run:

1> c(factorial).
{ok,factorial}
2> factorial:factorial(100).

I get: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

How does Erlang so effortless hold such large numbers? On erlang.org when it talks about number types it simply states that that they hold either integers or floats. It must be some kind of dynamic integer that adjust its byte size as necessary?

I find this very cool I just don't know how its done.

aronisstav
  • 7,755
  • 5
  • 23
  • 48
Khaines0625
  • 387
  • 1
  • 5
  • 17

1 Answers1

4

It is a common feature of many functional programming languages, called Arbitrary precision arithmetic.

Notice that in Erlang, arbitrary precision is available only for integers, not floats.

Community
  • 1
  • 1
aronisstav
  • 7,755
  • 5
  • 23
  • 48