Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 48 ยท Getting Started

Basic Types: Numbers, Atoms and Booleans

Learn Elixir's basic types: big integers, floats, arithmetic with / div and rem, atoms, true, false and nil, and the simple truthiness rule behind if.

Every value in Elixir has a type, and a handful of basic types cover most of what you write on day one: integers, floats, atoms and the special atoms true, false and nil. This lesson shows how each behaves, how arithmetic works (including a surprise about the / operator) and the very small truthiness rule that decides what counts as "true" in an if.

Elixir is dynamically typed: you do not declare types, and a variable can hold any kind of value. You can ask a value about its type with functions such as is_integer/1, which by convention return a boolean. The Elixir blocks are read-only here; the TryIts are JavaScript stand-ins.

The basic types

Integer As big as memory allows.
Float 64-bit decimals.
Atom A name that is its own value.
Boolean true and false are atoms.
1 / 2

Integers

Integers are whole numbers, and they have no fixed size. They grow as needed, so you never overflow:

ELIXIR
iex> 42
42
iex> 1_000_000
1000000
iex> 2 ** 100
1267650600228229401496703205376
iex> 0xFF
255
iex> 0b1010
10
iex> 0o17
15

Underscores are just visual separators. The prefixes 0x, 0b and 0o write hexadecimal, binary and octal numbers. The ** operator is exponentiation. A language with 32 or 64-bit integers would wrap or fail on 2 ** 100; Elixir returns the exact result.

Floats

Floats have a decimal point and optionally an exponent. They are 64-bit IEEE 754 numbers, so they carry the usual rounding behavior:

ELIXIR
iex> 3.14
3.14
iex> 1.0e3
1000.0
iex> 0.1 + 0.2
0.30000000000000004

A float needs digits on both sides of the point: 3. and .5 are errors. Use floats for measurements and ratios, and integers (for example cents) for money.

Arithmetic and the / surprise

The usual operators are +, -, * and /. Here is the part that catches newcomers: / always returns a float, even when the division is exact.

ELIXIR
iex> 10 / 2
5.0
iex> 7 / 2
3.5

For whole-number division, use div/2 and rem/2, or Integer.mod/2:

ELIXIR
iex> div(7, 2)
3
iex> rem(7, 2)
1
iex> div(-7, 2)
-3
iex> rem(-7, 2)
-1
iex> Integer.mod(-7, 2)
1

div truncates toward zero, and rem takes the sign of the dividend, so rem(-7, 2) is -1. Integer.mod/2 gives a result with the sign of the divisor, which is usually what you want for wrapping an index around a list. To round floats, use round/1, trunc/1 or Float.round/2:

ELIXIR
iex> round(3.5)
4
iex> trunc(3.9)
3
iex> Float.round(3.14159, 2)
3.14

Mixing integers and floats in arithmetic gives a float: 1 + 2.0 is 3.0.

The JavaScript stand-in below models these operations. It uses BigInt for the unlimited integers, since a plain JavaScript Number loses precision on large values. It only imitates the behavior.

JS Stand-in: integers, /, div and rem

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Atoms

An atom is a constant whose value is its own name. Atoms start with a colon:

ELIXIR
iex> :ok
:ok
iex> :error
:error
iex> :"hello world"
:"hello world"
iex> :ok == :ok
true
iex> Atom.to_string(:ok)
"ok"

Atoms are used everywhere as labels: status markers (:ok, :error), options, and the keys of maps. A function that can succeed or fail typically returns {:ok, value} or {:error, reason} (see error handling). Comparing two atoms is very fast, because each distinct atom exists once.

One important caution: the BEAM stores atoms in a table that is never garbage collected and has a fixed limit (just over a million). Never create atoms from untrusted input such as user text; the security basics lesson explains why. Writing atoms in your own source code is perfectly safe.

Module names such as String and Enum are atoms too (written with a capital letter). That is why is_atom(String) is true.

true, false and nil

The three special values are really atoms with friendly names:

ELIXIR
iex> true == :true
true
iex> is_atom(false)
true
iex> is_atom(nil)
true
iex> is_boolean(true)
true
iex> is_boolean(nil)
false

true and false are the booleans. nil means "nothing here": the result of looking up a missing key, or a function with nothing to return. Notice that nil is not a boolean, even though it behaves as false in conditions.

Truthiness

When a condition needs a yes or no answer (if, unless, &&, ||, !), Elixir uses a very simple rule: only false and nil are falsy. Everything else is truthy. That includes 0, the empty string "" and the empty list [], which are falsy in some other languages.

ELIXIR
if 0, do: "truthy", else: "falsy"
# "truthy"

if "", do: "truthy", else: "falsy"
# "truthy"

if nil, do: "truthy", else: "falsy"
# "falsy"

The operators &&, || and ! use this relaxed rule, and && and || return one of their operands rather than a pure boolean. The stricter and, or and not require a real boolean on the left. Operators and comparison covers the details.

JS Stand-in: the truthiness rule

Press Run (or Ctrl + Enter) to see the console output here.

No output.

If you come from JavaScript or Python, this is the thing to remember: in Elixir 0 and "" are truthy.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 10 / 2 return in Elixir?

    The / operator always returns a float. Use div(10, 2) when you want the integer 5.
  2. 2. Which values are falsy in Elixir?

    Only false and nil are falsy. Everything else, including 0, the empty string and the empty list, is truthy.
  3. 3. What is true in Elixir?

    true, false and nil are atoms. true is the same value as :true.
  4. 4. Why should you not turn user input into atoms?

    The atom table has a fixed limit and atoms are never freed. Creating them from untrusted input can crash the VM.
  5. 5. What does rem(-7, 2) return?

    rem takes the sign of the dividend, so rem(-7, 2) is -1. Integer.mod(-7, 2) would return 1.

Summary

  • Integers have unlimited size; floats are 64-bit decimals; / always returns a float.
  • Use div/2, rem/2 and Integer.mod/2 for whole-number division, and round, trunc and Float.round to round.
  • Atoms are constants named by themselves (:ok); never create them from untrusted input.
  • true, false and nil are atoms. Only false and nil are falsy.
  • Dynamic typing: use is_integer/1, is_float/1, is_atom/1 and friends to check a value's type.

Next: Strings and Binaries