Elixir Language Numbers


Example

Elixir comes with integers and floating point numbers. An integer literal can be written in decimal, binary, octal and hexadecimal formats.

iex> x = 291
291

iex> x = 0b100100011
291

iex> x = 0o443
291

iex> x = 0x123
291

As Elixir uses bignum arithmetic, the range of integer is only limited by the available memory on the system.

Floating point numbers are double precision and follows IEEE-754 specification.

iex> x = 6.8
6.8

iex> x = 1.23e-11
1.23e-11

Note that Elixir also supports exponent form for floats.

iex> 1 + 1
2

iex> 1.0 + 1.0
2.0

First we added two integers numbers, and the result is an integer. Later we added two floating point numbers, and the result is a floating point number.

Dividing in Elixir always returns a floating point number:

iex> 10 / 2
5.0

In the same way, if you add, subtract or multiply an integer by a floating point number the result will be floating point:

iex> 40.0 + 2
42.0

iex> 10 - 5.0
5.0

iex> 3 * 3.0
9.0

For integer division, one can use the div/2 function:

iex> div(10, 2)
5