Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib stdlibug 20.3 Example Program - Roots of a Polynomial
Personal tools
Document Actions

20.3 Example Program - Roots of a Polynomial



Click on the banner to return to the user guide home page.

20.3 Example Program - Roots of a Polynomial

Obtaining the Sample Program

The roots of a polynomial a x2 + b x + c = 0 are given by the formula:

x = (-b _ sqrt(b2 - 4ac))/2a

The following program takes as input three double precision numbers, and returns the complex roots as a pair of values.

typedef complex<double> dcomplex;

pair<dcomplex, dcomplex> quadratic
      (dcomplex a, dcomplex b, dcomplex c)
         // return the roots of a quadratic equation
{
   dcomplex root = sqrt(b * b - 4.0 * a * c);
   a *= 2.0;
   return make_pair(
      (-b + root)/a, 
      (-b - root)/a);
}


©Copyright 1996, Rogue Wave Software, Inc.


Powered by Plone This site conforms to the following standards: