Reading — step 1 of 5
Learn
~1 min readGetting Started
Standard arithmetic with +, -, *. Division is more nuanced: / always returns a float; div is integer division; rem is the remainder.
IO.puts 7 + 3 # 10
IO.puts 7 * 3 # 21
IO.puts 7 / 3 # 2.3333... (always float)
IO.puts div(7, 3) # 2
IO.puts rem(7, 3) # 1
IO.puts :math.pow(2, 10) # 1024.0 (Erlang module)
Integers are arbitrary precision — 1 |> Stream.iterate(&(&1 * 2)) |> Enum.at(1000) works without overflow.
For more math, the :math module (from Erlang) provides :math.sqrt, :math.log, etc. Note the leading colon — that's how you call modules whose names are atoms (Erlang modules).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…