Arithmetic

Once we have data stored in our programs, there are a number of ways we can manipulate that data. Let’s look at a few of them here.

Basic Operations

At its core, a computer is capable of performing all of the basic arithmetic operations, so we can write programs that can add, subtract, multiply and divide numbers. However, there is one big caveat that must be dealt with.

As discussed on the previous page, computer programs can store numbers as both integers and floating point numbers. What happens when we perform operations on two different types of numbers? For example, if an integer is added to a floating point number, would the resulting number be an integer or a floating point number, as in $ 1 + 1.5 $?

As our intuition suggests, performing the operation gives us $ 1 + 1.5 = 2.5 $, which is a floating point number. In general, when performing an operation on either two integers or two floating point numbers, the result will match the type of the two operands. However, when performing an operation on both an integer and a floating point number, the result is a floating point number.

There are two notable exceptions to this rule:

  1. In a strongly-typed programming language, such as Java, the result may be converted to match the type of the variable it is being stored in, if allowed by the language.
  2. When dividing two integers, the result may be either a floating point number or an integer, depending on the language.

We’ll discuss both of these exceptions later in this chapter when we dig into the details for each programming language.

New Operations

Video Materials

In addition to the basic operations listed above that we are all familiar with from our mathematics class, there are a few new operations we should be aware of as well.

Modulo

The first new operation, the modulo operation, finds the remainder after dividing two numbers. It is typically written as $ 9 \bmod 5 $ when printed, but most programming languages use the percent sign %, as in 9 % 5 to refer to this operation.

To calculate the result of the modulo operation, we must look back to long division. In the example above, we can calculate $ 9 \bmod 5 $ by first calculating $ 9 / 5 $, which is $ 1 $ with a remainder of $ 4 $. So, the result of $ 9 \bmod 5 $ is $ 4 $.

Another way to think about the modulo operation is to simply find the largest multiple of the second operand that is smaller than the first, and then subtract the two, just like you do when performing long division. So, we can find $ 42 \bmod 13 $ by first calculating the largest multiple of $ 13 $ that is smaller than $ 42 $, which is $ 39 $, or $ 13 * 3 $. Then, we can subtract $ 42 - 39 = 3 $, so $ 42 \bmod 13 = 3 $.

The modulo operation is very important in many areas of programming. In fact, it is one of the core operations for most modern forms of encryption!

Modulo & Negative Numbers

The modulo operation is not consistently defined when applied to negative numbers. Therefore, each programming language may return a slightly different result when performing this operation on a negative number. Make sure you carefully consider how this operation is used, and consult the official documentation for your programming language or test the operation if you aren’t sure how it will work.

Truncated & Floor Division

As discussed a bit earlier, one of the stranger things in programming is dealing with division. When dividing two integers, it is possible to end up with a result that is a floating point number, as in $ 9 / 8 = 1.125 $. So, how should we handle this?

It turns out that each programming language may handle it a bit differently. For example, Java would truncate the result by removing everything after the decimal point to make the result an integer. So, in Java, the statements 99 / 100 and -99 / 100 would both usually evaluate to 0. However, we can force the result to be a floating point number by making sure that at least one of the operands is a floating point number, as well as the variable we are storing the result in.

Python, on the other hand, would store the result as a floating point number by default if needed. However, Python also includes a special operator, known as floor division or //, that would round the result down. For positive numbers, this means that the result would be rounded toward 0, while negative numbers would be rounded away from 0. So, in Python, the statement 99 // 100 would evaluate to 0 since it is rounded toward 0, while -99 // 100 would evaluate to -1 since it is rounded away from 0.

In short, we just need to pay special attention when we use the division operation to make sure we get the result we expect.

Assignment & Equality

Lastly, we should briefly discuss the assignment operator. We’ve already come across it earlier in this chapter. In mathematics, we usually use the equals sign = to denote equality, as in $ x + 5 = 10 $.

In programming, we use the single equals sign = to perform assignment. This allows us to store a value into a variable, as in x = 5. This would store the value $ 5 $ into the variable x. We could also say that we assign the value $ 5 $ to x.

However, it is very important to note that the variable we are assigning a value to goes first, on the left side of the equals sign. So, we cannot say 5 = x in most programming languages.

Equality

Most programming languages use a double equals sign, or == to denote equality. We’ll learn more about equality and other comparison operators in a later chapter.