C complex number usage

Chris Linstid clinstid at gmail.com
Sun Jan 6 12:58:46 EST 2008


The problem it's complaining about is that mymean and mystdev are
variables of type "double" and gaussrand() returns a "double"... and
you're trying to assign the result to g[ii] which is type "double
_Complex".  You could try casting the result to "double _Complex" like
this:

g[ii] = (double _Complex) (mymean*(1+I) + mystdev*gaussrand() + I *
mystdev * gaussrand());

However, I'm not sure if that's exactly what you want because I don't
know what will happen to the value when you cast it as I'm not too
familiar with the complex number support in C.

Hope that helps.

     - Chris

On Jan 6, 2008 12:25 PM, Bruce Labitt <bruce.labitt at verizon.net> wrote:
> Hopefully a simple question.  I'm trying to write a C program that uses
> complex numbers.  (I'm a  "C" illiterate.  Just wrote my second
> program...)  Reading the header file complex.h hasn't helped too much.
> Can someone give me a hint on how to "fix" this?  Most of the references
> on C are *ancient* (like me) and don't cover the C99 complex number
> stuff.  Please ignore my awful coding style.
>
> #include <math.h>
> #include <time.h>
> #include <complex>
> #include <fftw3.h>
>
> double gaussrand();
> int N;   /* length of FFT */
> int ii;  /* index */
> N = 1024;
> double _Complex g[N];
> double mymean, mystdev;
> double _Complex I;
>
> // assign mymean and mystdev here...
>
> // Create array of complex random numbers
>    for (ii=0; ii<N; ii++)
>    {
>        g[ii] = mymean*(1+I) + mystdev*gaussrand() + I * mystdev *
> gaussrand();
>    }
> // End complex random number generation
>
> The compiler barfs on the g[ii] assignment...
> 41 C:\Dev-Cpp\myproj\main_tb_fftw.cpp cannot convert `double
> __complex__[((unsigned int)((int)N))]' to `double (*)[2]' in assignment
>
> Hopefully someone can point me in the write direction.  TIA.
> _______________________________________________
> gnhlug-discuss mailing list
> gnhlug-discuss at mail.gnhlug.org
> http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/
>


More information about the gnhlug-discuss mailing list