Document: WG14 N1419


Creation of complex value


Submitter: Fred J. Tydeman (USA)
Submission Date: 2009-10-28
Related documents: N818, SC22WG14.8195, N1399
Subject: Creation of complex value

Problem:

(x + y*I)
will NOT do the right thing if "I" is complex and "y" is NaN or infinity. It does work fine if "I" is imaginary. Users and library implementors have noticed this defect in the standard and have been surprised that there is no easy to use portable way to create a complex number.

WG14 paper N818 presented more details on why the problem exists as well as many possible solutions.

WG14 paper N1399 on complex multiply and divide resorted to a union of an array of 2 elements and a complex type as a means to create a complex value from a pair of real values. While this may be portable, it is not easy to use.

*((* double complex)&((double [2]){x, y})) might be another way to create a complex from a pair of reals.

Proposal.

Add 3 new functions to <complex.h> in section 7.3.9 Manipulation functions:

7.3.9.x The cmplx functions

Synopsis

  #include <complex.h>
  double      complex cmplx( double x, double y ); 
  float       complex cmplxf( float x, float y );
  long double complex cmplxl( long double x, long double y ); 

Description

The cmplx functions create a complex value from two real values. The value created is: x + i*y.

Returns

The cmplx functions return the complex value created from a pair of real values.

Add to 7.23 <tgmath.h>, paragraph 6, after cproj: cmplx.

Add to Annex B.2 <complex.h> after the cimag functions:

  double      complex cmplx( double x, double y ); 
  float       complex cmplxf( float x, float y );
  long double complex cmplxl( long double x, long double y ); 

Add to B.22 <tgmath.h>, after cproj: cmplx.

Add to G.6 <complex.h>, paragraph 5, after cproj: cmplx.

Add to the index: cmplx functions

Add to the rationale in the section on complex:

x + y*I will not create the expected value x + i*y if "I" is complex and "y" is a NaN; however, the expected value will be created if "I" is imaginary. Because of this, the cmplx() functions were added to C to allow a way to create a complex number from a pair of real values.