1 /* Test mpf_add.
2 
3 Copyright 1996, 2001 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11 
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "mpir.h"
26 #include "gmp-impl.h"
27 #include "tests.h"
28 
29 #ifndef SIZE
30 #define SIZE 16
31 #endif
32 
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36   mp_size_t size;
37   mp_exp_t exp;
38   int reps = 20000;
39   int i;
40   mpf_t u, v, w, wref;
41   mp_size_t bprec = 100;
42   mpf_t rerr, max_rerr, limit_rerr;
43   gmp_randstate_t rands;
44 
45   tests_start ();
46   gmp_randinit_default(rands);
47   if (argc > 1)
48     {
49       reps = strtol (argv[1], 0, 0);
50       if (argc > 2)
51 	bprec = strtol (argv[2], 0, 0);
52     }
53 
54   mpf_set_default_prec (bprec);
55 
56   mpf_init_set_ui (limit_rerr, 1);
57   mpf_div_2exp (limit_rerr, limit_rerr, bprec);
58 #if VERBOSE
59   mpf_dump (limit_rerr);
60 #endif
61   mpf_init (rerr);
62   mpf_init_set_ui (max_rerr, 0);
63 
64   mpf_init (u);
65   mpf_init (v);
66   mpf_init (w);
67   mpf_init (wref);
68   for (i = 0; i < reps; i++)
69     {
70       size = urandom (rands) % (2 * SIZE) - SIZE;
71       exp = urandom (rands) % SIZE;
72       mpf_rrandomb (u, rands, size, exp);
73 
74       size = urandom (rands) % (2 * SIZE) - SIZE;
75       exp = urandom (rands) % SIZE;
76       mpf_rrandomb (v, rands, size, exp);
77 
78       mpf_add (w, u, v);
79       refmpf_add (wref, u, v);
80 
81       mpf_reldiff (rerr, w, wref);
82       if (mpf_cmp (rerr, max_rerr) > 0)
83 	{
84 	  mpf_set (max_rerr, rerr);
85 #if VERBOSE
86 	  mpf_dump (max_rerr);
87 #endif
88 	  if (mpf_cmp (rerr, limit_rerr) > 0)
89 	    {
90 	      printf ("ERROR after %d tests\n", i);
91 	      printf ("   u = "); mpf_dump (u);
92 	      printf ("   v = "); mpf_dump (v);
93 	      printf ("wref = "); mpf_dump (wref);
94 	      printf ("   w = "); mpf_dump (w);
95 	      abort ();
96 	    }
97 	}
98     }
99 
100   mpf_clear (limit_rerr);
101   mpf_clear (rerr);
102   mpf_clear (max_rerr);
103 
104   mpf_clear (u);
105   mpf_clear (v);
106   mpf_clear (w);
107   mpf_clear (wref);
108   gmp_randclear(rands);
109   tests_end ();
110   exit (0);
111 }
112