Solve Quadratic Equation in C++

陌路散爱 提交于 2019-12-04 05:17:34

Something like this would work:

struct complex { double r,i; }
struct pair<T> { T p1, p2; }

pair<complex> GetResults(double a, double b, double c)
{
  pair<complex> result={0};

  if(a<0.000001)    // ==0
  {
    if(b>0.000001)  // !=0
      result.p1.r=result.p2.r=-c/b;
    else
      if(c>0.00001) throw exception("no solutions");
    return result;
  }

  double delta=b*b-4*a*c;
  if(delta>=0)
  {
    result.p1.r=(-b-sqrt(delta))/2/a;
    result.p2.r=(-b+sqrt(delta))/2/a;
  }
  else
  {
    result.p1.r=result.p2.r=-b/2/a;
    result.p1.i=sqrt(-delta)/2/a;
    result.p2.i=-sqrt(-delta)/2/a;
  }

  return result;
}

That way you get the results in a similar way for both real and complex results (the real results just have the imaginary part set to 0). Would look even prettier with boost!

edit: fixed for the delta thing and added a check for degenerate cases like a=0. Sleepless night ftl!

An important note to all of this. The solutions shown in these responses and in the original question are not robust.

The well known solution (-b +- sqrt(b^2 - 4ac)) / 2a is known to be non-robust in computation when ac is very small compered to b^2, because one is subtracting two very similar values. It is better to use the lesser known solution 2c / (-b -+ sqrt(b^2 -4ac)) for the other root.

A robust solution can be calculated as:

temp = -0.5 * (b + sign(b) * sqrt(b*b - 4*a*c);
x1 = temp / a;
x2 = c / temp;

The use of sign(b) ensures that we are not subtracting two similar values.

For the OP, modify this for complex numbers as shown by other posters.

You more or less have it, just check to see if the part that's inside the square root is negative and then keep track of that separately in your reductions.

You could basically just use std::complex<float> instead of float to get support for complex numbers.

Nicking the idea from Blindy:

typedef std::complex<double> complex;
using std::pair;
pair<complex> GetResults(double a, double b, double c)
{
  double delta=(b*b-4*a*c);
  double inv_2a = 1/2/a;
  if(delta >= 0) {
    double root = sqrt(delta);
    return std::make_pair(
        complex((-b-root)*inv_2a),
        complex((-b+root)*inv_2a);
  } else {
    double root = sqrt(-delta);
    return std::make_pair(
        complex(-b*inv_2a, -root*inv_2a)),
        complex(-b*inv_2a, +root*inv_2a)));
  }
}

I tried the program without using 'math.h' header and also tried different logic...but my program can answer only those quadratic equations which have coefficient of 'x square' as one ..... and where coefficient of 'x' can be expressed as an addition of two numbers which are factors of constant term. eg. x square +8x+16; x square +7x+12; etc. here 8=4+4 & 16=4*4; here coefficient of x can be expressed as an addition of two numbers which are factors of constant term 16... I myself is not fully satisfied with it but tried something different, without using the formula for solving quadratic equation. code is;

        #include<iostream.h>
        #include<conio.h>
         class quadratic
              {
                int b,c ;
                float l,k;
                public:
               void solution();
              };
        void quadratic::solution()
             {
                 cout<<"Enter coefficient of x and the constant term of the quadratic eqn where coefficient of x square is one";
                 cin>>b>>c;

                 for(l=1;l<b;l++)
                  {
                   for(k=1;k<b;k++)
                    {
                     if(l+k==b&&l*k==c)
                        {
                          cout<<"x="<<-l<<"\t"<<"or"<<"\t"<<"x="<<-k;
                          cout<<"\n";
                         }
                    }
                }
            }
              void main()
                 {
                  quadratic a;
                   clrscr();
                  a.solution();
                  getch();
                 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!