# NAG Library Routine Document

## 1Purpose

c02ajf determines the roots of a quadratic equation with real coefficients.

## 2Specification

Fortran Interface
 Subroutine c02ajf ( a, b, c, zsm, zlg,
 Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: a, b, c Real (Kind=nag_wp), Intent (Out) :: zsm(2), zlg(2)
C Header Interface
#include nagmk26.h
 void c02ajf_ (const double *a, const double *b, const double *c, double zsm[], double zlg[], Integer *ifail)

## 3Description

c02ajf attempts to find the roots of the quadratic equation $a{z}^{2}+bz+c=0$ (where $a$, $b$ and $c$ are real coefficients), by carefully evaluating the ‘standard’ closed formula
 $z=-b±b2-4ac 2a .$
It is based on the routine QDRTC from Smith (1967).
Note:  it is not necessary to scale the coefficients prior to calling the routine.

## 4References

Smith B T (1967) ZERPOL: a zero finding algorithm for polynomials using Laguerre's method Technical Report Department of Computer Science, University of Toronto, Canada

## 5Arguments

1:     $\mathbf{a}$ – Real (Kind=nag_wp)Input
On entry: must contain $a$, the coefficient of ${z}^{2}$.
2:     $\mathbf{b}$ – Real (Kind=nag_wp)Input
On entry: must contain $b$, the coefficient of $z$.
3:     $\mathbf{c}$ – Real (Kind=nag_wp)Input
On entry: must contain $c$, the constant coefficient.
4:     $\mathbf{zsm}\left(2\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the real and imaginary parts of the smallest root in magnitude are stored in ${\mathbf{zsm}}\left(1\right)$ and ${\mathbf{zsm}}\left(2\right)$ respectively.
5:     $\mathbf{zlg}\left(2\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the real and imaginary parts of the largest root in magnitude are stored in ${\mathbf{zlg}}\left(1\right)$ and ${\mathbf{zlg}}\left(2\right)$ respectively.
6:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{a}}=0.0$. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the root $-c/b$ and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{a}}=0.0$ and ${\mathbf{b}}=0.0$. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the largest machine representable number (see x02alf) and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{a}}=0.0$ and the root $-c/b$ overflows. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the largest machine representable number (see x02alf) and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{c}}=0.0$ and the root $-b/a$ overflows. In this case, both ${\mathbf{zsm}}\left(1\right)$ and ${\mathbf{zsm}}\left(2\right)$ contain zero.
${\mathbf{ifail}}=5$
On entry, $b$ is so large that ${b}^{2}$ is indistinguishable from ${b}^{2}-4ac$ and the root $-b/a$ overflows. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the root $-c/b$ and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.
If ${\mathbf{ifail}}>{\mathbf{0}}$ on exit, then ${\mathbf{zlg}}\left(1\right)$ contains the largest machine representable number (see x02alf) and ${\mathbf{zlg}}\left(2\right)$ contains zero.

## 7Accuracy

If ${\mathbf{ifail}}={\mathbf{0}}$ on exit, then the computed roots should be accurate to within a small multiple of the machine precision except when underflow (or overflow) occurs, in which case the true roots are within a small multiple of the underflow (or overflow) threshold of the machine.

## 8Parallelism and Performance

c02ajf is not threaded in any implementation.

None.

## 10Example

This example finds the roots of the quadratic equation ${z}^{2}+3z-10=0$.

### 10.1Program Text

Program Text (c02ajfe.f90)

### 10.2Program Data

Program Data (c02ajfe.d)

### 10.3Program Results

Program Results (c02ajfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017