1/* Test file for mpfr_get_flt and mpfr_set_flt
2
3Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
4Contributed by the Arenaire and Cacao projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#include <stdlib.h>
24#include <float.h>     /* for FLT_MIN */
25#include "mpfr-test.h"
26
27int
28main (void)
29{
30  mpfr_t x, y;
31  float f, g, infp;
32  int i;
33
34  infp = FLT_MAX + FLT_MAX;
35
36  tests_start_mpfr ();
37
38  mpfr_init2 (x, 24);
39  mpfr_init2 (y, 24);
40
41  mpfr_set_nan (x);
42  f = mpfr_get_flt (x, MPFR_RNDN);
43  if (f == f)
44    {
45      printf ("Error for mpfr_get_flt(NaN)\n");
46      exit (1);
47    }
48  mpfr_set_flt (x, f, MPFR_RNDN);
49  if (mpfr_nan_p (x) == 0)
50    {
51      printf ("Error for mpfr_set_flt(NaN)\n");
52      exit (1);
53    }
54
55  mpfr_set_inf (x, 1);
56  f = mpfr_get_flt (x, MPFR_RNDN);
57  mpfr_set_flt (x, f, MPFR_RNDN);
58  if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) < 0)
59    {
60      printf ("Error for mpfr_set_flt(mpfr_get_flt(+Inf)):\n");
61      printf ("f=%f, expected -Inf\n", f);
62      printf ("got "); mpfr_dump (x);
63      exit (1);
64    }
65
66  mpfr_set_inf (x, -1);
67  f = mpfr_get_flt (x, MPFR_RNDN);
68  mpfr_set_flt (x, f, MPFR_RNDN);
69  if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) > 0)
70    {
71      printf ("Error for mpfr_set_flt(mpfr_get_flt(-Inf)):\n");
72      printf ("f=%f, expected -Inf\n", f);
73      printf ("got "); mpfr_dump (x);
74      exit (1);
75    }
76
77  mpfr_set_ui (x, 0, MPFR_RNDN);
78  f = mpfr_get_flt (x, MPFR_RNDN);
79  mpfr_set_flt (x, f, MPFR_RNDN);
80  if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) < 0)
81    {
82      printf ("Error for mpfr_set_flt(mpfr_get_flt(+0))\n");
83      exit (1);
84    }
85
86  mpfr_set_ui (x, 0, MPFR_RNDN);
87  mpfr_neg (x, x, MPFR_RNDN);
88  f = mpfr_get_flt (x, MPFR_RNDN);
89  mpfr_set_flt (x, f, MPFR_RNDN);
90  if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) > 0)
91    {
92      printf ("Error for mpfr_set_flt(mpfr_get_flt(-0))\n");
93      exit (1);
94    }
95
96  mpfr_set_ui (x, 17, MPFR_RNDN);
97  f = mpfr_get_flt (x, MPFR_RNDN);
98  mpfr_set_flt (x, f, MPFR_RNDN);
99  if (mpfr_cmp_ui (x, 17) != 0)
100    {
101      printf ("Error for mpfr_set_flt(mpfr_get_flt(17))\n");
102      printf ("expected 17\n");
103      printf ("got      ");
104      mpfr_dump (x);
105      exit (1);
106    }
107
108  mpfr_set_si (x, -42, MPFR_RNDN);
109  f = mpfr_get_flt (x, MPFR_RNDN);
110  mpfr_set_flt (x, f, MPFR_RNDN);
111  if (mpfr_cmp_si (x, -42) != 0)
112    {
113      printf ("Error for mpfr_set_flt(mpfr_get_flt(-42))\n");
114      printf ("expected -42\n");
115      printf ("got      ");
116      mpfr_dump (x);
117      exit (1);
118    }
119
120  mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
121  for (i = -126; i < 128; i++)
122    {
123      f = mpfr_get_flt (x, MPFR_RNDN);
124      mpfr_set_flt (y, f, MPFR_RNDN);
125      if (mpfr_cmp (x, y) != 0)
126        {
127          printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
128          mpfr_printf ("expected %Ra\n", x);
129          mpfr_printf ("got      %Ra\n", y);
130          exit (1);
131        }
132      mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
133    }
134
135  mpfr_set_prec (x, 53);
136  mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
137  for (i = -126; i < 128; i++)
138    {
139      mpfr_nextbelow (x);
140      f = mpfr_get_flt (x, MPFR_RNDN);
141      mpfr_nextabove (x);
142      mpfr_set_flt (y, f, MPFR_RNDN);
143      if (mpfr_cmp (x, y) != 0)
144        {
145          printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
146          mpfr_printf ("expected %Ra\n", x);
147          mpfr_printf ("got      %Ra\n", y);
148          exit (1);
149        }
150      mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
151    }
152
153  mpfr_set_prec (x, 53);
154  mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
155  for (i = -126; i < 128; i++)
156    {
157      mpfr_nextabove (x);
158      f = mpfr_get_flt (x, MPFR_RNDN);
159      mpfr_nextbelow (x);
160      mpfr_set_flt (y, f, MPFR_RNDN);
161      if (mpfr_cmp (x, y) != 0)
162        {
163          printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
164          mpfr_printf ("expected %Ra\n", x);
165          mpfr_printf ("got      %Ra\n", y);
166          exit (1);
167        }
168      mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
169    }
170
171  mpfr_set_si_2exp (x, 1, -150, MPFR_RNDN);
172  g = 0.0;
173  f = mpfr_get_flt (x, MPFR_RNDN);
174  if (f != g)
175    {
176      printf ("Error for mpfr_get_flt(2^(-150),RNDN)\n");
177      printf ("expected %.8e, got %.8e\n", g, f);
178      exit (1);
179    }
180  f = mpfr_get_flt (x, MPFR_RNDZ);
181  if (f != g)
182    {
183      printf ("Error for mpfr_get_flt(2^(-150),RNDZ)\n");
184      printf ("expected %.8e, got %.8e\n", g, f);
185      exit (1);
186    }
187  f = mpfr_get_flt (x, MPFR_RNDD);
188  if (f != g)
189    {
190      printf ("Error for mpfr_get_flt(2^(-150),RNDD)\n");
191      printf ("expected %.8e, got %.8e\n", g, f);
192      exit (1);
193    }
194  g = FLT_MIN * FLT_EPSILON;
195  f = mpfr_get_flt (x, MPFR_RNDU);
196  if (f != g)
197    {
198      printf ("Error for mpfr_get_flt(2^(-150),RNDU)\n");
199      printf ("expected %.8e, got %.8e\n", g, f);
200      exit (1);
201    }
202  f = mpfr_get_flt (x, MPFR_RNDA);
203  if (f != g)
204    {
205      printf ("Error for mpfr_get_flt(2^(-150),RNDA)\n");
206      printf ("expected %.8e, got %.8e\n", g, f);
207      exit (1);
208    }
209
210  mpfr_set_si_2exp (x, 1, -151, MPFR_RNDN);
211  g = 0.0;
212  f = mpfr_get_flt (x, MPFR_RNDN);
213  if (f != g)
214    {
215      printf ("Error for mpfr_get_flt(2^(-151),RNDN)\n");
216      printf ("expected %.8e, got %.8e\n", g, f);
217      exit (1);
218    }
219  f = mpfr_get_flt (x, MPFR_RNDZ);
220  if (f != g)
221    {
222      printf ("Error for mpfr_get_flt(2^(-151),RNDZ)\n");
223      printf ("expected %.8e, got %.8e\n", g, f);
224      exit (1);
225    }
226  f = mpfr_get_flt (x, MPFR_RNDD);
227  if (f != g)
228    {
229      printf ("Error for mpfr_get_flt(2^(-151),RNDD)\n");
230      printf ("expected %.8e, got %.8e\n", g, f);
231      exit (1);
232    }
233  g = FLT_MIN * FLT_EPSILON;
234  f = mpfr_get_flt (x, MPFR_RNDU);
235  if (f != g)
236    {
237      printf ("Error for mpfr_get_flt(2^(-151),RNDU)\n");
238      printf ("expected %.8e, got %.8e\n", g, f);
239      exit (1);
240    }
241  f = mpfr_get_flt (x, MPFR_RNDA);
242  if (f != g)
243    {
244      printf ("Error for mpfr_get_flt(2^(-151),RNDA)\n");
245      printf ("expected %.8e, got %.8e\n", g, f);
246      exit (1);
247    }
248
249  mpfr_set_si_2exp (x, 1, -149, MPFR_RNDN);
250  g = FLT_MIN * FLT_EPSILON;
251  f = mpfr_get_flt (x, MPFR_RNDN);
252  if (f != g)
253    {
254      printf ("Error for mpfr_get_flt(2^(-149),RNDN)\n");
255      printf ("expected %.8e, got %.8e\n", g, f);
256      exit (1);
257    }
258  f = mpfr_get_flt (x, MPFR_RNDZ);
259  if (f != g)
260    {
261      printf ("Error for mpfr_get_flt(2^(-149),RNDZ)\n");
262      printf ("expected %.8e, got %.8e\n", g, f);
263      exit (1);
264    }
265  f = mpfr_get_flt (x, MPFR_RNDD);
266  if (f != g)
267    {
268      printf ("Error for mpfr_get_flt(2^(-149),RNDD)\n");
269      printf ("expected %.8e, got %.8e\n", g, f);
270      exit (1);
271    }
272  f = mpfr_get_flt (x, MPFR_RNDU);
273  if (f != g)
274    {
275      printf ("Error for mpfr_get_flt(2^(-149),RNDU)\n");
276      printf ("expected %.8e, got %.8e\n", g, f);
277      exit (1);
278    }
279  f = mpfr_get_flt (x, MPFR_RNDA);
280  if (f != g)
281    {
282      printf ("Error for mpfr_get_flt(2^(-149),RNDA)\n");
283      printf ("expected %.8e, got %.8e\n", g, f);
284      exit (1);
285    }
286
287  mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN);
288  g = FLT_MAX;
289  f = mpfr_get_flt (x, MPFR_RNDZ);
290  if (f != g)
291    {
292      printf ("Error for mpfr_get_flt(2^128,RNDZ)\n");
293      printf ("expected %.8e, got %.8e\n", g, f);
294      exit (1);
295    }
296  f = mpfr_get_flt (x, MPFR_RNDD);
297  if (f != g)
298    {
299      printf ("Error for mpfr_get_flt(2^128,RNDD)\n");
300      printf ("expected %.8e, got %.8e\n", g, f);
301      exit (1);
302    }
303  f = mpfr_get_flt (x, MPFR_RNDN); /* 2^128 rounds to itself with extended
304                                      exponent range, we should get +Inf */
305  g = infp;
306  if (f != g)
307    {
308      printf ("Error for mpfr_get_flt(2^128,RNDN)\n");
309      printf ("expected %.8e, got %.8e\n", g, f);
310      exit (1);
311    }
312  f = mpfr_get_flt (x, MPFR_RNDU);
313  if (f != g)
314    {
315      printf ("Error for mpfr_get_flt(2^128,RNDU)\n");
316      printf ("expected %.8e, got %.8e\n", g, f);
317      exit (1);
318    }
319  f = mpfr_get_flt (x, MPFR_RNDA);
320  if (f != g)
321    {
322      printf ("Error for mpfr_get_flt(2^128,RNDA)\n");
323      printf ("expected %.8e, got %.8e\n", g, f);
324      exit (1);
325    }
326
327  /* corner case: take x with 25 bits just below 2^128 */
328  mpfr_set_prec (x, 25);
329  mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN);
330  mpfr_nextbelow (x);
331  g = FLT_MAX;
332  f = mpfr_get_flt (x, MPFR_RNDZ);
333  if (f != g)
334    {
335      printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDZ)\n");
336      printf ("expected %.8e, got %.8e\n", g, f);
337      exit (1);
338    }
339  f = mpfr_get_flt (x, MPFR_RNDD);
340  if (f != g)
341    {
342      printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDD)\n");
343      printf ("expected %.8e, got %.8e\n", g, f);
344      exit (1);
345    }
346  f = mpfr_get_flt (x, MPFR_RNDN); /* first round to 2^128 (even rule),
347                                      thus we should get +Inf */
348  g = infp;
349  if (f != g)
350    {
351      printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDN)\n");
352      printf ("expected %.8e, got %.8e\n", g, f);
353      exit (1);
354    }
355  f = mpfr_get_flt (x, MPFR_RNDU);
356  if (f != g)
357    {
358      printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDU)\n");
359      printf ("expected %.8e, got %.8e\n", g, f);
360      exit (1);
361    }
362  f = mpfr_get_flt (x, MPFR_RNDA);
363  if (f != g)
364    {
365      printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDA)\n");
366      printf ("expected %.8e, got %.8e\n", g, f);
367      exit (1);
368    }
369
370  mpfr_clear (x);
371  mpfr_clear (y);
372
373  tests_end_mpfr ();
374  return 0;
375}
376