Blame tests/mpf/t-add.c

Packit 5c3484
/* Test mpf_add.
Packit 5c3484
Packit 5c3484
Copyright 1996, 2001 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library test suite.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is free software; you can redistribute it
Packit 5c3484
and/or modify it under the terms of the GNU General Public License as
Packit 5c3484
published by the Free Software Foundation; either version 3 of the License,
Packit 5c3484
or (at your option) any later version.
Packit 5c3484
Packit 5c3484
The GNU MP Library test suite is distributed in the hope that it will be
Packit 5c3484
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5c3484
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 5c3484
Public License for more details.
Packit 5c3484
Packit 5c3484
You should have received a copy of the GNU General Public License along with
Packit 5c3484
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "tests.h"
Packit 5c3484
Packit 5c3484
#ifndef SIZE
Packit 5c3484
#define SIZE 16
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
main (int argc, char **argv)
Packit 5c3484
{
Packit 5c3484
  mp_size_t size;
Packit 5c3484
  mp_exp_t exp;
Packit 5c3484
  int reps = 20000;
Packit 5c3484
  int i;
Packit 5c3484
  mpf_t u, v, w, wref;
Packit 5c3484
  mp_size_t bprec = 100;
Packit 5c3484
  mpf_t rerr, max_rerr, limit_rerr;
Packit 5c3484
Packit 5c3484
  tests_start ();
Packit 5c3484
Packit 5c3484
  if (argc > 1)
Packit 5c3484
    {
Packit 5c3484
      reps = strtol (argv[1], 0, 0);
Packit 5c3484
      if (argc > 2)
Packit 5c3484
	bprec = strtol (argv[2], 0, 0);
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpf_set_default_prec (bprec);
Packit 5c3484
Packit 5c3484
  mpf_init_set_ui (limit_rerr, 1);
Packit 5c3484
  mpf_div_2exp (limit_rerr, limit_rerr, bprec);
Packit 5c3484
#if VERBOSE
Packit 5c3484
  mpf_dump (limit_rerr);
Packit 5c3484
#endif
Packit 5c3484
  mpf_init (rerr);
Packit 5c3484
  mpf_init_set_ui (max_rerr, 0);
Packit 5c3484
Packit 5c3484
  mpf_init (u);
Packit 5c3484
  mpf_init (v);
Packit 5c3484
  mpf_init (w);
Packit 5c3484
  mpf_init (wref);
Packit 5c3484
  for (i = 0; i < reps; i++)
Packit 5c3484
    {
Packit 5c3484
      size = urandom () % (2 * SIZE) - SIZE;
Packit 5c3484
      exp = urandom () % SIZE;
Packit 5c3484
      mpf_random2 (u, size, exp);
Packit 5c3484
Packit 5c3484
      size = urandom () % (2 * SIZE) - SIZE;
Packit 5c3484
      exp = urandom () % SIZE;
Packit 5c3484
      mpf_random2 (v, size, exp);
Packit 5c3484
Packit 5c3484
      mpf_add (w, u, v);
Packit 5c3484
      refmpf_add (wref, u, v);
Packit 5c3484
Packit 5c3484
      mpf_reldiff (rerr, w, wref);
Packit 5c3484
      if (mpf_cmp (rerr, max_rerr) > 0)
Packit 5c3484
	{
Packit 5c3484
	  mpf_set (max_rerr, rerr);
Packit 5c3484
#if VERBOSE
Packit 5c3484
	  mpf_dump (max_rerr);
Packit 5c3484
#endif
Packit 5c3484
	  if (mpf_cmp (rerr, limit_rerr) > 0)
Packit 5c3484
	    {
Packit 5c3484
	      printf ("ERROR after %d tests\n", i);
Packit 5c3484
	      printf ("   u = "); mpf_dump (u);
Packit 5c3484
	      printf ("   v = "); mpf_dump (v);
Packit 5c3484
	      printf ("wref = "); mpf_dump (wref);
Packit 5c3484
	      printf ("   w = "); mpf_dump (w);
Packit 5c3484
	      abort ();
Packit 5c3484
	    }
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  mpf_clear (limit_rerr);
Packit 5c3484
  mpf_clear (rerr);
Packit 5c3484
  mpf_clear (max_rerr);
Packit 5c3484
Packit 5c3484
  mpf_clear (u);
Packit 5c3484
  mpf_clear (v);
Packit 5c3484
  mpf_clear (w);
Packit 5c3484
  mpf_clear (wref);
Packit 5c3484
Packit 5c3484
  tests_end ();
Packit 5c3484
  exit (0);
Packit 5c3484
}