1 /* Test mpf_mul, mpf_div, mpf_ui_div, and mpf_div_ui.
2 
3 Copyright 1996, 2000, 2001, 2003 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 = 10000;
36   int i;
37   mpf_t u, v, w, x;
38   mp_size_t bprec = SIZE * GMP_LIMB_BITS;
39   mpf_t rerr, limit_rerr;
40   unsigned long ulimb, vlimb;
41   int single_flag;
42 
43   tests_start ();
44 
45   if (argc > 1)
46     {
47       reps = strtol (argv[1], 0, 0);
48       if (argc > 2)
49           bprec = strtol (argv[2], 0, 0);
50     }
51 
52   mpf_set_default_prec (bprec);
53 
54   mpf_init (rerr);
55   mpf_init (limit_rerr);
56 
57   mpf_init (u);
58   mpf_init (v);
59   mpf_init (w);
60   mpf_init (x);
61 
62   for (i = 0; i < reps; i++)
63     {
64       mp_size_t res_prec;
65 
66       res_prec = urandom () % bprec + 1;
67       mpf_set_prec (w, res_prec);
68       mpf_set_prec (x, res_prec);
69 
70       mpf_set_ui (limit_rerr, 1);
71       mpf_div_2exp (limit_rerr, limit_rerr, res_prec - 1);
72 
73       single_flag = 0;
74 
75       if ((urandom () & 1) != 0)
76           {
77             size = urandom () % (2 * SIZE) - SIZE;
78             exp = urandom () % SIZE;
79             mpf_random2 (u, size, exp);
80           }
81       else
82           {
83             ulimb = urandom ();
84             mpf_set_ui (u, ulimb);
85             single_flag = 1;
86           }
87 
88       if ((urandom () & 1) != 0)
89           {
90             size = urandom () % (2 * SIZE) - SIZE;
91             exp = urandom () % SIZE;
92             mpf_random2 (v, size, exp);
93           }
94       else
95           {
96             vlimb = urandom ();
97             mpf_set_ui (v, vlimb);
98             single_flag = 2;
99           }
100 
101       if (mpf_sgn (v) == 0)
102           continue;
103 
104       mpf_div (w, u, v);
105       mpf_mul (x, w, v);
106       mpf_reldiff (rerr, u, x);
107       if (mpf_cmp (rerr, limit_rerr) > 0)
108           {
109             printf ("ERROR in mpf_mul or mpf_div after %d tests\n", i);
110             printf ("   u = "); mpf_dump (u);
111             printf ("   v = "); mpf_dump (v);
112             printf ("   x = "); mpf_dump (x);
113             printf ("   w = "); mpf_dump (w);
114             abort ();
115           }
116 
117       if (single_flag == 2)
118           {
119             mpf_div_ui (x, u, vlimb);
120             mpf_reldiff (rerr, w, x);
121             if (mpf_cmp (rerr, limit_rerr) > 0)
122               {
123                 printf ("ERROR in mpf_div or mpf_div_ui after %d tests\n", i);
124                 printf ("   u = "); mpf_dump (u);
125                 printf ("   v = "); mpf_dump (v);
126                 printf ("   x = "); mpf_dump (x);
127                 printf ("   w = "); mpf_dump (w);
128                 abort ();
129               }
130           }
131 
132       if (single_flag == 1)
133           {
134             mpf_ui_div (x, ulimb, v);
135             mpf_reldiff (rerr, w, x);
136             if (mpf_cmp (rerr, limit_rerr) > 0)
137               {
138                 printf ("ERROR in mpf_div or mpf_ui_div after %d tests\n", i);
139                 printf ("   u = "); mpf_dump (u);
140                 printf ("   v = "); mpf_dump (v);
141                 printf ("   x = "); mpf_dump (x);
142                 printf ("   w = "); mpf_dump (w);
143                 abort ();
144               }
145           }
146     }
147 
148   mpf_clear (rerr);
149   mpf_clear (limit_rerr);
150 
151   mpf_clear (u);
152   mpf_clear (v);
153   mpf_clear (w);
154   mpf_clear (x);
155 
156   tests_end ();
157   exit (0);
158 }
159