1 /* Test compatibility mpf-mpfr.
3 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #if defined (__cplusplus)
40 unsigned long int prec;
41 unsigned long int prec2;
52 gmp_randstate_t state;
54 /* Initialization Functions */
56 mpf_set_default_prec (prec);
57 prec2 = mpf_get_default_prec ();
60 printf ("Error in get_default_prec: %lu < %lu\n", prec2, prec);
67 prec2 = mpf_get_prec (x);
70 printf ("Error in get_prec: %lu < %lu\n", prec2, prec);
76 mpf_set_prec (x, 2 * prec);
77 prec2 = mpf_get_prec (x);
80 printf ("Error in set_prec: %lu < %lu\n", prec2, 2 * prec);
86 mpf_set_prec_raw (x, prec);
87 prec2 = mpf_get_prec (x);
90 printf ("Error in set_prec_raw: %lu < %lu\n", prec2, prec);
96 /* Assignment Functions */
103 mpz_init_set_ui (z, 17);
108 mpq_set_ui (q, 2, 3);
112 mpf_set_str (x, "3.1415e1", 10);
115 /* Combined Initialization and Assignment Functions */
120 mpf_init_set_ui (x, 17);
122 mpf_init_set_si (x, -17);
124 mpf_init_set_d (x, 17.0);
126 mpf_init_set_str (x, "3.1415e1", 10);
128 /* Conversion Functions */
131 d = mpf_get_d_2exp (&exp, x);
134 s = mpf_get_str (NULL, &exp, 10, 10, x);
135 /* MPF doen't have mpf_free_str */
138 /* Use d, l and u to avoid a warning with -Wunused-but-set-variable
139 from GCC 4.6. The variables above were mainly used for prototype
141 (void) d; (void) l; (void) u;
143 /* Arithmetic Functions */
146 mpf_add_ui (y, x, 1);
148 mpf_ui_sub (y, 1, x);
149 mpf_sub_ui (y, x, 1);
151 mpf_mul_ui (y, x, 17);
153 mpf_ui_div (y, 17, x);
154 mpf_div_ui (y, x, 17);
157 mpf_pow_ui (y, x, 2);
160 mpf_mul_2exp (y, x, 17);
161 mpf_div_2exp (y, x, 17);
163 /* Comparison Functions */
166 i = mpf_cmp_d (y, 1.7);
167 i = mpf_cmp_ui (y, 17);
168 i = mpf_cmp_si (y, -17);
169 i = mpf_eq (y, x, 17);
170 mpf_reldiff (y, y, x);
173 /* Input and Output Functions */
175 f = fopen ("/dev/null", "w");
178 mpf_out_str (f, 10, 10, x);
182 mpf_set_prec (x, 15);
183 mpf_set_prec (y, 15);
184 /* We may use src_fopen instead of fopen, but it is defined
185 in mpfr-test, and not in mpfr.h and gmp.h, and we want
186 to test theses includes files. */
187 f = fopen ("inp_str.data", "r");
190 i = mpf_inp_str (x, f, 10);
191 if ((i == 0) || mpf_cmp_ui (x, 31415))
193 printf ("Error in reading 1st line from file inp_str.data\n");
199 /* Miscellaneous Functions */
205 i = mpf_integer_p (x);
207 i = mpf_fits_ulong_p (x);
208 i = mpf_fits_slong_p (x);
209 i = mpf_fits_uint_p (x);
210 i = mpf_fits_sint_p (x);
211 i = mpf_fits_ushort_p (x);
212 i = mpf_fits_sshort_p (x);
214 gmp_randinit_lc_2exp_size (state, 128);
215 mpf_urandomb (x, state, 10);
216 gmp_randclear (state);
218 /* Conversion to mpz */
224 if (mpf_cmp_ui (x, 17) != 0)
226 fprintf (stderr, "Error in conversion to/from mpz\n");
227 fprintf (stderr, "expected 17, got %1.16e\n", mpf_get_d (x));
231 /* clear all variables */