| rquad_c |
|
Table of contents
Procedure
rquad_c ( Roots of a quadratic equation )
void rquad_c ( SpiceDouble a,
SpiceDouble b,
SpiceDouble c,
SpiceDouble root1[2],
SpiceDouble root2[2] )
AbstractFind the roots of a quadratic equation. Required_ReadingNone. KeywordsMATH POLYNOMIAL ROOT Brief_I/OVARIABLE 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.
ParametersNone. Exceptions
1) If the input coefficients `a' and `b' are both zero, the error
SPICE(DEGENERATECASE) is signaled. The output arguments are not
modified.
FilesNone. ParticularsNone. 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
Restrictions1) No checks for overflow of the roots are performed. Literature_ReferencesNone. Author_and_InstitutionN.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_Entriesroots of a quadratic equation Link to routine rquad_c source file rquad_c.c |
Fri Dec 31 18:41:11 2021