Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: PowerPC Numerics / Part 1 - The PowerPC Numerics Environment
Chapter 6 - Numeric Operations and Functions


Comparisons

PowerPC Numerics supports the usual numeric comparisons: less than, less than or equal to, greater than, greater than or equal to, equal to, and not equal to (see Table 6-1 for a complete listing). For real numbers, these comparisons behave according to the familiar ordering of real numbers.

Comparisons With NaNs and Infinities

Numeric comparisons handle NaNs and Infinities as well as real numbers. The usual trichotomy for real numbers is extended so that, for any numeric values a and b, exactly one of the following statements is true:

The following rule determines which statement is true: If a or b is a NaN, then a and b are unordered; otherwise, a is less than, equal to, or greater than b according to the ordering of the real numbers, with the understanding that

+0 = -0 and - < every real number < +

Comparison Operators

The meaning of high-level language relational operators is a natural extension of their old meaning based on trichotomy. For example, the C expression x=y is true if x is less than y or if x equals y, and is false if x is greater than y or if x and y are unordered. Note that the numeric not-equal relation means less than, greater than, or unordered. The FPCE technical report extends the usual set of C relational operators to a set of 14 comparisons, shown in Table 6-1.
Table 6-1 Comparison symbols
SymbolRelationInvalid if
unordered?
< Less thanYes
> Greater thanYes
<= Less than or equal to Yes
>= Greater than or equal to Yes
== Equal to No
!= Not equal to (unordered, less than, or greater than)No
!<>= Unordered No
<> Less than or greater thanYes
<>= Not unordered (less than, equal to, or greater than)Yes
!<= Not less than or equal to (unordered or greater than)No
!< Not less than (unordered, greater than, or equal to)No
!>= Not greater than or equal to (unordered or less than)No
!> Not greater than (unordered, less than, or equal to)No
!<> Unordered or equalNo

Some relational operators in high-level language comparisons contain the predicate less than or greater than, but not unordered. In C, those relational operators are <, <=, >, and >= (but not == and !=). For those relations, comparisons signal invalid if the operands are unordered, that is, if either operand is a NaN. For the operators equal and nonequal, comparisons with NaN are not misleading; thus, when x or y is a NaN, the relation x == y is false, which is not misleading. Likewise, when x or y is a NaN, x != y returns true, again not misleading. On the other hand, when x or y is a NaN, x < y being false might tempt you to conclude that x y, so PowerPC Numerics signals invalid to help you avoid the pitfall. Table 6-1 shows the results of such comparisons in C.

The full 26 distinct comparison predicates of the IEEE standard may be obtained by logical negation of all of the operators except for == and !=, which never signal invalid. For example, (x < y) and !(x !< y) are logically equivalent for all possible values of a and b, but the former raises the invalid exception flag when x and y compare unordered while the latter does not.

A comparison with a signaling NaN as an operand always signals invalid, just as in arithmetic operations.

In addition to the comparison operators, there are also library functions that perform comparisons. See "Comparison Functions" in Chapter 10, "Transcendental Functions."


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996