typestar

Numbers in Erlang

div and rem for integers, / for floats, no overflow ever.

%% div and rem for integers, / for floats, and no overflow ever.
main(_) ->
    Seconds = 745,
    io:format("~p min ~p sec~n", [Seconds div 60, Seconds rem 60]),

    io:format("Exact split: ~p~n", [7 / 2]),
    io:format("Floor split: ~p~n", [7 div 2]),

    %% Integers grow without bound; bsl shifts bits left.
    io:format("2^100 = ~p~n", [1 bsl 100]),

    io:format("Rounded: ~p and ~p~n", [round(2.5), trunc(2.9)]),
    io:format("Absolute: ~p~n", [abs(-45)]).

How it works

  1. div and rem split the seconds into minutes.
  2. 1 bsl 100 computes 2^100 exactly — integers just grow.
  3. round, trunc, and abs cover the conversions.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
437
Tokens
134
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 124 seconds.

Type this snippet

Step 4 of 4 in Text & numbers, step 22 of 31 in Language basics.

← Previous Next →

Numbers in other languages