xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpf/t-dm2exp.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpf_div, mpf_div_2exp, mpf_mul_2exp.
2 
3 Copyright 1996, 2000, 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   int reps = 100000;
34   int i;
35   mpf_t u, v, w1, w2, w3;
36   mp_size_t bprec = 100;
37   mpf_t rerr, limit_rerr;
38   mp_size_t un;
39   mp_exp_t ue;
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 (rerr);
53   mpf_init (limit_rerr);
54 
55   mpf_init (u);
56   mpf_init (v);
57   mpf_init (w1);
58   mpf_init (w2);
59   mpf_init (w3);
60 
61   for (i = 0; i < reps; i++)
62     {
63       unsigned long int res_prec;
64       unsigned long int pow2;
65 
66       res_prec = urandom () % (bprec + 100);
67       mpf_set_prec (w1, res_prec);
68       mpf_set_prec (w2, res_prec);
69       mpf_set_prec (w3, res_prec);
70 
71       mpf_set_ui (limit_rerr, 1);
72       mpf_div_2exp (limit_rerr, limit_rerr, res_prec);
73 
74       pow2 = urandom () % 0x10000;
75       mpf_set_ui (v, 1);
76       mpf_mul_2exp (v, v, pow2);
77 
78       un = urandom () % (2 * SIZE) - SIZE;
79       ue = urandom () % SIZE;
80       mpf_random2 (u, un, ue);
81 
82       mpf_div_2exp (w1, u, pow2);
83       mpf_div (w2, u, v);
84       mpf_reldiff (rerr, w1, w2);
85       if (mpf_cmp (rerr, limit_rerr) > 0)
86 	{
87 	  printf ("ERROR in mpf_div or mpf_div_2exp after %d tests\n", i);
88 	  printf ("   u = "); mpf_dump (u);
89 	  printf ("   v = "); mpf_dump (v);
90 	  printf ("  w1 = "); mpf_dump (w1);
91 	  printf ("  w2 = "); mpf_dump (w2);
92 	  abort ();
93 	}
94       mpf_mul_2exp (w3, w1, pow2);
95       mpf_reldiff (rerr, u, w3);
96       if (mpf_cmp (rerr, limit_rerr) > 0)
97 	{
98 	  printf ("ERROR in mpf_mul_2exp after %d tests\n", i);
99 	  printf ("   u = "); mpf_dump (u);
100 	  printf ("   v = "); mpf_dump (v);
101 	  printf ("  w1 = "); mpf_dump (w1);
102 	  printf ("  w3 = "); mpf_dump (w3);
103 	  abort ();
104 	}
105     }
106 
107   mpf_clear (rerr);
108   mpf_clear (limit_rerr);
109 
110   mpf_clear (u);
111   mpf_clear (v);
112   mpf_clear (w1);
113   mpf_clear (w2);
114   mpf_clear (w3);
115 
116   tests_end ();
117   exit (0);
118 }
119