Liczby Link to heading

Zapisywanie liczb w komputerze wygląda bardzo podobnie do zapisu w zeszycie do matematyki. Przykładowe liczby:

1
2
4
8
16

Działania dodawania i odejmowania Link to heading

Komputer potrafi dużo więcej niż tylko zapisać liczby. Może obliczyć działania na liczbach. Kiedy w zeszycie zapisujemy dodawanie dwóch liczb używamy znaku plus:

$$ 8 + 5 $$

Podobnie możemy zapisać to samo działanie w komputerze:

8 + 5

Tak zapisane działanie może być łatwo obliczone zarówno w głowie jak i przez komputer. Naciśnij przycisk ▶ i zobacz czy komputer pokazał dobry wynik.

Odejmowanie zapiszemy przy pomocy znaku minus, czyli tak:

8 - 5

Możemy też zapisać dłuższe działania:

1 + 6 + 12 + 3 + 10 - 29

Komputer może bardzo szybko policzyć nawet bardzo długie działania z bardzo dużymi liczbami. Przekonaj się sama i wpisz własny przykład.

Using arithmetic operators, we can add +, subtract -, multiply * and divide /. We also have two special arithmetic operators, div/2 for integer division and rem/2 for remainder.

Arithmetic operators manipulate both positive and negative integers and floats.

As previously mentioned in the lesson on floats, arithmetic operations on floats may result in floating-point errors.

Addition Link to heading

You can create entire equations by writing operators one after the other. So long as there is a valid number (integer or float) on the operator’s left and right hand side.

4 + 3 + 2 + 1

The following code will crash because there is a dangling + operator. The error says TokenMissingError and expression is incomplete because we did not complete the mathematical expression. Whenever our code crashes, Elixir lets you know why with an error message.

for p <- :erlang.processes(), do: :erlang.garbage_collect(p)

As mentioned, adding floats together may result in floating point errors, so only use them when a small amount of innaccuracy isn’t an issue.

1.2 + 1.4

You can use floats and integers together in arithmetic expressions.

1.2 + 4
1.5 + 3.5 + 3 + 4.1 + 9

You can also add negative and positive numbers together. Adding a negative number is the same as subtracting a positive number.

8 + -9

Adding negative numbers might feel unintuitive, so it’s rare to write code like this, but it’s useful to know you can.

Your Turn Link to heading

In the Elixir cell below, add two massive numbers together that you couldn’t do in your head.

Subtraction Link to heading

We can subtract numbers with the minus - operator.

20 - 12

Like with addition, we can subtract floats and integers in any combination.

2.5 - 10
10 - 2.5 - 2.1

Floating-point errors can still occur.

1.3 - 1.2

Subtracting a negative is the same as adding a positive.

10 - -1

Your Turn Link to heading

In the Elixir cell below, subtract two massive numbers together.

Multiplication Link to heading

Computers can efficiently multiply numbers. Under the hood, multiplication is just repeated addition. For example, 5 * 5 is 5 + 5 + 5 + 5 + 5.

10 * 10

Multiplication can still result in floating-point errors.

2.2 * 2.1

You can multiply integers and floats both positive and negative in combination, like with addition and subtraction.

2 * 30 * -2 * 1.4

Your Turn Link to heading

In the Elixir cell below, multiply two massive numbers together.

Division Link to heading

We can also divide numbers. However, in Elixir there are two operators for division. There is the / operator, which will always return a float, and the div operator, which will always return an integer (rounded down).

10 / 5

div is a function. You will learn more about functions in the future. For now, it’s enough to know that div allows you to take two numbers and divide them.

div(10, 5)

Remember that div always rounds values down, so 1.5 becomes 1.

div(3, 2)

There’s a limit to how precise numbers can be in any computer program, so you should always take care when using fractions that don’t divide evenly. You can also run into the same floating-point calculation issues discussed in the floats lesson.

For example, notice that 10 / 3 is 3.3333333333333335.

10 / 3

Your Turn Link to heading

In the Elixir cell below, divide 223 by 71 using / to get a nice slice of pi. Your answer should be close to 3.14.

Divide 100 by 3 using div. Your answer should be 33.