Document: WG14 N1502

Fix for DR 334 (Comparison macros)

Submitter: Fred J. Tydeman (USA)
Submission Date: 2010-06-17
Related documents: DR 334, N1203, N1302, N1442, N1459, N1472, N1481, N1487
Subject: Fix for DR 334 (Comparison macros)

This Defect Report has been discussed at several meetings. The committee decided to defer this topic until after the type generic macro issue was resolved. Since the type generic macro issue was resolved at the Boulder meeting (May 2010), it is now time to fix DR 334.

Clause 6 (Language) has Constraints and Semantics and uses operands. Clause 7 (Library) has Description and Returns and uses arguments. The DR mentioned Constraints and Semantics, apparently assuming that the comparison macros were built-in operators.

In 7.12.14 Comparison macros, first paragraph, last sentence; make the existing:

In the synopses in this subclause, real-floating indicates that the argument shall be an expression of real floating type.

into its own paragraph. That would match how constraints are done in clause 6. In addition, add this footnote

[footnote]If any argument is of integer type, or any other type that is not a real floating type, the behavior is undefined.

to that sentence. This address the issue of integer arguments (which are allowed to the relational operators).

Add new paragraph to 7.12.14 Comparison macros (or add it to the above own paragraph):

The two arguments to each macro need not be of the same real floating type [footnote].
[footnote]Whether arguments evaluated with extra range or precision are left widened is unspecified.

That sentence above (approved via N1459) allows the two arguments to be different types. The footnote covers the issue of widened arguments. The description of the functions that follow in the standard are clear enough that the usual arithmetic conversions will be applied in any comparison to bring the two operands to the common real type.

Add the following new subclause to the end of Annex F (approved via N1472/N1481/N1487)

F.10.11 Comparison macros

Relational operators and their corresponding comparison macros (7.12.14) produce equivalent result values, even if argument values are evaluated with extra range or precision. Thus, comparison macro arguments evaluated with extra precision or range are not converted to a narrower type, unless the wide evaluation method similarly narrows widened operands of relational operators. The standard wide evaluation methods, characterized by FLT_EVAL_METHOD equals 1 or 2 (, do not narrow widened operands of relational operators.

Add the following to the Rationale:

7.12.14 does not specify whether arguments of comparison macros evaluated with extra range or precision are converted to a narrower type (implementations have done both). F.10.11 specifies this behavior, in keeping with the Annex F goal of predictable floating-point behavior, and consistent with the macros' being "quiet" versions of the relational operators. For the standard wide-evaluation methods and most others, converting comparison macro arguments to long double would enable portable implementation of the F.10.11 specification.

Passing arguments of different types will cause them to be converted to the common real type. Passing integer arguments to the comparison macros may work as expected or fail in unknown ways (it is undefined behavior); the same applies if both arguments are imaginary types (however, cimag() may be used to convert the imaginary arguments to real type, which can then be compared).

Update DR 334 where (at the end) it says see WG14 document Nxxx to N1502.