Arithmetic Operations

Learn how arithmetic operations work in Bash.

Arithmetic operations

Let’s start with the arithmetic operations since they are relatively simplest. Programming languages use regular symbols to denote them:

  • + addition
  • - subtraction
  • / division
  • * multiplication

There are two more operations that are often used in programming. These are exponentiation and division with remainder.

Let’s suppose that we want to raise the number a to the power of b. We can write it as a​b​​. Here, a is the base and b is the exponent. If we want to raise two to the power of seven, we write 2​7​​. The same operation in Bash looks like this:

2**7

Calculating the remainder of a division is a complex but essential operation in programming. So, we should consider it in detail.

Let’s suppose that we divide one integer by another. We get a fractional number in the result. The division operation produced a remainder in this case.

Here’s an additional example. Let’s suppose we want to divide the number 10 (the dividend) by 3 (the divisor). If we round the result, we will get 3.33333 (the quotient). The remainder of the division equals 1 in this case. To find it, we should multiply the divisor 3 by the integer part of the quotient 3 (the incomplete quotient). Then, we subtract the result from the dividend 10. It gives us the remainder, which equals 1.

Let’s write our calculations in formulas. We can introduce the following notation for that:

  • a is a dividend
  • b is a divisor
  • q is an incomplete quotient
  • r is a remainder

Using the preceding notation, we get this formula for calculating the dividend:

a = b * q + r

Let’s move the b * q multiplication to the left side of the equal sign. Then, we get the following formula for finding the remainder:

r = a - b * q

The right choice of an incomplete quotient q raises questions. Sometimes, several numbers fit this role. There is a restriction that helps us choose the right one. The q quotient should have the value that makes the r remainder’s absolute value less than the b divisor. In other words, it should fulfill the following inequality:

|r| < |b|

The percent sign denotes the operation of finding the remainder in Bash. Some languages use the same symbol for the modulo operation. These two operations are not the same. They give the same results only when the signs of the dividend and the divisor match.

Here is an example of calculating the division remainder and modulo when dividing 19 by 12 and -19 by -12. We get these results:

19 % 12 = 19 - 12 * 1 = 7
19 modulo 12 = 19 - 12 * 1 = 7

-19 % -12 = -19 - (-12) * 1 = -7
-19 modulo -12 = -19 - (-12) * 1 = -7

Let’s change the signs of the dividend and divisor. Then, we get the following pairs of numbers: 19, -12 and -19, 12. If we calculate the division remainder and modulo for them, we get these results:

19 % -12 = 19 - (-12) * (-1) = 7
19 modulo -12 = 19 - (-12) * (-2) = -5

-19 % 12 = -19 - 12 * (-1) = -7
-19 modulo 12 = -19 - 12 * (-2) = 5

We see that the remainder and the modulo differ for the same pairs of numbers. It looks strange because we use the same formula for calculating them. The trick happens when we choose the q incomplete quotient. We calculate it like this when finding the division remainder:

q = a / b

We should round the result to the lowest absolute number, which means discarding all decimal places.

Calculating the incomplete quotient for finding the modulo depends on the signs of a and b. If the signs are the same, the formula for the quotient stays the same:

q = a / b

If the signs differ, we should use another formula:

q = (a / b) + 1

We should round the result to the lowest absolute number in both cases.

When somebody talks about the division remainder r, they usually assume that both the dividend a and divisor b are positive. This is why programming books often mention the following condition for r:

0 ≤ r < |b|

However, we can get a negative remainder when dividing numbers with different signs. Let’s remember a simple rule: the r remainder always has the same sign as the a dividend. If the signs of r and a differ, we found the modulo but not the division remainder.

Let’s always keep in mind the difference between the division remainder and modulo. Some programming languages provide the % operator that calculates the remainder. Other languages have the same operator, but it calculates the modulo. It leads to confusion.

If we are unsure of our calculations, we should check them. The % operator in Bash always computes the division remainder. For example, we want to find the remainder of dividing 32 by -7. This command completes this task:

echo $((32 % -7))

The preceding command prints the division remainder that equals four.

Run the commands discussed in this lesson in the terminal below.

Get hands-on with 1200+ tech skills courses.