Math for Computer Science

Introduction

We learned in a previous lesson about counting with negative numbers. Money, temperature and elevation can all be counted with negative numbers. When working with larger amounts, it makes sense to use counting shortcuts like addition and subtraction. How can we add and subtract negative numbers?

If we count the money we have with positive numbers, we can count the money we owe, debt, with negative numbers. Adding debt to the money we have would result in a decrease in our wealth, so adding negative numbers is like subtracting positive numbers. Subtracting debt from the money we have would result in an increase in our wealth, so subtracting negative numbers is like adding positive numbers.

How are negative numbers added and subtracted?

Adding and subtracting negative numbers can be summarized with the following rules:

• Adding a positive results in a move to the right on the number line
• Adding a negative results in a move to the left on the number line
• Subtracting a positive results in a move to the left on the number line
• Subtracting a negative results in a move to the right on the number line

How is the addition and subtraction of negative numbers used in computer science?

Since computer programs are often used to model concepts that can be described by negative numbers like financial transactions or climate, it is common to add or subtract negative numbers as well. In a video game, the position of an object might be negative, in which case it would be necessary to add or subtract negative numbers to calculate changes to that object's position.

Examples

Q: 5 + (-3)

Your browser does not support the canvas element.

A: Negative 3 is represented by an arrow in the negative direction (left). Since we are adding -3, we move in that same direction (left). Thus, 5 + (-3) is the same as 5 − 3, or 2.

Subtracting negative numbers

Q: 5 − (-3)

Your browser does not support the canvas element.

A: Negative 3 is represented by an arrow in the negative direction (left). Since we are subtracting -3, we move in the opposite direction (right). Thus, 5 − (-3) is the same as 5 + 3, or 8.