Esempio n. 1
0
int
main (int argc, char *argv[])
{
  mpfr_t x;
  FILE *f;
  int i;
  tests_start_mpfr ();

  mpfr_init (x);

  mpfr_set_prec (x, 15);
  f = src_fopen ("inp_str.data", "r");
  if (f == NULL)
    {
      printf ("Error, can't open inp_str.data\n");
      exit (1);
    }
  i = mpfr_inp_str (x, f, 10, GMP_RNDN);
  if (i == 0 || mpfr_cmp_ui (x, 31415))
    {
      printf ("Error in reading 1st line from file inp_str.data (%d)\n", i);
      mpfr_dump (x);
      exit (1);
    }
  getc (f);
  i = mpfr_inp_str (x, f, 10, GMP_RNDN);
  if ((i == 0) || mpfr_cmp_ui (x, 31416))
    {
      printf ("Error in reading 2nd line from file inp_str.data (%d)\n", i);
      mpfr_dump (x);
      exit (1);
    }
  getc (f);
  i = mpfr_inp_str (x, f, 10, GMP_RNDN);
  if (i != 0)
    {
      printf ("Error in reading 3rd line from file inp_str.data (%d)\n", i);
      mpfr_dump (x);
      exit (1);
    }
  fclose (f);

  mpfr_clear (x);

  tests_end_mpfr ();
  return 0;
}
Esempio n. 2
0
static void
tpl_read_mpfr_mantissa (mpc_datafile_context_t* datafile_context, mpfr_ptr x)
{
   if (datafile_context->nextchar == EOF) {
      printf ("Error: Unexpected EOF when reading mpfr mantissa "
              "in file '%s' line %lu\n",
              datafile_context->pathname, datafile_context->line_number);
      exit (1);
   }
   ungetc (datafile_context->nextchar, datafile_context->fd);
   if (mpfr_inp_str (x, datafile_context->fd, 0, GMP_RNDN) == 0) {
      printf ("Error: Impossible to read mpfr mantissa "
              "in file '%s' line %lu\n",
              datafile_context->pathname, datafile_context->line_number);
      exit (1);
   }
   datafile_context->nextchar = getc (datafile_context->fd);
   tpl_skip_whitespace_comments (datafile_context);
}
Esempio n. 3
0
static void
read_mpfr_mantissa (FILE *fp, mpfr_ptr x)
{
   if (nextchar == EOF) {
      printf ("Error: Unexpected EOF when reading mpfr mantissa "
              "in file '%s' line %lu\n",
              pathname, line_number);
      exit (1);
   }
   ungetc (nextchar, fp);
   if (mpfr_inp_str (x, fp, 0, MPFR_RNDN) == 0) {
      printf ("Error: Impossible to read mpfr mantissa "
              "in file '%s' line %lu\n",
              pathname, line_number);
      exit (1);
   }
   nextchar = getc (fp);
   skip_whitespace_comments (fp);
}
Esempio n. 4
0
File: tests.c Progetto: Kirija/XPIR
/* Check data in file f for function foo, with name 'name'.
   Each line consists of the file f one:

   xprec yprec rnd x y

   where:

   xprec is the input precision
   yprec is the output precision
   rnd is the rounding mode (n, z, u, d, a, Z, *)
   x is the input (hexadecimal format)
   y is the expected output (hexadecimal format) for foo(x) with rounding rnd

   If rnd is Z, y is the expected output in round-toward-zero, and the
   four directed rounding modes are tested, then the round-to-nearest
   mode is tested in precision yprec-1. This is useful for worst cases,
   where yprec is the minimum value such that one has a worst case in a
   directed rounding mode.

   If rnd is *, y must be an exact case. All the rounding modes are tested
   and the ternary value is checked (it must be 0).
 */
