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