Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpf / tests / t-sqrt.c
1 /* Test mpf_sqrt, mpf_mul.
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   mp_size_t size;
36   mp_exp_t exp;
37   int reps = 100000;
38   int i;
39   mpf_t x, y, y2;
40   mp_size_t bprec = 100;
41   mpf_t rerr, max_rerr, limit_rerr;
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 (x);
61   mpf_init (y);
62   mpf_init (y2);
63   for (i = 0; i < reps; i++)
64     {
65       size = urandom () % SIZE;
66       exp = urandom () % SIZE;
67       mpf_random2 (x, size, exp);
68
69       mpf_sqrt (y, x);
70       mpf_mul (y2, y, y);
71
72       mpf_reldiff (rerr, x, y2);
73       if (mpf_cmp (rerr, max_rerr) > 0)
74         {
75           mpf_set (max_rerr, rerr);
76 #if VERBOSE
77           mpf_dump (max_rerr);
78 #endif
79           if (mpf_cmp (rerr, limit_rerr) > 0)
80             {
81               printf ("ERROR after %d tests\n", i);
82               printf ("   x = "); mpf_dump (x);
83               printf ("   y = "); mpf_dump (y);
84               printf ("  y2 = "); mpf_dump (y2);
85               abort ();
86             }
87         }
88     }
89
90   exit (0);
91 }
92
93 oo (x)
94      mpf_t x;
95 {
96   mp_size_t i;
97   printf (" exp = %ld\n", x->_mp_exp);
98   printf ("size = %d\n", x->_mp_size);
99   for (i = ABS (x->_mp_size) - 1; i >= 0; i--)
100     printf ("%08lX ", x->_mp_d[i]);
101   printf ("\n");
102   mpf_dump (x);
103 }