Simple Integer Arithmetic

Let’s evaluate some arithmetic expressions.

 
1>​ 2 + 3 * 4.
 
14
 
2>​ (2 + 3) * 4.
 
20

You’ll see that Erlang follows the normal rules for arithmetic expressions, so 2 + 3 * 4 means 2 + (3 * 4) and not (2 + 3) * 4.

Erlang uses arbitrary-sized integers for performing integer arithmetic. In Erlang, integer arithmetic is exact, so you don’t have to worry about arithmetic overflows or not being able to represent an integer in a certain word size.

Why not try it? You can impress your friends by calculating with very large numbers.

 
3>​ 123456789 * 987654321 * 112233445566778899 * 998877665544332211.
 
13669560260321809985966198898925761696613427909935341

You can enter integers in a number of ways (for details, see Integers ...

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.