Index of Functions: A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X 
Index Page
rquad_c

Table of contents
Procedure
Abstract
Required_Reading
Keywords
Brief_I/O
Detailed_Input
Detailed_Output
Parameters
Exceptions
Files
Particulars
Examples
Restrictions
Literature_References
Author_and_Institution
Version
Index_Entries

Procedure

   rquad_c ( Roots of a quadratic equation ) 

   void rquad_c ( SpiceDouble  a,
                  SpiceDouble  b,
                  SpiceDouble  c,
                  SpiceDouble  root1[2],
                  SpiceDouble  root2[2] )

Abstract

   Find the roots of a quadratic equation.

Required_Reading

   None.

Keywords

   MATH
   POLYNOMIAL
   ROOT


Brief_I/O

   VARIABLE  I/O  DESCRIPTION
   --------  ---  --------------------------------------------------
   a          I   Coefficient of quadratic term.
   b          I   Coefficient of linear term.
   c          I   Constant.
   root1      O   Root built from positive discriminant term.
   root2      O   Root built from negative discriminant term.

Detailed_Input

   a,
   b,
   c           are the coefficients of a quadratic polynomial

                    2
                  ax   +   bx   +   c.

Detailed_Output

   root1,
   root2       are the roots of the equation,

                     2
                   ax   +   bx   +   c   =  0.


               root1 and root2 are both arrays of length 2. The
               first element of each array is the real part of a
               root; the second element contains the complex part
               of the same root.

               When a is non-zero, root1 represents the root

                                _____________
                               /  2
                  - b   +    \/  b    -   4ac
                  ---------------------------
                                2a


               and root2 represents the root

                                _____________
                               /  2
                  - b   -    \/  b    -   4ac
                  --------------------------- .
                                2a


               When a is zero and b is non-zero, root1 and root2
               both represent the root

                  - c / b.

Parameters

   None.

Exceptions

   1)  If the input coefficients `a' and `b' are both zero, the error
       SPICE(DEGENERATECASE) is signaled. The output arguments are not
       modified.

Files

   None.

Particulars

   None.

Examples

   The numerical results shown for these examples may differ across
   platforms. The results depend on the SPICE kernels used as
   input, the compiler and supporting libraries, and the machine
   specific arithmetic implementation.

   1) Humor us and suppose we want to compute the "golden ratio."

      The quantity `r' is defined by the equation

         1/r = r/(1-r),

      which is equivalent to

          2
         r   +  r  -  1  =  0.

      The following code example does the job.


      Example code begins here.


      /.
         Program rquad_ex1
      ./
      #include <stdio.h>
      #include "SpiceUsr.h"

      int main( )
      {
         /.
         Local variables.
         ./
         SpiceDouble             root1 [ 2 ];
         SpiceDouble             root2 [ 2 ];

         /.
         Compute "golden ratio."  The root we want,

                    ___
                   /
            -1 + \/  5
            -----------,
                 2


         is contained in root1.
         ./
         rquad_c ( 1., 1., -1., root1, root2 );

         /.
         Print the results.
         ./
         printf ( "The \"golden ratio\" is %f\n", root1[0] );

         return ( 0 );

      }


      When this program was executed on a Mac/Intel/cc/64-bit
      platform, the output was:


      The "golden ratio" is 0.618034


   2) Calculate the roots of the following quadratic equation:

          2
         x  + 2x + 3 = 0

      Example code begins here.


      /.
         Program rquad_ex2
      ./
      #include <stdio.h>
      #include "SpiceUsr.h"

      int main( )
      {
         /.
         Local variables.
         ./
         SpiceDouble             root1 [ 2 ];
         SpiceDouble             root2 [ 2 ];

         /.
         Let's do one with imaginary roots just for fun.
         ./

         rquad_c ( 1.,  2.,  3.,  root1,  root2 );

         printf ( "Root #1: %12.7f %12.7f\n", root1[0], root1[1] );
         printf ( "Root #2: %12.7f %12.7f\n", root2[0], root2[1] );

         return ( 0 );
      }


      When this program was executed on a Mac/Intel/cc/64-bit
      platform, the output was:


      Root #1:   -1.0000000    1.4142136
      Root #2:   -1.0000000   -1.4142136

Restrictions

   1)  No checks for overflow of the roots are performed.

Literature_References

   None.

Author_and_Institution

   N.J. Bachman        (JPL)
   J. Diaz del Rio     (ODC Space)

Version

   -CSPICE Version 1.0.1, 04-AUG-2021 (JDR)

       Edited the header to comply with NAIF standard.

       Created complete code examples from existing code fragments.

   -CSPICE Version 1.0.0, 13-JUN-1999 (NJB)

Index_Entries

   roots of a quadratic equation
Fri Dec 31 18:41:11 2021