xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tget_flt.c (revision 4d5abbe83f525258eb479e5fca29f25cb943f379)
1 /* Test file for mpfr_get_flt and mpfr_set_flt
2 
3 Copyright 2009, 2010, 2011, 2012, 2013 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 #include <stdlib.h>
24 #include <float.h>     /* for FLT_MIN */
25 #include "mpfr-test.h"
26 
27 int
28 main (void)
29 {
30   mpfr_t x, y;
31   float f, g, infp;
32   int i;
33 
34   infp = (float) DBL_POS_INF;
35   if (infp * 0.5 != infp)
36     {
37       fprintf (stderr, "Error, FLT_MAX + FLT_MAX does not yield INFP\n");
38       fprintf (stderr, "(this is probably a compiler bug, please report)\n");
39       exit (1);
40     }
41 
42   tests_start_mpfr ();
43 
44   mpfr_init2 (x, 24);
45   mpfr_init2 (y, 24);
46 
47 #if !defined(MPFR_ERRDIVZERO)
48   mpfr_set_nan (x);
49   f = mpfr_get_flt (x, MPFR_RNDN);
50   if (f == f)
51     {
52       printf ("Error for mpfr_get_flt(NaN)\n");
53       exit (1);
54     }
55   mpfr_set_flt (x, f, MPFR_RNDN);
56   if (mpfr_nan_p (x) == 0)
57     {
58       printf ("Error for mpfr_set_flt(NaN)\n");
59       exit (1);
60     }
61 
62   mpfr_set_inf (x, 1);
63   f = mpfr_get_flt (x, MPFR_RNDN);
64   mpfr_set_flt (x, f, MPFR_RNDN);
65   if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) < 0)
66     {
67       printf ("Error for mpfr_set_flt(mpfr_get_flt(+Inf)):\n");
68       printf ("f=%f, expected -Inf\n", f);
69       printf ("got "); mpfr_dump (x);
70       exit (1);
71     }
72 
73   mpfr_set_inf (x, -1);
74   f = mpfr_get_flt (x, MPFR_RNDN);
75   mpfr_set_flt (x, f, MPFR_RNDN);
76   if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) > 0)
77     {
78       printf ("Error for mpfr_set_flt(mpfr_get_flt(-Inf)):\n");
79       printf ("f=%f, expected -Inf\n", f);
80       printf ("got "); mpfr_dump (x);
81       exit (1);
82     }
83 #endif
84 
85   mpfr_set_ui (x, 0, MPFR_RNDN);
86   f = mpfr_get_flt (x, MPFR_RNDN);
87   mpfr_set_flt (x, f, MPFR_RNDN);
88   if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) < 0)
89     {
90       printf ("Error for mpfr_set_flt(mpfr_get_flt(+0))\n");
91       exit (1);
92     }
93 
94   mpfr_set_ui (x, 0, MPFR_RNDN);
95   mpfr_neg (x, x, MPFR_RNDN);
96   f = mpfr_get_flt (x, MPFR_RNDN);
97   mpfr_set_flt (x, f, MPFR_RNDN);
98   if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) > 0)
99     {
100       printf ("Error for mpfr_set_flt(mpfr_get_flt(-0))\n");
101       exit (1);
102     }
103 
104   mpfr_set_ui (x, 17, MPFR_RNDN);
105   f = mpfr_get_flt (x, MPFR_RNDN);
106   mpfr_set_flt (x, f, MPFR_RNDN);
107   if (mpfr_cmp_ui (x, 17) != 0)
108     {
109       printf ("Error for mpfr_set_flt(mpfr_get_flt(17))\n");
110       printf ("expected 17\n");
111       printf ("got      ");
112       mpfr_dump (x);
113       exit (1);
114     }
115 
116   mpfr_set_si (x, -42, MPFR_RNDN);
117   f = mpfr_get_flt (x, MPFR_RNDN);
118   mpfr_set_flt (x, f, MPFR_RNDN);
119   if (mpfr_cmp_si (x, -42) != 0)
120     {
121       printf ("Error for mpfr_set_flt(mpfr_get_flt(-42))\n");
122       printf ("expected -42\n");
123       printf ("got      ");
124       mpfr_dump (x);
125       exit (1);
126     }
127 
128   mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
129   for (i = -126; i < 128; i++)
130     {
131       f = mpfr_get_flt (x, MPFR_RNDN);
132       mpfr_set_flt (y, f, MPFR_RNDN);
133       if (mpfr_cmp (x, y) != 0)
134         {
135           printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
136           printf ("expected "); mpfr_dump (x);
137           printf ("got      "); mpfr_dump (y);
138           exit (1);
139         }
140       mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
141     }
142 
143   mpfr_set_prec (x, 53);
144   mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
145   for (i = -126; i < 128; i++)
146     {
147       mpfr_nextbelow (x);
148       f = mpfr_get_flt (x, MPFR_RNDN);
149       mpfr_nextabove (x);
150       mpfr_set_flt (y, f, MPFR_RNDN);
151       if (mpfr_cmp (x, y) != 0)
152         {
153           printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
154           printf ("expected "); mpfr_dump (x);
155           printf ("got      "); mpfr_dump (y);
156           exit (1);
157         }
158       mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
159     }
160 
161   mpfr_set_prec (x, 53);
162   mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN);
163   for (i = -126; i < 128; i++)
164     {
165       mpfr_nextabove (x);
166       f = mpfr_get_flt (x, MPFR_RNDN);
167       mpfr_nextbelow (x);
168       mpfr_set_flt (y, f, MPFR_RNDN);
169       if (mpfr_cmp (x, y) != 0)
170         {
171           printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n");
172           printf ("expected "); mpfr_dump (x);
173           printf ("got      "); mpfr_dump (y);
174           exit (1);
175         }
176       mpfr_mul_2exp (x, x, 1, MPFR_RNDN);
177     }
178 
179   mpfr_set_si_2exp (x, 1, -150, MPFR_RNDN);
180   g = 0.0;
181   f = mpfr_get_flt (x, MPFR_RNDN);
182   if (f != g)
183     {
184       printf ("Error for mpfr_get_flt(2^(-150),RNDN)\n");
185       printf ("expected %.8e, got %.8e\n", g, f);
186       exit (1);
187     }
188   f = mpfr_get_flt (x, MPFR_RNDZ);
189   if (f != g)
190     {
191       printf ("Error for mpfr_get_flt(2^(-150),RNDZ)\n");
192       printf ("expected %.8e, got %.8e\n", g, f);
193       exit (1);
194     }
195   f = mpfr_get_flt (x, MPFR_RNDD);
196   if (f != g)
197     {
198       printf ("Error for mpfr_get_flt(2^(-150),RNDD)\n");
199       printf ("expected %.8e, got %.8e\n", g, f);
200       exit (1);
201     }
202   g = FLT_MIN * FLT_EPSILON;
203   f = mpfr_get_flt (x, MPFR_RNDU);
204   if (f != g)
205     {
206       printf ("Error for mpfr_get_flt(2^(-150),RNDU)\n");
207       printf ("expected %.8e, got %.8e\n", g, f);
208       exit (1);
209     }
210   f = mpfr_get_flt (x, MPFR_RNDA);
211   if (f != g)
212     {
213       printf ("Error for mpfr_get_flt(2^(-150),RNDA)\n");
214       printf ("expected %.8e, got %.8e\n", g, f);
215       exit (1);
216     }
217 
218   mpfr_set_si_2exp (x, 1, -151, MPFR_RNDN);
219   g = 0.0;
220   f = mpfr_get_flt (x, MPFR_RNDN);
221   if (f != g)
222     {
223       printf ("Error for mpfr_get_flt(2^(-151),RNDN)\n");
224       printf ("expected %.8e, got %.8e\n", g, f);
225       exit (1);
226     }
227   f = mpfr_get_flt (x, MPFR_RNDZ);
228   if (f != g)
229     {
230       printf ("Error for mpfr_get_flt(2^(-151),RNDZ)\n");
231       printf ("expected %.8e, got %.8e\n", g, f);
232       exit (1);
233     }
234   f = mpfr_get_flt (x, MPFR_RNDD);
235   if (f != g)
236     {
237       printf ("Error for mpfr_get_flt(2^(-151),RNDD)\n");
238       printf ("expected %.8e, got %.8e\n", g, f);
239       exit (1);
240     }
241   g = FLT_MIN * FLT_EPSILON;
242   f = mpfr_get_flt (x, MPFR_RNDU);
243   if (f != g)
244     {
245       printf ("Error for mpfr_get_flt(2^(-151),RNDU)\n");
246       printf ("expected %.8e, got %.8e\n", g, f);
247       exit (1);
248     }
249   f = mpfr_get_flt (x, MPFR_RNDA);
250   if (f != g)
251     {
252       printf ("Error for mpfr_get_flt(2^(-151),RNDA)\n");
253       printf ("expected %.8e, got %.8e\n", g, f);
254       exit (1);
255     }
256 
257   mpfr_set_si_2exp (x, 1, -149, MPFR_RNDN);
258   g = FLT_MIN * FLT_EPSILON;
259   f = mpfr_get_flt (x, MPFR_RNDN);
260   if (f != g)
261     {
262       printf ("Error for mpfr_get_flt(2^(-149),RNDN)\n");
263       printf ("expected %.8e, got %.8e\n", g, f);
264       exit (1);
265     }
266   f = mpfr_get_flt (x, MPFR_RNDZ);
267   if (f != g)
268     {
269       printf ("Error for mpfr_get_flt(2^(-149),RNDZ)\n");
270       printf ("expected %.8e, got %.8e\n", g, f);
271       exit (1);
272     }
273   f = mpfr_get_flt (x, MPFR_RNDD);
274   if (f != g)
275     {
276       printf ("Error for mpfr_get_flt(2^(-149),RNDD)\n");
277       printf ("expected %.8e, got %.8e\n", g, f);
278       exit (1);
279     }
280   f = mpfr_get_flt (x, MPFR_RNDU);
281   if (f != g)
282     {
283       printf ("Error for mpfr_get_flt(2^(-149),RNDU)\n");
284       printf ("expected %.8e, got %.8e\n", g, f);
285       exit (1);
286     }
287   f = mpfr_get_flt (x, MPFR_RNDA);
288   if (f != g)
289     {
290       printf ("Error for mpfr_get_flt(2^(-149),RNDA)\n");
291       printf ("expected %.8e, got %.8e\n", g, f);
292       exit (1);
293     }
294 
295   mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN);
296   g = FLT_MAX;
297   f = mpfr_get_flt (x, MPFR_RNDZ);
298   if (f != g)
299     {
300       printf ("Error for mpfr_get_flt(2^128,RNDZ)\n");
301       printf ("expected %.8e, got %.8e\n", g, f);
302       exit (1);
303     }
304   f = mpfr_get_flt (x, MPFR_RNDD);
305   if (f != g)
306     {
307       printf ("Error for mpfr_get_flt(2^128,RNDD)\n");
308       printf ("expected %.8e, got %.8e\n", g, f);
309       exit (1);
310     }
311 #if !defined(MPFR_ERRDIVZERO)
312   f = mpfr_get_flt (x, MPFR_RNDN); /* 2^128 rounds to itself with extended
313                                       exponent range, we should get +Inf */
314   g = infp;
315   if (f != g)
316     {
317       printf ("Error for mpfr_get_flt(2^128,RNDN)\n");
318       printf ("expected %.8e, got %.8e\n", g, f);
319       exit (1);
320     }
321   f = mpfr_get_flt (x, MPFR_RNDU);
322   if (f != g)
323     {
324       printf ("Error for mpfr_get_flt(2^128,RNDU)\n");
325       printf ("expected %.8e, got %.8e\n", g, f);
326       exit (1);
327     }
328   f = mpfr_get_flt (x, MPFR_RNDA);
329   if (f != g)
330     {
331       printf ("Error for mpfr_get_flt(2^128,RNDA)\n");
332       printf ("expected %.8e, got %.8e\n", g, f);
333       exit (1);
334     }
335 #endif
336 
337   /* corner case: take x with 25 bits just below 2^128 */
338   mpfr_set_prec (x, 25);
339   mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN);
340   mpfr_nextbelow (x);
341   g = FLT_MAX;
342   f = mpfr_get_flt (x, MPFR_RNDZ);
343   if (f != g)
344     {
345       printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDZ)\n");
346       printf ("expected %.8e, got %.8e\n", g, f);
347       exit (1);
348     }
349   f = mpfr_get_flt (x, MPFR_RNDD);
350   if (f != g)
351     {
352       printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDD)\n");
353       printf ("expected %.8e, got %.8e\n", g, f);
354       exit (1);
355     }
356   f = mpfr_get_flt (x, MPFR_RNDN); /* first round to 2^128 (even rule),
357                                       thus we should get +Inf */
358   g = infp;
359   if (f != g)
360     {
361       printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDN)\n");
362       printf ("expected %.8e, got %.8e\n", g, f);
363       exit (1);
364     }
365   f = mpfr_get_flt (x, MPFR_RNDU);
366   if (f != g)
367     {
368       printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDU)\n");
369       printf ("expected %.8e, got %.8e\n", g, f);
370       exit (1);
371     }
372   f = mpfr_get_flt (x, MPFR_RNDA);
373   if (f != g)
374     {
375       printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDA)\n");
376       printf ("expected %.8e, got %.8e\n", g, f);
377       exit (1);
378     }
379 
380   mpfr_clear (x);
381   mpfr_clear (y);
382 
383   tests_end_mpfr ();
384   return 0;
385 }
386