Elixir Course Online | Prograils - Software Development Company
Integers and Floats
Integer representation
There are four integer representations you can use:
Decimal
iex> 1000 1000
Hexadecimal
iex> 0x1F 31
Octal
iex> 0o777 511
Binary
iex> 0b1010 10
When you're dealing with a big decimal number, you might want to separate its digits into groups with an underscore. For example: 1000000
equals 1_000_000
.
Another cool fact about big numbers is that there is no limit on the size of integers.
Float representation
Floating-point numbers have 16 digits of accuracy with the maximum value of 1038. The only requirement for Float data representation is at least one digit before and after the decimal point.
iex> 10.000e1
100.0
iex> 10.5
10.5
iex> 0.82812
0.82812
Arithmetical operators
Elixir supports basic arithmetical operations:
+
iex> 5 + 5 10
-
iex> 5 - 5 0
/
- result of this operator is represented byFloat
typeiex> 10 / 10 1.0
div
- result of this operator is represented byInteger
typeiex> div(10, 10) 1
*
iex> 5 * 5 25
rem
- remainder operator, result will have the same sign as the function’s first argument, which is the only difference comparing to moduloiex> rem(14, 5) 4 iex> rem(-14, 5) -4
Comparison operators
In most cases you will use comparison operators for numbers, however it's not a must. In Elixir, comparison is based on type according to rule:
number < atom < reference < function < port < pid < tuple < map < list < binary
List of comparison operators:
value equality
iex> 1 == 1 true iex> 1 == 1.0 true
strict equality - checks value and type equality
iex> 1 === 1 true iex> 1 === 1.0 false
value inequality
iex> 1 != 2 true
strict inequality
iex> 1 !== 1 false iex> 1 !== 1.0 true
greater than
iex> 2 > 1 true
greater than or equal to
iex> 2 >= 1 true iex> 2 >= 2 true iex> 2 >= 2.5 false
lower than
iex> 1 < 2 true
lower than or equal to
iex> 1 <= 2 true iex> 1 <= 1 true iex> 1 <= 0.5 false