6

我想使用 C99 中定义的复数,但我需要支持不支持它的编译器(想到 MS 编译器)。

我不需要很多功能,在没有支持的编译器上实现所需的功能并不太难。但是我很难实现“类型”本身。理想情况下,我想做类似的事情:

#ifndef HAVE_CREAL
double creal(complex z)
{
/* .... */
}
#endif

#ifndef HAVE_CREALF
float creal(float complex z)
{
/* ... */
}
#endif

但我不确定如果编译器无法识别“float complex”,我会看到如何执行此操作。我实际上认为这是不可能的,但Dinkumware的 C 库似乎另有说明。解决办法是什么 ?我不介意使用函数/宏对类型进行操作,但我需要一种将值分配给复数的方法,并以与 C99 兼容的方式取回其实部/虚部。

解决方案

我最终做了这样的事情:

#ifdef USE_C99_COMPLEX
#include <complex.h>
typedef complex my_complex;
#else
typedef struct {
  double x, y;
} my_complex;
#endif

/*
 * Those unions are used to convert a pointer of my_complex to native C99
 * complex or our own complex type indenpendently on whether C99 complex
 * support is available
 */
#ifdef USE_C99_COMPLEX
typedef union {
    my_complex my_z;
    complex c99_z;
} __complex_to_c99_cast;
#else
typedef union {
    my_complex my_z;
    my_complex c99_z;
} __complex_to_c99_cast;
#endif

对于类型定义,如下定义一组复杂函数:

#ifndef HAVE_CREAL
double my_creal(my_complex z)
{
    union {
            my_complex z;
            double a[2];
    } z1;
    z1.z = z;
    return z1.a[0];
}
#endif

#ifdef HAVE_CREAL
my_complex my_creal(ny_complex z)
{
    __complex_to_c99_cast z1;
    __complex_to_c99_cast ret;

    z1.my_z = z;
    ret.c99_z = creal(z1.c99_z);
    return ret.npy_z;
}
#endif

这有点复杂,但这使我能够在可用时轻松重用 C lib 函数,并且可以通过代码生成器部分自动化。

4

2 回答 2

6

无论您做什么,都无法在非 C99 编译器中正确解析“浮点复杂”。因此,与其写那个,不如做一些 typedef。如果你只需要支持一种复杂类型就容易多了,所以我只用float complex.

首先,定义类型:

#if __STDC_VERSION__ >= 199901L
//using a C99 compiler
#include &lt;complex.h>
typedef float _Complex float_complex;
#else
typedef struct 
{
    float re, im;
} float_complex;
#endif

然后,我们需要能够创建复数,并模拟 creal 和 cimag。

#if __STDC_VERSION__ >= 199901L
//creal, cimag already defined in complex.h

inline complex_float make_complex_float(float real, float imag)
{
   return real + imag * I;
}
#else
#define creal(z) ((z).re)
#define cimag(z) ((z).im)

extern const complex_float complex_i; //put in a translation unit somewhere
#define I complex_i
inline complex_float make_complex_float(float real, float imag)
{
    complex_float z = {real, imag};
    return z;
}
#endif

接下来,编写包含加法、减法、乘法、除法和比较的函数。

#if __STDC_VERSION__ >= 199901L
#define add_complex(a, b) ((a)+(b))
//similarly for other operations
#else //not C99
inline float_complex add_complex(float_complex a, float_complex b)
{
  float_complex z = {a.re + b.re, a.im + b.im};
  return z;
}
//similarly for subtract, multiply, divide, and comparison operations.

Note that add_complex(c, 5) doesn't work in C89 mode in the above code, because the compiler doesn't know how to make 5 into a complex. This is a tricky problem to fix in C without compiler support -- you have to resort to tricks like the new tgmath.h uses, which are compiler-specific.

Unfortunately, the effect of all of this is that the nice C99 syntax like a+b to add complex numbers has to be written add_complex(a, b).

Another option (as another poster pointed to) is to use C++ std::complex on non-C99 compilers. This might be OK if you can wrap things in typedefs and #ifdefs. However, you'd require either C++ or C99.

于 2009-06-30T13:57:49.760 回答
-1

我在 msdn 网站上找到了一个库。这是一个链接。 http://msdn.microsoft.com/en-us/library/0352zzhd.aspx

我希望这会有所帮助。

于 2009-06-30T13:51:23.020 回答