/* nag_cubic_roots (c02akc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * NAG C Library
 *
 * Mark 6, 2000.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagc02.h>


int main(void)
{
  double   *errest = 0, *zeroi = 0, *zeror = 0;
  double   r, s, t, u;
  Integer  i;
  Integer  exit_status = 0;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_cubic_roots (c02akc) Example Program Results\n\n");

  if
  (
    !(errest = NAG_ALLOC(3, double)) ||
    !(zeroi = NAG_ALLOC(3, double)) ||
    !(zeror = NAG_ALLOC(3, double))
  )
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%lf %lf %lf %lf ", &u, &r, &s, &t);

  /* nag_cubic_roots (c02akc).
   * Zeros of a cubic polynomial with real coefficients
   */
  nag_cubic_roots(u, r, s, t, zeror, zeroi, errest, &fail);
  if (fail.code == NE_NOERROR)
    {
      printf(
              "\n Roots of cubic equation             Error estimates\n");
      printf(
              "                                   (machine-dependent)\n\n");
      for (i = 0; i <= 2; ++i)
        {
          printf(" z = %10.5f %10.5f%s         %g\n",
                  zeror[i], zeroi[i], "*i", errest[i]);
        }
    }
  else
    {
      printf("Error from nag_cubic_roots (c02akc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

 END:
  NAG_FREE(errest);
  NAG_FREE(zeroi);
  NAG_FREE(zeror);
  return exit_status;
}