Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / mpf_compat.h
1 /* Test compatibility mpf-mpfr.
2
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.
5
6 This file is part of the GNU MPFR Library.
7
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.
12
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.
17
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. */
22
23 #if defined (__cplusplus)
24 #include <cstdio>
25 #else
26 #include <stdio.h>
27 #endif
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "gmp.h"
32 #include "mpfr.h"
33 #ifdef MPFR
34 #include "mpf2mpfr.h"
35 #endif
36
37 int
38 main (void)
39 {
40   unsigned long int prec;
41   unsigned long int prec2;
42   mpf_t x, y;
43   mpz_t z;
44   mpq_t q;
45   double d;
46   signed long int exp;
47   long l;
48   unsigned long u;
49   char *s;
50   int i;
51   FILE *f;
52   gmp_randstate_t state;
53
54   /* Initialization Functions */
55   prec = 53;
56   mpf_set_default_prec (prec);
57   prec2 = mpf_get_default_prec ();
58   if (prec2 < prec)
59     {
60       printf ("Error in get_default_prec: %lu < %lu\n", prec2, prec);
61       exit (1);
62     }
63
64   mpf_init (y);
65
66   mpf_init2 (x, prec);
67   prec2 = mpf_get_prec (x);
68   if (prec2 < prec)
69     {
70       printf ("Error in get_prec: %lu < %lu\n", prec2, prec);
71       mpf_clear (y);
72       mpf_clear (x);
73       exit (1);
74     }
75
76   mpf_set_prec (x, 2 * prec);
77   prec2 = mpf_get_prec (x);
78   if (prec2 < 2 * prec)
79     {
80       printf ("Error in set_prec: %lu < %lu\n", prec2, 2 * prec);
81       mpf_clear (y);
82       mpf_clear (x);
83       exit (1);
84     }
85
86   mpf_set_prec_raw (x, prec);
87   prec2 = mpf_get_prec (x);
88   if (prec2 < prec)
89     {
90       printf ("Error in set_prec_raw: %lu < %lu\n", prec2, prec);
91       mpf_clear (y);
92       mpf_clear (x);
93       exit (1);
94     }
95
96   /* Assignment Functions */
97
98   mpf_set (y, x);
99   mpf_set_ui (x, 1);
100   mpf_set_si (x, -1);
101   mpf_set_d (x, 1.0);
102
103   mpz_init_set_ui (z, 17);
104   mpf_set_z (x, z);
105   mpz_clear (z);
106
107   mpq_init (q);
108   mpq_set_ui (q, 2, 3);
109   mpf_set_q (x, q);
110   mpq_clear (q);
111
112   mpf_set_str (x, "3.1415e1", 10);
113   mpf_swap (x, y);
114
115   /* Combined Initialization and Assignment Functions */
116
117   mpf_clear (x);
118   mpf_init_set (x, y);
119   mpf_clear (x);
120   mpf_init_set_ui (x, 17);
121   mpf_clear (x);
122   mpf_init_set_si (x, -17);
123   mpf_clear (x);
124   mpf_init_set_d (x, 17.0);
125   mpf_clear (x);
126   mpf_init_set_str (x, "3.1415e1", 10);
127
128   /* Conversion Functions */
129
130   d = mpf_get_d (x);
131   d = mpf_get_d_2exp (&exp, x);
132   l = mpf_get_si (x);
133   u = mpf_get_ui (x);
134   s = mpf_get_str (NULL, &exp, 10, 10, x);
135   /* MPF doen't have mpf_free_str */
136   mpfr_free_str (s);
137
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
140      checking. */
141   (void) d;  (void) l;  (void)  u;
142
143   /* Arithmetic Functions */
144
145   mpf_add (y, x, x);
146   mpf_add_ui (y, x, 1);
147   mpf_sub (y, x, x);
148   mpf_ui_sub (y, 1, x);
149   mpf_sub_ui (y, x, 1);
150   mpf_mul (y, x, x);
151   mpf_mul_ui (y, x, 17);
152   mpf_div (y, x, x);
153   mpf_ui_div (y, 17, x);
154   mpf_div_ui (y, x, 17);
155   mpf_sqrt (y, x);
156   mpf_sqrt_ui (y, 17);
157   mpf_pow_ui (y, x, 2);
158   mpf_neg (y, x);
159   mpf_abs (y, x);
160   mpf_mul_2exp (y, x, 17);
161   mpf_div_2exp (y, x, 17);
162
163   /* Comparison Functions */
164
165   i = mpf_cmp (y, x);
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);
171   i = mpf_sgn (x);
172
173   /* Input and Output Functions */
174
175   f = fopen ("/dev/null", "w");
176   if (f != NULL)
177     {
178       mpf_out_str (f, 10, 10, x);
179       fclose (f);
180     }
181
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");
188   if (f != NULL)
189     {
190       i = mpf_inp_str (x, f, 10);
191       if ((i == 0) || mpf_cmp_ui (x, 31415))
192         {
193           printf ("Error in reading 1st line from file inp_str.data\n");
194           exit (1);
195         }
196       fclose (f);
197     }
198
199   /* Miscellaneous Functions */
200
201   mpf_ceil (y, x);
202   mpf_floor (y, x);
203   mpf_trunc (y, x);
204
205   i = mpf_integer_p (x);
206
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);
213
214   gmp_randinit_lc_2exp_size (state, 128);
215   mpf_urandomb (x, state, 10);
216   gmp_randclear (state);
217
218   /* Conversion to mpz */
219   mpz_init (z);
220   mpf_set_ui (x, 17);
221   mpz_set_f (z, x);
222   mpf_set_z (x, z);
223   mpz_clear (z);
224   if (mpf_cmp_ui (x, 17) != 0)
225     {
226       fprintf (stderr, "Error in conversion to/from mpz\n");
227       fprintf (stderr, "expected 17, got %1.16e\n", mpf_get_d (x));
228       exit (1);
229     }
230
231   /* clear all variables */
232   mpf_clear (y);
233   mpf_clear (x);
234
235   return 0;
236 }