Thread: complex numbers in c++ (and c)

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    complex numbers in c++ (and c)

    I am about to start a small signal processing personal project, and so I set out to define a complex number class along with some simple operators to go along with it.

    As I did so, the first thing I noticed was that as I typed in the word "complex", Code::Blocks (the IDE I am using) highlighted the word for me as if it was a standard datatype.

    This confused me, as I have never heard of complex numbers being part of the standard. So I did a little research and found this:

    Complex - Using the GNU Compiler Collection (GCC)

    and also this:

    complex - C++ Reference

    It seems that there is a C and a C++ implementation. Are either of these standard? Are they proprietary? Why is my head about to explode?
    My Website

    "Circular logic is good because it is."

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    complex was added to C in C99, and was in C++ since I don't know when.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    man i must be living in a cave. been coding in c++ since at least 2000 and i never knew this.
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    complex numbers in Code Blocks

    hello..i have my graduated thesis where i want to use some complex numbers - to make a table and multiple the tables together- and i searched the internet and find out that Code Blocks can do that..so i downloaded the codeblocks-8.02mingw-setup from it's site and done the installation.. but i have this simple code with can not be compiled..
    Code:
    #include <stdio.h>
    #include <complex.h>
    
    int main ()
    {
    complex a=5+2i;
    complex b=4+9i;
    complex c=a+b;
    printf("ok!\n");
    
    return 0;
    }
    when i push build i get this error..

    cc1.exe: error: unrecognized command line option "-Wfatal-errors"


    do i need to do some configurations to compile and run this??if yes please tell me..it's very important!!thank you!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    cc1?

    Either way, under options -> compiler & linker settings, there's a large pile of checkboxes; uncheck the one that says fatal-errors.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    By the way, if this is really supposed to be C++, then I would expect something like this instead:
    Code:
    #include <complex>
    #include <iostream>
    
    int main()
    {
        using std::complex;
        complex<double> a(5, 2);
        complex<double> b(4, 9);
        complex<double> c(a + b);
        std::cout << "ok!\n";
        return 0;
    }
    I am not familiar with the complex number library in standard C, but the 5+2i mathematical notation should result in a compile error when you try to use it in C (or C++).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    1
    With complex.h you need to use capital I instead of lowercase i for imaginary numbers. You also need to explicitly multiply I by the imaginary coefficient.
    For example:
    Code:
    complex a=5+2*I;

Popular pages Recent additions subscribe to a feed