Introduce complex literals

Jens Gustedt, INRIA and ICube, France

2024-07-10

target

integration into IS ISO/IEC 9899:202y

document history

document number date comment
n3298 202407 remove tg features
factorize the syntax
n3241 202404 original proposal

license

CC BY, see https://creativecommons.org/licenses/by/4.0

1 Problem description

The current integration of complex types into the C standard is a bit half-hearted:

A discussion during the June 2024 remote meeting was not able to produce consensus on the form of the feature as it was proposed in n3241, and the arguments given often seemed to have mixed up the “i” that is used as a literal suffix and I, the macro.

One important objection that was brought forward against the original paper was that it was too restrictive compared to existing practice, which basically allows all combinatoric of combination of complex suffix and binary floating point.

By combining _Generic, casts to real types and complex arithmetic it is easy to provide expressions that result in the real, imaginary, complex conjugate and complex projective values, and that are suitable as arithmetic constant expressions. But these are not as easy to implement as it seems at the first sight, because they should properly take care of border cases such as imaginary parts that are positive or negative zeros, infinities or NaNs. The original paper proposed type-generic features in <tgmath.h> but we removed this because there was no consensus, yet. Our intent is to repropose this later, once the dust of the changes concerning imaginary types and Annex G has settled.

2 Proposed additions to C2y

2.1 Complex literals

2.1.1 C compilers

Several modern C compilers (gcc, clang, XL C) already have suffixes with i or j for complex literals since decades. In general, similar to the suffix component u for unsigned integer literals, i and j may precede or follow f and l and capitalized versions are also supported.

2.1.2 Other languages

2.1.3 Proposal

Users coming from different backgrounds will have different expectations; in particular probably about the half of the community that intensively use complex types come from engineering and signal processing; there the convention is to use a “j” or “J” suffix. Since the compilers with these features already support the full combinatorics of these suffixes, we propose to integrate this convention into C2y as is.

3 Questions

  1. Does WG14 want to integrate complex number literals as proposed in n3298 into C2y?
  2. Does WG14 want to integrate complex number literals as proposed in n3298 but where the allowed suffixes for complex-suffix are reduced to i and I into C2y?

4 Wording

Removals are in stroke-out red, additions in underlined green.

4.1 Changes to clause 6.4.4.3

In p1, change the current definition of the term floating-suffix to real-floating-suffix and add a new definition for that term and for complex-suffix.

real-floating-suffix: one of

f l F L df dd dl DF DD DL

floating-suffix:
real-floating-suffix complex-suffixopt
complex-suffix real-floating-suffixopt

complex-suffix: one of

i I j J

In the “Constraints” section change and then add at the end or after p2 (at the discretion of the editors):

A real floating suffix df, dd, dl, DF, DD, or DL shall not be used in a hexadecimal floating constant. A floating suffix shall not designate a type that the implementation does not provide.

Modify the beginning of paragraph p5

The presence of a complex suffix indicates that the type of the literal is the complex type that corresponds to the type of the literal where the complex suffix is removed. The described floating value of such a complex literal designates the imaginary part of an arithmetic constant expression of the indicated complex type, the real part is (positive) zero.FNT) An unsuffixed floating constant has type double. If suffixed by a real floating suffix it has a type according to the following table: …

FNT) Implementations that define the macro __STDC_NO_COMPLEX__ may not support complex types. For these implementations, a literal with a complex suffix presents a constraint violation.

4.2 Changes to clauses 7.24.1.5, 7.24.1.6, 7.24.1.7, 7.31.4.1.2, and 7.31.4.1.3

These strtoXXX and wcstoXXX functions all refer to “floating constant” but should never designate a complex value if an “i”, “I”, “j”, or “J” follows a real floating constant. For backwards compatibility, parsing of the number should always stop before such a character. Change all of these occurrences in these clauses

real floating constant …

Note that in other clauses, the term “floating constant” is appropriate and should not be changed.

Note also that with already voted changes the term will soon be “floating literal” instead of “floating constant” and the changes proposed here should be applied analogously.

4.3 Changes to clause H .5.2

Change the beginning of p2 to

This subclause expands real-floating-suffix (6.4.4.2) to also include: …

Add a new note after p7 to

NOTE 2 It follows from here and from 6.4.4.2 that the valid combinations of such an expanded real float suffix with a complex suffix correspond to the supported complex types as described in H .2.5.