Numbers

Erlang has two kinds of number and no more: integers and floats. An integer is exact and unbounded, a float is a 64-bit IEEE double, and the operators say which one you get. There is no separate byte, short or long, and no overflow to watch.

Integers of any size

Plain digits build an integer, and the number grows as large as it needs: when the machine word is too small, the runtime switches to a bignum internally and the source never notices. A left shift is a quick way to build a large power of two, and the decimal digits of the result are a list away:

power.erl

-module(power). -export([main/0]). main() -> Big = 1 bsl 100, io:format("2^100 = ~p~n", [Big]), io:format("digits: ~p~n", [length(integer_to_list(Big))]).

erlc power.erl

erl -noshell -eval 'power:main(), halt().'

2^100 = 1267650600228229401496703205376 digits: 31

bsl shifts the bits of the left operand left by the right one, so 1 bsl 100 is 2 to the power 100. The number has 31 decimal digits and fits in no machine word, which is ordinary for Erlang and the reason a factorial or a hash does not wrap around.

Floats

A float is written with a dot and at least one digit on each side: 2.0, not 2., because a dot after a number is the full stop that ends the expression. An exponent is allowed, as in 1.0e3. The division operator always answers a float, even when both sides are integers, and a float has about 15 significant digits:

erl

1> 2. 2 2> 2.0. 2.0 3> 7 / 2. 3.5 4> 1 / 3. 0.3333333333333333 5> 2.0 * 2.0. 4.0

The first line is the mistake: 2. is the integer 2, and a program that expects a float from 2 / 1 gets 2.0 while 2 div 1 gets 2. Mixing an integer and a float in one arithmetic expression is allowed and gives a float.

Integer math

div divides whole numbers and drops the fraction, and rem gives the remainder of that division. Both demand integer arguments. The result of div is truncated toward zero, and the sign of rem follows the dividend:

erl

1> 7 div 2. 3 2> 7 rem 2. 1 3> -7 rem 2. -1 4> -7 div 2. -3 5> 0.1 + 0.2. 0.30000000000000004

The last line is the float's limited precision: 0.1 and 0.2 have no exact binary form, and their sum is the nearest double. Division by zero is no such subtlety: it raises a badarith error that names the operator and the operands, never a silent infinity. A digit sum shows div and rem together: the last digit is the remainder of a division by ten, and the rest of the number is the quotient.

digits.erl

-module(digits). -export([main/0, sum/1]). sum(0) -> 0; sum(N) -> N rem 10 + sum(N div 10). main() -> io:format("sum of digits of 9875: ~p~n", [sum(9875)]).

erlc digits.erl

erl -noshell -eval 'digits:main(), halt().'

sum of digits of 9875: 29

Bases and character codes

The prefix Base# writes an integer in another base: two for binary, eight for octal, sixteen for hexadecimal, and any base up to 36. The dollar sign writes the code of one character. Both forms are plain integers once compiled.

erl

1> 2#1010. 10 2> 16#ff. 255 3> 8#777. 511 4> $A. 65 5> $a. 97 6> 1 bsl 10. 1024

$A is the code 65, not the string "A" and not the atom A: a character literal is one integer. The next lessons use it when a string is a list of codes.

The math module

Square roots, powers, logarithms and trigonometry live in math, and every function takes and returns floats. math:pi/0 is a constant function, and io:format with ~.2f prints a float with two decimals:

interest.erl

-module(interest). -export([main/0]). main() -> Start = 1000, Rate = 0.05, Years = 3, Total = Start * math:pow(1 + Rate, Years), io:format("after ~p years: ~.2f~n", [Years, Total]).

erlc interest.erl

erl -noshell -eval 'interest:main(), halt().'

after 3 years: 1157.63

math:pow/2 returns a float even for integer arguments, so the whole calculation stays in the float domain. The Type conversions lesson turns a float back into an integer when a count is needed.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How large can an Erlang integer be?

    Answer

    Any size that fits in memory

    Erlang integers are arbitrary precision: the runtime grows the number as needed.

  2. How is a float written?

    Answer

    2.0

    A dot needs a digit on each side; 2.0 is a float and 2. is the integer 2.

  3. What does 7 / 2. return?

    Answer

    3.5

    The slash always divides as a float, even when both arguments are integers.

  4. Which operator divides integers and drops the remainder?

    Answer

    div

    div is integer division; rem gives the remainder instead.

  5. What does -7 rem 2. return?

    Answer

    -1

    The remainder takes the sign of the dividend, so a negative left side gives a negative remainder.

  6. What does $A. return?

    Answer

    The number 65

    The dollar sign builds the character code of the next character, and the code of A is 65.

  7. What happens at 7 / 0.?

    Answer

    It raises a badarith error

    Division by zero raises an arithmetic error that names the operator and the operands.

  8. What does this module print? -module(q1). -export([main/0]). main() -> io:format("~p~n", [1 bsl 10]).

    Answer

    1024

    1 bsl 10 moves the single bit ten places left, which is 1024.

  9. What does this module print? -module(q2). -export([main/0]). sum(0) -> 0; sum(N) -> N rem 10 + sum(N div 10). main() -> io:format("~p~n", [sum(4321)]).

    Answer

    10

    The digits 4 + 3 + 2 + 1 add up to 10.

  10. What does this module print? -module(q3). -export([main/0]). main() -> io:format("~.1f~n", [7 / 2]).

    Answer

    3.5

    7 / 2 is 3.5, and ~.1f prints one decimal digit.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev

Изображение баннера