Thread: imaginary numbers

  1. #16
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    You don't need to actually use complex numbers, you just need to know how they work and be clever with some radical simplification.

    The Quadratic Formula. The quadratic equation ax^2+bx+c has the solutions

    x=(-b +- sqr (b^2-4ac))/2a
    i is the square root of -1. The part of the quadratic equation that could make complex numbers necessary is (sqr(b^2-4ac)). If (b^2-4ac) < 0 then you're going to need some complex numbers. For the example I'm about to give, let a=8, b=0, and c=2.

    At this point, we're trying to find the square root of -64.

    In your code, if you just take the absolute value of b^2-4ac, take the square root of that, and slap an i after it (if b^2-4ac evaluates to <0) you'll be fine. Then, just stick that number into the quadratic finish it off, we have this:

    x=(-b +- sqr (b^2-4ac))/2a
    x=8i/16 or -8i/16
    x=.5i or -.5i

    See, never needed to program anything with complex numbers.

  2. #17
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    First of all, I'd like to say that the solution proposed by Blackrat is IMO the best for your problem. Secondly, there is a complex number class in the standard header files. I believe it's complex.h, but I may be wrong. If you can't find that library or have trouble using it, write your own class for complex numbers. I did that for a class once. Not difficult at all if you know how to work with complex numbers. The only data members you need are double a, b;

    Hmmm, I never know how to end these posts. Umm, good luck!

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #18
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Originally posted by Gil22
    alright i think i actually need to use complex therea way to declare these?
    Yeah, include complex and declare a std::complex. A lot of what you will need has already been implemented for you.

  4. #19
    Registered User
    Join Date
    Apr 2003
    an easier way to find ou if a number is imaginary is to find out if the equation under the radical is negative. If so then output an i after the output of the answer. If not then just evaluate it regullarly. A little bit of the code would look like this:
    float determinant;
    float root;
    determinant = b*b - (4*a*c);
    if(determinant < 0)
            determinant = determinant * -1;
            root1 =  (b + Sqrt(determinant) )/ 2;
            root 2 = (b - Sqrt(determinant) )/ 2;
            cout << "The answers are: " << root1 << "i, " << root2 << "i." << endl;
            root1 =  (b + Sqrt(determinant) )/ 2;
            root 2 = (b - Sqrt(determinant) )/ 2;
            cout << "The answers are: " << root1 << ", " << root2 << endl;
    There you go. Hope it helps you out.

    cout << My_Balls;
    cin >> Jo_Mouf;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Absolute value of imaginary numbers?
    By cpjust in forum A Brief History of
    Replies: 20
    Last Post: 08-11-2008, 05:52 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Real and Imaginary numbers
    By tetra in forum C++ Programming
    Replies: 13
    Last Post: 02-03-2003, 11:49 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM