[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Complex Numbers in C

Posted on 2007-11-26
6
Medium Priority
?
372 Views
Last Modified: 2010-04-15
Could someone give me sone help on working out complex root using C?

Here is my code so far:
#include <stdio.h>
#include <math.h>

double t1=0,t2=0,t3=0;
double quad1(double t1,double t2, double t3);
double quad2(double t1,double t2, double t3);

main()
{
      printf("Enter the X square coeff: ");
      scanf("%lf",&t1);
      printf("Enter the X coeff: ");
      scanf("%lf",&t2);
      printf("Enter the last term: ");
      scanf("%lf",&t3);

      printf("The 2 solutions of your equation are: %lf and %lf",quad1(t1,t2,t3),quad2(t1,t2,t3));

}

double quad1(double t1,double t2, double t3)
{
      double ans1=0,det1=0;

      det1= pow(t2,2) - (4*t1*t3); // b^2 - 4ac
      ans1= (((-1)*t2) + pow(det1,0.5))/(2*t1);
      return(ans1);
}

double quad2(double t1,double t2, double t3)
{
      double ans2=0,det2=0;
      
      det2= pow(t2,2) - (4*t1*t3); // b^2 - 4ac
      ans2= (((-1)*t2) - pow(det2,0.5))/(2*t1);
      return(ans2);
}

As you can see, it only works out real roots

Cheers
0
Comment
Question by:jonnytabpni
  • 3
  • 2
6 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 20349823
Hi jonnytabpni,

The C math routines that you're using (pow) doesn't deal with imaginary numbers.  To get the results you seek, you'll need to determine if the result will be real or imaginary, set a flag accordingly, and if imaginary, pass the absolute value of the data item.

The result is that the function will always return the root value, and you'll indicate in a separate field whether the result is real or imaginary.


Good Luck,
Kent
0
 

Author Comment

by:jonnytabpni
ID: 20349946
ok fair enough but im confused how to actually work out the complex roots once iv passed the data onto a seperate function.

cheers
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20349993
Hi jonnytabpni,

There are several ways to do this, though they all require a bit of effort.

C functions return one value.  Period.  So you can't get the function to return the real root and a real/imaginary flag.  The two most common ways around this are to return one or both values via function parameters, or to build a 'complex' structure that contains a the two critical components and pass it to the function.

typedef struct
{
  int    Imaginary;
  rel   Value;
) compex_t;



Good Luck,
Kent
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:jonnytabpni
ID: 20350368
ok i've got it:

#include <stdio.h>
#include <math.h>

double a,b,c;
int find_roots(double,double,double,double*,double*);

main()

{
      double x1,x2;
      int type;


      
      printf("Enter the 3 quadratic coeffs: ");
      scanf("%lf %lf %lf",&a,&b,&c);



      type = find_roots(a,b,c, &x1,&x2);

      if(type==0)
      {
            printf("The 2 solutions of your equation are: %lf and %lf",x1,x2);
            if((((-1)*b)/a) == (x1+x2)) printf("\nCheck Confirmed!"); //Check to see if real roots are correct
      }
      else
      {
            printf("The 2 complex roots of your equations are %lf + j%lf and %lf - j%lf",x1,fabs(x2),x1,fabs(x2));
      }


}


int find_roots(double a,double b, double c,double *x1,double *x2)

{

      double ans1=0,det=0;

      det= pow(b,2) - (4*a*c); // b^2 - 4ac
      
      if(det>0)
      {
            det = sqrt(det);

            *x1 = ((-1)*(b))/(2*a) + (det/(2*a));
            *x2 = ((-1)*(b))/(2*a) - (det/(2*a));
            return(0);
      }
      else
      {
            *x1 = ((-1)*(b))/(2*a);
            *x2 = det/(2*a);
            return(1);
      }
}

the only thing i need now is a way to check to see if the comple roots are correct
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 20350641
Hi jonnytabpni,

That should be pretty easy to do.  Build several test cases of known values.

 4  ^  2
 9  ^  2

 4  ^  1/2
 9  ^  1/2

If the values are correct (and they should be) rerun the test with negative coefficients.

Then, just to make sure:

  27 ^ 1/3
 -27 ^ 1/3


Good Luck,
Kent
0
 
LVL 5

Expert Comment

by:mandelia
ID: 20356731
Have you Looked into Complex.h.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question