Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpf / tests / t-dm2exp.c
1 /* Test mpf_div, mpf_div_2exp, mpf_mul_2exp.
2
3 Copyright (C) 1996 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 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 Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library 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., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "urandom.h"
26
27 #ifndef SIZE
28 #define SIZE 16
29 #endif
30
31 main (argc, argv)
32      int argc;
33      char **argv;
34 {
35   int reps = 500000;
36   int i;
37   mpf_t u, v, w1, w2;
38   mp_size_t bprec = 100;
39   mpf_t rerr, limit_rerr;
40
41   if (argc > 1)
42     {
43       reps = strtol (argv[1], 0, 0);
44       if (argc > 2)
45         bprec = strtol (argv[2], 0, 0);
46     }
47
48   mpf_set_default_prec (bprec);
49
50   mpf_init (rerr);
51   mpf_init (limit_rerr);
52
53   mpf_init (u);
54   mpf_init (v);
55   mpf_init (w1);
56   mpf_init (w2);
57
58   for (i = 0; i < reps; i++)
59     {
60       mp_size_t res_prec;
61       unsigned long int pow2;
62
63       res_prec = urandom () % (bprec + 100);
64       mpf_set_prec (w1, res_prec);
65       mpf_set_prec (w2, res_prec);
66
67       mpf_set_ui (limit_rerr, 1);
68       mpf_div_2exp (limit_rerr, limit_rerr, res_prec);
69
70       pow2 = urandom () % 0x10000;
71       mpf_set_ui (v, 1);
72       mpf_mul_2exp (v, v, pow2);
73
74       mpf_div_2exp (w1, u, pow2);
75       mpf_div (w2, u, v);
76       mpf_reldiff (rerr, w1, w2);
77       if (mpf_cmp (rerr, limit_rerr) > 0)
78         {
79           printf ("ERROR in mpf_div or mpf_div_2exp after %d tests\n", i);
80           printf ("   u = "); mpf_dump (u);
81           printf ("   v = "); mpf_dump (v);
82           printf ("  w1 = "); mpf_dump (w1);
83           printf ("  w2 = "); mpf_dump (w2);
84           abort ();
85         }
86     }
87
88   exit (0);
89 }
90
91 oo (x)
92      mpf_t x;
93 {
94   mp_size_t i;
95   printf (" exp = %ld\n", x->_mp_exp);
96   printf ("size = %d\n", x->_mp_size);
97   for (i = ABS (x->_mp_size) - 1; i >= 0; i--)
98     printf ("%08lX ", x->_mp_d[i]);
99   printf ("\n");
100   mpf_dump (x);
101 }