Annotation of OpenXM_contrib/gmp/tests/mpf/t-conv.c, Revision 1.1.1.1
1.1 ohara 1: /* Test mpf_get_str and mpf_set_str.
2:
3: Copyright 1996, 2000, 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 2.1 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; 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 <stdlib.h>
24: #include <string.h> /* for strlen */
25:
26: #include "gmp.h"
27: #include "gmp-impl.h"
28: #include "tests.h"
29:
30: #ifndef SIZE
31: #define SIZE 10
32: #endif
33:
34: #ifndef EXPO
35: #define EXPO 20
36: #endif
37:
38: int
39: main (int argc, char **argv)
40: {
41: mpf_t x, y;
42: int reps = 20000;
43: int i;
44: mp_size_t bprec = 100;
45: mpf_t d, rerr, max_rerr, limit_rerr;
46: char *str;
47: mp_exp_t bexp;
48: long size, exp;
49: int base;
50: char buf[SIZE * BITS_PER_MP_LIMB + 5];
51:
52: tests_start ();
53:
54: if (argc > 1)
55: {
56: reps = strtol (argv[1], 0, 0);
57: if (argc > 2)
58: bprec = strtol (argv[2], 0, 0);
59: }
60:
61: mpf_set_default_prec (bprec);
62:
63: mpf_init_set_ui (limit_rerr, 1);
64: mpf_div_2exp (limit_rerr, limit_rerr, bprec);
65: #if VERBOSE
66: mpf_dump (limit_rerr);
67: #endif
68: mpf_init (rerr);
69: mpf_init_set_ui (max_rerr, 0);
70:
71: mpf_init (x);
72: mpf_init (y);
73: mpf_init (d);
74:
75: for (i = 0; i < reps; i++)
76: {
77: if (i == 0)
78: {
79: /* exercise the special case in get_str for for x==0 */
80: mpf_set_ui (x, 0L);
81: base = 10;
82: }
83: else
84: {
85: size = urandom () % (2 * SIZE) - SIZE;
86: exp = urandom () % EXPO;
87: mpf_random2 (x, size, exp);
88: base = urandom () % 35 + 2;
89: }
90:
91: str = mpf_get_str (0, &bexp, base, 0, x);
92:
93: if (str[0] == '-')
94: sprintf (buf, "-0.%s@%ld", str + 1, bexp);
95: else
96: sprintf (buf, "0.%s@%ld", str, bexp);
97:
98: mpf_set_str_or_abort (y, buf, -base);
99: (*__gmp_free_func) (str, strlen (str) + 1);
100:
101: mpf_reldiff (rerr, x, y);
102: if (mpf_cmp (rerr, max_rerr) > 0)
103: {
104: mpf_set (max_rerr, rerr);
105: #if VERBOSE
106: mpf_dump (max_rerr);
107: #endif
108: if (mpf_cmp (rerr, limit_rerr) > 0)
109: {
110: printf ("ERROR after %d tests\n", i);
111: printf ("base = %d\n", base);
112: printf (" x = "); mpf_dump (x);
113: printf (" y = "); mpf_dump (y);
114: abort ();
115: }
116: }
117: }
118:
119: mpf_clear (limit_rerr);
120: mpf_clear (rerr);
121: mpf_clear (max_rerr);
122:
123: mpf_clear (x);
124: mpf_clear (y);
125: mpf_clear (d);
126:
127: tests_end ();
128: exit (0);
129: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>