Introduction to Game Programming with C++ (Wordware Game Developers Library)

2.6 Inequalities

Inequalities naturally extend from equations and are, as will be demonstrated later in this book, especially important for programming. Inequalities describe the relationship between numbers; not in the same way or as accurately as ratio, but more generally in terms of range. Using inequalities we can say that some number is greater than or less than another number, or that it falls anywhere in a range between two numbers, one greater and one smaller.

Inequalities use the following symbols:

2.6.1 Expressing Inequalities

The equation 3<9 means "3 is less than 9," and 7>1 means "7 is greater than 1." The arrows between the numbers can be thought to point in a direction along the number line; that is, greater numbers are farther to the right (>) and lower numbers are farther to the left (<). Inequalities can also be expressed with variables such as x < 7, meaning "x is less than 7"; in this case we do not know what number x is. It could be 0, 5, or even −9. All it states is that x, whatever it is, is less than 7.

In a similar fashion, the greater than or equal to and less than or equal to symbols can also be used. x ≤ 9 means x is less than or equal to 9. x could be less than 9, or it could be equal to 9, but it cannot be more than 9. The reverse is true for the greater than or equal to symbol.

2.6.2 Compound Inequalities

When two inequalities are combined, the result is called a compound inequality. For example, x > 3 or x < 9 means "x is greater than 3 OR x is less than 9." Another would be x > 3 and x < 9, meaning "x is greater than 3 AND x is less than 9." This latter example is expressing a range of values between a smaller and larger number. The variable x is between 3 and 9 but does not include those numbers. If we wanted to include any one of those numbers, we should use the greater than or equal to (≥)/less than or equal to (≤) symbols.

2.6.3 Chained/Continued Inequalities

The combined inequality of AND is so prevalent it has its own notation. This is called a chained or continued inequality. Consider the following inequality:

Each term has a relation to the one on the right. 3 is less than x AND x is less than 9. In short, this means x is greater than 3 and x is less than 9.

2.6.4 Solving Inequalities

Inequalities can be expressed and solved in a similar fashion to linear equations. Such inequalities are known as linear equalities.

This inequality was solved in the same way as an equation. In the case above, x is some number less than 5. Let's consider some facts about inequalities.

Категории