Math for Computer Science

2 | Comparing negative numbers

Practice Table of Contents

Introduction

Some problems have many possible solutions: as long as we get to school before the bell rings, we will be on time for class (the number of hours that have passed in the day must be less than a value); as long as we have enough gas in our gas tank, our car can get to our destination (the number of gallons of gas we have must be greater than a value). When comparing numbers, then, it can be useful to describe one number as less than, greater than, or equal to another.

We can compare negative numbers in the same way that we compare positive numbers: just as one mountain might be taller than another, a scuba diver might dive deeper than another. The closer a scuba diver gets to the surface, the greater we would consider his height. Thus, negative numbers closer to zero are greater than negative numbers further from zero.


How are negative numbers compared?

  • On a number line, numbers always increase (become "more positive") to the right and decrease (become "more negative") to the left.
  • Thus, numbers to the right are greater than numbers to the left and numbers to the left are less than numbers to the right.
  • To describe one number as less than another, we use the symbol "<".
  • To describe one number as greater than another, we use the symbol ">".
  • To describe one number as equal to another, we use the symbol "=".

How is the comparison of negative numbers used in computer science?

Computer programs are responsive to user input. Comparisons are often used in conditional statements to create branches in a program which allow for different outcomes. In a video game, for example, if a player's score is greater than 100, then the player could win the game.

Examples

Less and more

Your browser does not support the canvas element.

Q: What number is 2 more than -3?

A: To find the number 2 more than -3, we move 2 units to the right on the number line. Thus, the solution is -1.


Less than and greater than

Your browser does not support the canvas element.

Q: Compare -3 and -1 using <, > or =.

A: Since -3 is less than -1, the solution is: -3 < -1.

Practice | Comparing negative numbers