Tizen 2.1 base
[external/gmp.git] / tests / mpf / t-add.c
1 /* Test mpf_add.
2
3 Copyright 1996, 2001 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 Lesser General Public License as published by
9 the Free Software Foundation; either version 3 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 Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "tests.h"
26
27 #ifndef SIZE
28 #define SIZE 16
29 #endif
30
31 int
32 main (int argc, char **argv)
33 {
34   mp_size_t size;
35   mp_exp_t exp;
36   int reps = 20000;
37   int i;
38   mpf_t u, v, w, wref;
39   mp_size_t bprec = 100;
40   mpf_t rerr, max_rerr, limit_rerr;
41
42   tests_start ();
43
44   if (argc > 1)
45     {
46       reps = strtol (argv[1], 0, 0);
47       if (argc > 2)
48         bprec = strtol (argv[2], 0, 0);
49     }
50
51   mpf_set_default_prec (bprec);
52
53   mpf_init_set_ui (limit_rerr, 1);
54   mpf_div_2exp (limit_rerr, limit_rerr, bprec);
55 #if VERBOSE
56   mpf_dump (limit_rerr);
57 #endif
58   mpf_init (rerr);
59   mpf_init_set_ui (max_rerr, 0);
60
61   mpf_init (u);
62   mpf_init (v);
63   mpf_init (w);
64   mpf_init (wref);
65   for (i = 0; i < reps; i++)
66     {
67       size = urandom () % (2 * SIZE) - SIZE;
68       exp = urandom () % SIZE;
69       mpf_random2 (u, size, exp);
70
71       size = urandom () % (2 * SIZE) - SIZE;
72       exp = urandom () % SIZE;
73       mpf_random2 (v, size, exp);
74
75       mpf_add (w, u, v);
76       refmpf_add (wref, u, v);
77
78       mpf_reldiff (rerr, w, wref);
79       if (mpf_cmp (rerr, max_rerr) > 0)
80         {
81           mpf_set (max_rerr, rerr);
82 #if VERBOSE
83           mpf_dump (max_rerr);
84 #endif
85           if (mpf_cmp (rerr, limit_rerr) > 0)
86             {
87               printf ("ERROR after %d tests\n", i);
88               printf ("   u = "); mpf_dump (u);
89               printf ("   v = "); mpf_dump (v);
90               printf ("wref = "); mpf_dump (wref);
91               printf ("   w = "); mpf_dump (w);
92               abort ();
93             }
94         }
95     }
96
97   mpf_clear (limit_rerr);
98   mpf_clear (rerr);
99   mpf_clear (max_rerr);
100
101   mpf_clear (u);
102   mpf_clear (v);
103   mpf_clear (w);
104   mpf_clear (wref);
105
106   tests_end ();
107   exit (0);
108 }