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

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

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

  mpfr_inits2 (prec, x, y, z, (void *) 0);
  mpfr_set_ui (x, 7, MPFR_RNDN);
  mpfr_set_ui (y, 2, MPFR_RNDN);
  mpfr_root (y, y, 5, MPFR_RNDN);  /* y = 2^(1/5) */
  mpfr_add (x, x, y, MPFR_RNDN);
  mpfr_set_ui (z, 8, MPFR_RNDN);
  mpfr_root (z, z, 5, MPFR_RNDN);
  mpfr_mul_ui (z, z, 5, MPFR_RNDN);
  mpfr_sub (x, x, z, MPFR_RNDN);
  mpfr_root (x, x, 3, MPFR_RNDN);
  mpfr_set_ui (z, 4, MPFR_RNDN);
  mpfr_root (z, z, 5, MPFR_RNDN);
  mpfr_add (x, x, z, MPFR_RNDN);
  mpfr_sub (x, x, y, MPFR_RNDN);
  printf ("Precision %4d:  ", prec);
  mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);
  putchar ('\n');
  mpfr_clears (x, y, z, (void *) 0);
}

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

  if (argc < 2)
    {
      fprintf (stderr, "Usage: pb-roots <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: incorrect precision %d\n", prec);
          exit (EXIT_FAILURE);
        }
      roots (prec);
    }

  return 0;
}

/* results ~= 1 */

