1 /* Test mpf_get_str and mpf_set_str.
3 Copyright 1996, 2000, 2001, 2008 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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.
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.
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/. */
22 #include <string.h> /* for strlen */
37 main (int argc, char **argv)
42 mp_size_t bprec = 100;
43 mpf_t d, rerr, max_rerr, limit_rerr;
48 char buf[SIZE * GMP_LIMB_BITS + 5];
54 reps = strtol (argv[1], 0, 0);
56 bprec = strtol (argv[2], 0, 0);
59 mpf_set_default_prec (bprec);
61 mpf_init_set_ui (limit_rerr, 1);
62 mpf_div_2exp (limit_rerr, limit_rerr, bprec);
64 mpf_dump (limit_rerr);
67 mpf_init_set_ui (max_rerr, 0);
73 /* First test some specific values. */
75 mpf_set_str (y, "1.23456e1000", 0);
77 mpf_set_str (x, "1.23456e1000", 10);
78 if (mpf_cmp (x, y) != 0)
80 mpf_set_str (x, "1.23456e+1000", 0);
81 if (mpf_cmp (x, y) != 0)
83 mpf_set_str (x, "1.23456e+1000", 10);
84 if (mpf_cmp (x, y) != 0)
87 /* Now test random values. */
89 for (i = 0; i < reps; i++)
93 /* exercise the special case in get_str for for x==0 */
99 size = urandom () % (2 * SIZE) - SIZE;
100 exp = urandom () % EXPO;
101 mpf_random2 (x, size, exp);
102 base = urandom () % 61 + 2;
105 str = mpf_get_str (0, &bexp, base, 0, x);
108 sprintf (buf, "-0.%s@%ld", str + 1, bexp);
110 sprintf (buf, "0.%s@%ld", str, bexp);
112 mpf_set_str_or_abort (y, buf, -base);
113 (*__gmp_free_func) (str, strlen (str) + 1);
115 mpf_reldiff (rerr, x, y);
116 if (mpf_cmp (rerr, max_rerr) > 0)
118 mpf_set (max_rerr, rerr);
122 if (mpf_cmp (rerr, limit_rerr) > 0)
124 printf ("ERROR after %d tests\n", i);
125 printf ("base = %d\n", base);
126 printf (" x = "); mpf_dump (x);
127 printf (" y = "); mpf_dump (y);
133 mpf_clear (limit_rerr);
135 mpf_clear (max_rerr);