Listing 1: math-complex.h

// Compatibility file for C99 and C++ complex from May 2003
// C/C++ Users Journal.
//
// This header can be included by either C99 or ANSI C++
// programs to allow both real and complex floating point
// arithmetic to be written in a common subset of C99 & C++.
// Note that overloads for both the real and complex math
// functions are available after this header has been
// included.

#ifndef MATH_COMPLEX_H_INCLUDED
#define MATH_COMPLEX_H_INCLUDED

#ifdef __cplusplus

#include <cmath>
#include <complex>

using namespace std;

typedef complex<float> float_complex;
typedef complex<double> double_complex;
typedef complex<long double> long_double_complex;

// Since C99 does not have type-generics for modf of float
// and long double and C++ does not have the suffixed names,
// provide suffixed names for C++.
// Note: Some C++ implementations do not yet support the
// overloaded modf function called by these inlines.
inline float modff(float xf, float *pxf)
    {return modf(xf, pxf);}
inline long double modfl(long double xl, long double *pxl)
    {return modf(xl, pxl);}

#else

// Note that <tgmath.h> includes <math.h> and <complex.h>
#include <tgmath.h>

typedef float complex float_complex;
typedef double complex double_complex;
typedef long double complex long_double_complex;

#define float_complex(r,i) ((float)(r) + ((float)(i))*I)
#define double_complex(r,i) ((double)(r) + ((double)(i))*I)
#define long_double_complex(r,i) ((long double)(r) + ((long double)(i))*I)

#define real(x) creal(x)
#define imag(x) cimag(x)
#define abs(x) fabs(x)
#define arg(x) carg(x)

#endif  // #ifdef __cplusplus

#endif  // #ifndef MATH_COMPLEX_H_INCLUDED