Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Language Basics

Numbers and Atoms

Learn Erlang's basic data types: arbitrary-size integers, floats, number bases, atoms, booleans and the type-checking functions every beginner needs.

Every Erlang value is called a term. This lesson covers the simplest terms: numbers and atoms. They are the building blocks for tuples, lists and maps later on.

Integers

Erlang integers have no fixed size. They grow as large as memory allows, so you never meet integer overflow:

ERLANG
1> 2 + 2.
4
2> 1000000 * 1000000 * 1000000.
1000000000000000000
3> 2 bsl 100.
2535301200456458802993406410752
4> 100000000000000000000 * 100000000000000000000.
10000000000000000000000000000000000000000

This is handy for cryptography and math, and it removes a whole class of bugs.

Number bases and characters

A number can be written in any base from 2 to 36 using Base#Digits, and a character literal begins with $:

ERLANG
1> 2#1010.
10
2> 16#FF.
255
3> 8#777.
511
4> $A.
65
5> $a - $A.
32

Underscores can separate digits in recent OTP versions for readability, for example 1_000_000.

Floats

A number with a decimal point (or an exponent) is a float, a 64-bit IEEE 754 double:

ERLANG
1> 3.14.
3.14
2> 1.0e3.
1000.0
3> 0.1 + 0.2.
0.30000000000000004

The binary rounding problem in the last line is common to nearly every language. Never compare floats for equality after arithmetic.

Arithmetic operators

Operator Meaning Example Result
+, -, * Add, subtract, multiply 3 * 4 12
/ Division, always a float 7 / 2 3.5
div Integer division 7 div 2 3
rem Integer remainder 7 rem 2 1
-X Negation -(3) -3

Mixing integers and floats yields a float. 10 / 5 is 2.0, not 2. Use div for integer results. div and rem only accept integers; 7.0 div 2 raises a badarith error.

The math module covers the rest: math:sqrt/1, math:pow/2, math:pi/0, math:log/1, math:sin/1. Conversions use the built-ins trunc/1, round/1, floor/1, ceil/1, float/1 and integer_to_list/1:

ERLANG
1> round(2.5).
3
2> trunc(2.9).
2
3> float(3).
3.0
4> integer_to_list(255, 16).
"FF"
5> list_to_integer("42").
42

Atoms

An atom is a constant whose value is its own name. Atoms begin with a lowercase letter and may contain letters, digits, underscores and @:

ERLANG
1> ok.
ok
2> hello_world.
hello_world
3> 'Hello World'.
'Hello World'
4> 'can have spaces'.
'can have spaces'

If an atom has spaces, starts with an uppercase letter or contains special characters, wrap it in single quotes. Atoms are compared very fast: the runtime keeps one entry per name, and equality is a pointer comparison.

You use atoms where other languages use enums or symbols: ok, error, undefined, true, false, {ok, Value}, {error, Reason} and the names of modules and functions.

Atoms are never garbage collected, and the atom table has a fixed limit (about 1,048,576 by default). Never create atoms from untrusted input, for example with list_to_atom/1 on user data. Use list_to_existing_atom/1 or keep the text as a binary or string.

Booleans are atoms

Erlang has no separate boolean type. true and false are atoms, and the boolean operators accept only them:

ERLANG
1> true and false.
false
2> true or false.
true
3> not true.
false
4> true andalso (1 < 2).
true
5> is_atom(true).
true

The details of and, andalso and comparisons come in the operators lesson.

Type tests

Erlang is dynamically typed, but you can test a term with the is_* functions, which return true or false:

ERLANG
1> is_integer(5).
true
2> is_float(5).
false
3> is_number(5.0).
true
4> is_atom(hello).
true
5> is_atom("hello").
false

Other tests include is_list/1, is_tuple/1, is_map/1, is_binary/1, is_function/1, is_pid/1 and is_boolean/1. They are legal in guards, as you will see in case, if and guards.

Stand-ins in JavaScript

JavaScript's numbers are doubles, so we use BigInt to imitate Erlang's arbitrary-size integers. This is not Erlang.

JS Stand-in: big integers and bases (JavaScript, not Erlang)

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

No output.

JS Stand-in: div, rem and the / operator (JavaScript, not Erlang)

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

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the result of 7 / 2 in Erlang?

    The / operator always produces a float. Use div for integer division.
  2. 2. What is true in Erlang?

    Erlang has no boolean type. true and false are ordinary atoms.
  3. 3. What does 16#FF evaluate to?

    Base#Digits notation: FF in base 16 is 255.
  4. 4. Why should you avoid list_to_atom/1 on user input?

    Creating atoms from untrusted data can exhaust the atom table and crash the VM.

Summary

  • Integers are unbounded; floats are 64-bit doubles. / gives a float, div and rem work on integers.
  • Base#Digits writes numbers in other bases; $A is a character code.
  • Atoms are named constants starting with a lowercase letter, or quoted with single quotes.
  • true and false are atoms, and is_* functions test types.
  • Never create atoms from untrusted input.

Next: Variables and Single Assignment