Thread: complex numbers

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    complex numbers

    I'm successfully using <complex.h> functionality, but I'm a little confused. I'm trying to keep everything in float instead of double and I was uncertain what "I" was, so just to be sure, I defined "If":

    Code:
    complex float If;
    then,

    Code:
    If=(float)I;
    After doing this and replacing all my I's with If's, nothing worked, all my answers were wrong. Why would that be?

    Also, after switching from double to float, some things got incredibly slow, specifically:

    Code:
    temp+=data[xx]*(cosf(phase[xx])+I*sinf(phase[xx]));
    This is really bogged down. I was using "cexpf" which was incredibly slow too, so I tried this just in case.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    If=(float)I;
    umm... why?

    > After doing this and replacing all my I's with If's, nothing worked, all my answers were wrong. Why would that be?

    What were you trying to do?

    > Also, after switching from double to float, some things got incredibly slow, specifically:

    Code:
    temp+=data[xx]*(cosf(phase[xx])+I*sinf(phase[xx]));
    What was switched from double to float? What are all the other identifiers declared as?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I've not used C99 complex, but I would think it would have to be (complex float)I if anything. I'm pretty sure (float)I would end up being 0.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    Duh! Thanks.

    Anyone have thoughts on the slowness?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yes. I think you're not using the right function for the types. So... what are the identifiers declared as?

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Reading about complex.h I believe that I is just what it is in math, the imaginary number i. It says that it is actually a macro expanding to a const expression of type _Imaginary float and have a value of I.
    To declare a complex number with float values you, lets say z, you declare float complex z.
    Isn't this right? Haven't use it, just read the manual for complex.h.
    So the If is a float complex number. The I is the imaginary number i (or j). So the
    Code:
    If = (float)I
    makes no sense to me. The casting also makes no sense.

    Now, I don't know what you changed here:
    Code:
    temp+=data[xx]*(cosf(phase[xx])+I*sinf(phase[xx]));
    Do you actually mean If instead of I? If that is the case then correctly nothing works.


    But if my understanding is correct, then:
    1) temp should be declared as float complex
    2) I should be as it is

    Try actually declaring temp as double complex and then as float complex. Then check the size with sizeof(). If it is different then you know now that float complex is what you want, a complex number with float variables.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    Yeah, I made a mistake and should have said:

    Code:
    If=(complex float)I;
    I don't think this is necessary, but I just wanted to make sure I wasn't expanding to double.

    The line of code was a different thought. It could be "I" or "If". Using "If" is probably not necessary. Either way, it's a bottleneck in my code and I'm trying to figure out why.

    I'm actually using FFTW in my program, so my complex values are of type fftwf_complex:

    Code:
    fftwf_complex *data, temp,*answer;
    float *phase;
    
    ... //Big loop
    temp+=data[xx]*(cosf(phase[xx])+I*sinf(phase[xx]));
    
    ...
    answer[yy]=temp;
    That's the basic idea.

    I read someplace that FFTW uses some fast sine and cosine routines, but I haven't been able to figure out how to use them alone...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM