Floating-Point Numbers
Let’s try doing some arithmetic with floating-point numbers.
| 1> 5/3. |
| 1.6666666666666667 |
In line 1 the number at the end of the line is the integer
3
. The period signifies the end of the expression and is
not a decimal point. If I had wanted a floating-point number here,
I’d have written 3.0
.
When you divide two integers with /, the result is
automatically converted to a floating-point number; thus,
5/3
evaluates to 1.6666666666666667
.
| 2> 4/2. |
| 2.0 |
Even though 4
is exactly divisible by 2
, the result
is a floating-point number and not an integer. To obtain an integer
result from division, we have to use the operators
div
and rem
.
| 3> 5 div 3. |
| 1 |
| 4> 5 rem 3. |
| 2 |
| 5> 4 div 2. |
| 2 |
N div M
divides N
by
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.