void
data_check (const char *f, int (*foo) (FLIST), const char *name)
{
  FILE *fp;
  int xprec, yprec;  /* not mpfr_prec_t because of the fscanf */
  mpfr_t x, y, z;
  mpfr_rnd_t rnd;
  char r;
  int c;

  fp = fopen (f, "r");
  if (fp == NULL)
    fp = src_fopen (f, "r");
  if (fp == NULL)
    {
      char *v = (char *) MPFR_VERSION_STRING;

      /* In the '-dev' versions, assume that the data file exists and
         return an error if the file cannot be opened to make sure
         that such failures are detected. */
      while (*v != '\0')
        v++;
      if (v[-4] == '-' && v[-3] == 'd' && v[-2] == 'e' && v[-1] == 'v')
        {
          printf ("Error: unable to open file '%s'\n", f);
          exit (1);
        }
      else
        return;
    }

  mpfr_init (x);
  mpfr_init (y);
  mpfr_init (z);

  while (!feof (fp))
    {
      /* skip whitespace, for consistency */
      if (fscanf (fp, " ") == EOF)
        {
          if (ferror (fp))
            {
              perror ("data_check");
              exit (1);
            }
          else
            break;  /* end of file */
        }

      if ((c = getc (fp)) == EOF)
        {
          if (ferror (fp))
            {
              perror ("data_check");
              exit (1);
            }
          else
            break;  /* end of file */
        }

      if (c == '#') /* comment: read entire line */
        {
          do
            {
              c = getc (fp);
            }
          while (!feof (fp) && c != '\n');
        }
      else
        {
          ungetc (c, fp);

          c = fscanf (fp, "%d %d %c", &xprec, &yprec, &r);
          MPFR_ASSERTN (xprec >= MPFR_PREC_MIN && xprec <= MPFR_PREC_MAX);
          MPFR_ASSERTN (yprec >= MPFR_PREC_MIN && yprec <= MPFR_PREC_MAX);
          if (c == EOF)
            {
              perror ("data_check");
              exit (1);
            }
          else if (c != 3)
            {
              printf ("Error: corrupted line in file '%s'\n", f);
              exit (1);
            }

          switch (r)
            {
            case 'n':
              rnd = MPFR_RNDN;
              break;
            case 'z': case 'Z':
              rnd = MPFR_RNDZ;
              break;
            case 'u':
              rnd = MPFR_RNDU;
              break;
            case 'd':
              rnd = MPFR_RNDD;
              break;
            case '*':
              rnd = MPFR_RND_MAX; /* non-existing rounding mode */
              break;
            default:
              printf ("Error: unexpected rounding mode"
                      " in file '%s': %c\n", f, (int) r);
              exit (1);
            }
          mpfr_set_prec (x, xprec);
          mpfr_set_prec (y, yprec);
          if (mpfr_inp_str (x, fp, 0, MPFR_RNDN) == 0)
            {
              printf ("Error: corrupted argument in file '%s'\n", f);
              exit (1);
            }
          if (mpfr_inp_str (y, fp, 0, MPFR_RNDN) == 0)
            {
              printf ("Error: corrupted result in file '%s'\n", f);
              exit (1);
            }
          if (getc (fp) != '\n')
            {
              printf ("Error: result not followed by \\n in file '%s'\n", f);
              exit (1);
            }
          /* Skip whitespace, in particular at the end of the file. */
          if (fscanf (fp, " ") == EOF && ferror (fp))
            {
              perror ("data_check");
              exit (1);
            }
          if (r == '*')
            {
              int rndint;
              RND_LOOP (rndint)
                test5rm (foo, x, y, z, (mpfr_rnd_t) rndint, 2, name);
            }
          else
            test5rm (foo, x, y, z, rnd, r != 'Z', name);
        }
    }

  mpfr_clear (x);
  mpfr_clear (y);
  mpfr_clear (z);

  fclose (fp);
}
Esempio n. 5
0
int
main (int argc, char *argv[])
{
    mpfr_t x;
    mpfr_t y;
    FILE *f;
    int i;
    tests_start_mpfr ();

    mpfr_init (x);
    mpfr_init (y);

    mpfr_set_prec (x, 15);
    f = src_fopen ("inp_str.data", "r");
    if (f == NULL)
    {
        printf ("Error, can't open inp_str.data\n");
        exit (1);
    }
    i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
    if (i == 0 || mpfr_cmp_ui (x, 31415))
    {
        printf ("Error in reading 1st line from file inp_str.data (%d)\n", i);
        mpfr_dump (x);
        exit (1);
    }
    getc (f);
    i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
    if ((i == 0) || mpfr_cmp_ui (x, 31416))
    {
        printf ("Error in reading 2nd line from file inp_str.data (%d)\n", i);
        mpfr_dump (x);
        exit (1);
    }
    getc (f);
    i = mpfr_inp_str (x, f, 10, MPFR_RNDN);
    if (i != 0)
    {
        printf ("Error in reading 3rd line from file inp_str.data (%d)\n", i);
        mpfr_dump (x);
        exit (1);
    }

    mpfr_set_prec (x, 53);
    mpfr_set_prec (y, 53);
    mpfr_set_str (y, "1.0010010100001110100101001110011010111011100001110010e226",
                  2, MPFR_RNDN);
    for (i = 2; i < 63; i++)
    {
        getc (f);
        if (mpfr_inp_str (x, f, i, MPFR_RNDN) == 0 || !mpfr_equal_p (x, y))
        {
            printf ("Error in reading %dth line from file inp_str.data\n", i+2);
            mpfr_dump (x);
            exit (1);
        }
        mpfr_set_ui (x, 0, MPFR_RNDN);
    }

    fclose (f);

    mpfr_clear (x);
    mpfr_clear (y);

    tests_end_mpfr ();
    return 0;
}