/* $Id: pb-roots-dir.c 47490 2011-11-10 17:07:08Z vinc17/ypig $ */

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <mpfr.h>

static const mp_rnd_t rnd[3] = { MPFR_RNDD, MPFR_RNDN, MPFR_RNDU };

static void roots (int prec)
{
  mpfr_t x, y;
  int i;

  mpfr_inits2 (prec, x, y, (void *) 0);
  for (i = 0; i < 3; i++)
    {
      mpfr_set_ui (x, 7, rnd[i]);
      mpfr_set_ui (y, 2, rnd[i]);
      mpfr_root (y, y, 5, rnd[i]);
      mpfr_add (x, x, y, rnd[i]);
      mpfr_set_ui (y, 8, rnd[2-i]);
      mpfr_root (y, y, 5, rnd[2-i]);
      mpfr_mul_ui (y, y, 5, rnd[2-i]);
      mpfr_sub (x, x, y, rnd[i]);
      mpfr_root (x, x, 3, rnd[i]);
      mpfr_set_ui (y, 4, rnd[i]);
      mpfr_root (y, y, 5, rnd[i]);
      mpfr_add (x, x, y, rnd[i]);
      mpfr_set_ui (y, 2, rnd[2-i]);
      mpfr_root (y, y, 5, rnd[2-i]);
      mpfr_sub (x, x, y, rnd[i]);
      printf (i ? "                 " : "Precision %4d:  ", prec);
      mpfr_out_str (stdout, 10, 0, x, rnd[i]);
      putchar ('\n');
    }
  mpfr_clears (x, y, (void *) 0);
}

int main (int argc, char *argv[])
{
  int i;

  if (argc < 2)
    {
      fprintf (stderr, "Usage: pb-roots-dir <prec> ...\n");
      exit (EXIT_FAILURE);
    }

  for (i = 1; i < argc; i++)
    {
      int prec;

      prec = atoi (argv[i]);
      if (prec < MPFR_PREC_MIN || prec > MPFR_PREC_MAX)
        {
          fprintf (stderr, "pb-roots-dir: incorrect precision %d\n", prec);
          exit (EXIT_FAILURE);
        }
      roots (prec);
    }

  return 0;
}

/* results ~= 1 */

