Tracks
/
Elixir
Elixir
/
Syllabus
/
Floating Point Numbers
Fl

Floating Point Numbers in Elixir

6 exercises

About Floating Point Numbers

There are two different kinds of numbers in Elixir - integers and floats.

Floats are numbers with one or more digits behind the decimal separator. They use the 64-bit double precision floating-point format.

float = 3.45
# => 3.45

Elixir also supports the scientific notation for floats.

1.25e-2
# => 0.0125

Rounding errors

Floats are infamous for their rounding errors.

0.1 + 0.2
# => 0.30000000000000004

However, those kinds of errors are not specific to Elixir. They happen in all programming languages. This is because all data on our computers is stored and processed as binary code. In binary, only fractions whose denominator can be expressed as 2^n (e.g. 1/4, 3/8, 5/16) can be expressed exactly. Other fractions are expressed as estimations.

# 3/4
Float.ratio(0.75)
# => {3, 4}

# 3/5
Float.ratio(0.6)
# => {5404319552844595, 9007199254740992}

You can learn more about this problem at 0.30000000000000004.com. The Float Toy page has a nice, graphical explanation how a floating-point number's bits are converted to an actual floating-point value.

Comparison

Integers and floats can be considered equal (==) if they have the same value. However, being different types, they are never strictly equal (===).

1 == 1.0
# => true

1 === 1.0
# => false

Conversion

Integers and floats can be mixed together in a single arithmetic expression. Using a float in an expression ensures the result will be a float too.

2 * 3
# => 6

2 * 3.0
# => 6.0

Floats can be rounded (round), rounded up (ceil), or rounded down (floor).

To round a float into a float, use functions from the Float module (Float.round, Float.ceil, Float.floor). To get an integer instead, use functions from the Kernel module (round, ceil, floor).

Another method of changing a float into an integer is cutting off its decimal part with trunc.

Float.ceil(5.2)
# => 6.0

ceil(5.2)
# => 6

trunc(5.2)
# => 5
Edit via GitHub The link opens in a new window or tab

Learn Floating Point Numbers

Practicing is locked

Unlock 2 more exercises to practice Floating Point Numbers