xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tdiv.c (revision 230b95665bbd3a9d1a53658a36b1053f8382a519)
1 /* Test file for mpfr_div (and some mpfr_div_ui, etc. tests).
2 
3 Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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 <stdio.h>
24 #include <stdlib.h>
25 
26 #include "mpfr-test.h"
27 
28 static void
29 check_equal (mpfr_srcptr a, mpfr_srcptr a2, char *s,
30              mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t r)
31 {
32   if ((MPFR_IS_NAN (a) && MPFR_IS_NAN (a2)) ||
33       mpfr_equal_p (a, a2))
34     return;
35   printf ("Error in %s\n", mpfr_print_rnd_mode (r));
36   printf ("b  = ");
37   mpfr_dump (b);
38   printf ("c  = ");
39   mpfr_dump (c);
40   printf ("mpfr_div    result: ");
41   mpfr_dump (a);
42   printf ("%s result: ", s);
43   mpfr_dump (a2);
44   exit (1);
45 }
46 
47 static int
48 mpfr_all_div (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t r)
49 {
50   mpfr_t a2;
51   unsigned int oldflags, newflags;
52   int inex, inex2;
53 
54   oldflags = __gmpfr_flags;
55   inex = mpfr_div (a, b, c, r);
56 
57   if (a == b || a == c)
58     return inex;
59 
60   newflags = __gmpfr_flags;
61 
62   mpfr_init2 (a2, MPFR_PREC (a));
63 
64   if (mpfr_integer_p (b) && ! (MPFR_IS_ZERO (b) && MPFR_IS_NEG (b)))
65     {
66       /* b is an integer, but not -0 (-0 is rejected as
67          it becomes +0 when converted to an integer). */
68       if (mpfr_fits_ulong_p (b, MPFR_RNDA))
69         {
70           __gmpfr_flags = oldflags;
71           inex2 = mpfr_ui_div (a2, mpfr_get_ui (b, MPFR_RNDN), c, r);
72           MPFR_ASSERTN (SAME_SIGN (inex2, inex));
73           MPFR_ASSERTN (__gmpfr_flags == newflags);
74           check_equal (a, a2, "mpfr_ui_div", b, c, r);
75         }
76       if (mpfr_fits_slong_p (b, MPFR_RNDA))
77         {
78           __gmpfr_flags = oldflags;
79           inex2 = mpfr_si_div (a2, mpfr_get_si (b, MPFR_RNDN), c, r);
80           MPFR_ASSERTN (SAME_SIGN (inex2, inex));
81           MPFR_ASSERTN (__gmpfr_flags == newflags);
82           check_equal (a, a2, "mpfr_si_div", b, c, r);
83         }
84     }
85 
86   if (mpfr_integer_p (c) && ! (MPFR_IS_ZERO (c) && MPFR_IS_NEG (c)))
87     {
88       /* c is an integer, but not -0 (-0 is rejected as
89          it becomes +0 when converted to an integer). */
90       if (mpfr_fits_ulong_p (c, MPFR_RNDA))
91         {
92           __gmpfr_flags = oldflags;
93           inex2 = mpfr_div_ui (a2, b, mpfr_get_ui (c, MPFR_RNDN), r);
94           MPFR_ASSERTN (SAME_SIGN (inex2, inex));
95           MPFR_ASSERTN (__gmpfr_flags == newflags);
96           check_equal (a, a2, "mpfr_div_ui", b, c, r);
97         }
98       if (mpfr_fits_slong_p (c, MPFR_RNDA))
99         {
100           __gmpfr_flags = oldflags;
101           inex2 = mpfr_div_si (a2, b, mpfr_get_si (c, MPFR_RNDN), r);
102           MPFR_ASSERTN (SAME_SIGN (inex2, inex));
103           MPFR_ASSERTN (__gmpfr_flags == newflags);
104           check_equal (a, a2, "mpfr_div_si", b, c, r);
105         }
106     }
107 
108   mpfr_clear (a2);
109 
110   return inex;
111 }
112 
113 #ifdef CHECK_EXTERNAL
114 static int
115 test_div (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
116 {
117   int res;
118   int ok = rnd_mode == MPFR_RNDN && mpfr_number_p (b) && mpfr_number_p (c);
119   if (ok)
120     {
121       mpfr_print_raw (b);
122       printf (" ");
123       mpfr_print_raw (c);
124     }
125   res = mpfr_all_div (a, b, c, rnd_mode);
126   if (ok)
127     {
128       printf (" ");
129       mpfr_print_raw (a);
130       printf ("\n");
131     }
132   return res;
133 }
134 #else
135 #define test_div mpfr_all_div
136 #endif
137 
138 #define check53(n, d, rnd, res) check4(n, d, rnd, 53, res)
139 
140 /* return 0 iff a and b are of the same sign */
141 static int
142 inex_cmp (int a, int b)
143 {
144   if (a > 0)
145     return (b > 0) ? 0 : 1;
146   else if (a == 0)
147     return (b == 0) ? 0 : 1;
148   else
149     return (b < 0) ? 0 : 1;
150 }
151 
152 static void
153 check4 (const char *Ns, const char *Ds, mpfr_rnd_t rnd_mode, int p,
154         const char *Qs)
155 {
156   mpfr_t q, n, d;
157 
158   mpfr_inits2 (p, q, n, d, (mpfr_ptr) 0);
159   mpfr_set_str1 (n, Ns);
160   mpfr_set_str1 (d, Ds);
161   test_div(q, n, d, rnd_mode);
162   if (mpfr_cmp_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN) )
163     {
164       printf ("mpfr_div failed for n=%s, d=%s, p=%d, rnd_mode=%s\n",
165               Ns, Ds, p, mpfr_print_rnd_mode (rnd_mode));
166       printf ("got      ");mpfr_print_binary(q);
167       mpfr_set_str (q, Qs, ((p==53) ? 10 : 2), MPFR_RNDN);
168       printf("\nexpected "); mpfr_print_binary(q);
169       putchar('\n');
170       exit (1);
171     }
172   mpfr_clears (q, n, d, (mpfr_ptr) 0);
173 }
174 
175 static void
176 check24 (const char *Ns, const char *Ds, mpfr_rnd_t rnd_mode, const char *Qs)
177 {
178   mpfr_t q, n, d;
179 
180   mpfr_inits2 (24, q, n, d, (mpfr_ptr) 0);
181 
182   mpfr_set_str1 (n, Ns);
183   mpfr_set_str1 (d, Ds);
184   test_div(q, n, d, rnd_mode);
185   if (mpfr_cmp_str1 (q, Qs) )
186     {
187       printf ("mpfr_div failed for n=%s, d=%s, prec=24, rnd_mode=%s\n",
188              Ns, Ds, mpfr_print_rnd_mode(rnd_mode));
189       printf ("expected quotient is %s, got ", Qs);
190       mpfr_out_str(stdout,10,0,q, MPFR_RNDN); putchar('\n');
191       exit (1);
192     }
193   mpfr_clears (q, n, d, (mpfr_ptr) 0);
194 }
195 
196 /* the following examples come from the paper "Number-theoretic Test
197    Generation for Directed Rounding" from Michael Parks, Table 2 */
198 static void
199 check_float(void)
200 {
201   check24("70368760954880.0", "8388609.0", MPFR_RNDN, "8.388609e6");
202   check24("140737479966720.0", "16777213.0", MPFR_RNDN, "8.388609e6");
203   check24("70368777732096.0", "8388611.0", MPFR_RNDN, "8.388609e6");
204   check24("105553133043712.0", "12582911.0", MPFR_RNDN, "8.38861e6");
205   /* the exponent for the following example was forgotten in
206      the Arith'14 version of Parks' paper */
207   check24 ("12582913.0", "12582910.0", MPFR_RNDN, "1.000000238");
208   check24 ("105553124655104.0", "12582910.0", MPFR_RNDN, "8388610.0");
209   check24("140737479966720.0", "8388609.0", MPFR_RNDN, "1.6777213e7");
210   check24("70368777732096.0", "8388609.0", MPFR_RNDN, "8.388611e6");
211   check24("105553133043712.0", "8388610.0", MPFR_RNDN, "1.2582911e7");
212   check24("105553124655104.0", "8388610.0", MPFR_RNDN, "1.258291e7");
213 
214   check24("70368760954880.0", "8388609.0", MPFR_RNDZ, "8.388608e6");
215   check24("140737479966720.0", "16777213.0", MPFR_RNDZ, "8.388609e6");
216   check24("70368777732096.0", "8388611.0", MPFR_RNDZ, "8.388608e6");
217   check24("105553133043712.0", "12582911.0", MPFR_RNDZ, "8.38861e6");
218   check24("12582913.0", "12582910.0", MPFR_RNDZ, "1.000000238");
219   check24 ("105553124655104.0", "12582910.0", MPFR_RNDZ, "8388610.0");
220   check24("140737479966720.0", "8388609.0", MPFR_RNDZ, "1.6777213e7");
221   check24("70368777732096.0", "8388609.0", MPFR_RNDZ, "8.38861e6");
222   check24("105553133043712.0", "8388610.0", MPFR_RNDZ, "1.2582911e7");
223   check24("105553124655104.0", "8388610.0", MPFR_RNDZ, "1.258291e7");
224 
225   check24("70368760954880.0", "8388609.0", MPFR_RNDU, "8.388609e6");
226   check24("140737479966720.0", "16777213.0", MPFR_RNDU, "8.38861e6");
227   check24("70368777732096.0", "8388611.0", MPFR_RNDU, "8.388609e6");
228   check24("105553133043712.0", "12582911.0", MPFR_RNDU, "8.388611e6");
229   check24("12582913.0", "12582910.0", MPFR_RNDU, "1.000000357");
230   check24 ("105553124655104.0", "12582910.0", MPFR_RNDU, "8388611.0");
231   check24("140737479966720.0", "8388609.0", MPFR_RNDU, "1.6777214e7");
232   check24("70368777732096.0", "8388609.0", MPFR_RNDU, "8.388611e6");
233   check24("105553133043712.0", "8388610.0", MPFR_RNDU, "1.2582912e7");
234   check24("105553124655104.0", "8388610.0", MPFR_RNDU, "1.2582911e7");
235 
236   check24("70368760954880.0", "8388609.0", MPFR_RNDD, "8.388608e6");
237   check24("140737479966720.0", "16777213.0", MPFR_RNDD, "8.388609e6");
238   check24("70368777732096.0", "8388611.0", MPFR_RNDD, "8.388608e6");
239   check24("105553133043712.0", "12582911.0", MPFR_RNDD, "8.38861e6");
240   check24("12582913.0", "12582910.0", MPFR_RNDD, "1.000000238");
241   check24 ("105553124655104.0", "12582910.0", MPFR_RNDD, "8388610.0");
242   check24("140737479966720.0", "8388609.0", MPFR_RNDD, "1.6777213e7");
243   check24("70368777732096.0", "8388609.0", MPFR_RNDD, "8.38861e6");
244   check24("105553133043712.0", "8388610.0", MPFR_RNDD, "1.2582911e7");
245   check24("105553124655104.0", "8388610.0", MPFR_RNDD, "1.258291e7");
246 
247   check24("70368760954880.0", "8388609.0", MPFR_RNDA, "8.388609e6");
248 }
249 
250 static void
251 check_double(void)
252 {
253   check53("0.0", "1.0", MPFR_RNDZ, "0.0");
254   check53("-7.4988969224688591e63", "4.8816866450288732e306", MPFR_RNDD,
255           "-1.5361282826510687291e-243");
256   check53("-1.33225773037748601769e+199", "3.63449540676937123913e+79",
257           MPFR_RNDZ, "-3.6655920045905428978e119");
258   check53("9.89438396044940256501e-134", "5.93472984109987421717e-67",MPFR_RNDU,
259           "1.6672003992376663654e-67");
260   check53("9.89438396044940256501e-134", "5.93472984109987421717e-67",MPFR_RNDA,
261           "1.6672003992376663654e-67");
262   check53("9.89438396044940256501e-134", "-5.93472984109987421717e-67",
263           MPFR_RNDU, "-1.6672003992376663654e-67");
264   check53("-4.53063926135729747564e-308", "7.02293374921793516813e-84",
265           MPFR_RNDD, "-6.4512060388748850857e-225");
266   check53("6.25089225176473806123e-01","-2.35527154824420243364e-230",
267           MPFR_RNDD, "-2.6540006635008291192e229");
268   check53("6.25089225176473806123e-01","-2.35527154824420243364e-230",
269           MPFR_RNDA, "-2.6540006635008291192e229");
270   check53("6.52308934689126e15", "-1.62063546601505417497e273", MPFR_RNDN,
271           "-4.0250194961676020848e-258");
272   check53("1.04636807108079349236e-189", "3.72295730823253012954e-292",
273           MPFR_RNDZ, "2.810583051186143125e102");
274   /* problems found by Kevin under HP-PA */
275   check53 ("2.861044553323177e-136", "-1.1120354257068143e+45", MPFR_RNDZ,
276            "-2.5727998292003016e-181");
277   check53 ("-4.0559157245809205e-127", "-1.1237723844524865e+77", MPFR_RNDN,
278            "3.6091968273068081e-204");
279   check53 ("-1.8177943561493235e-93", "-8.51233984260364e-104", MPFR_RNDU,
280            "2.1354814184595821e+10");
281 }
282 
283 static void
284 check_64(void)
285 {
286   mpfr_t x,y,z;
287 
288   mpfr_inits2 (64, x, y, z, (mpfr_ptr) 0);
289 
290   mpfr_set_str_binary(x, "1.00100100110110101001010010101111000001011100100101010000000000E54");
291   mpfr_set_str_binary(y, "1.00000000000000000000000000000000000000000000000000000000000000E584");
292   test_div(z, x, y, MPFR_RNDU);
293   if (mpfr_cmp_str (z, "0.1001001001101101010010100101011110000010111001001010100000000000E-529", 2, MPFR_RNDN))
294     {
295       printf("Error for tdiv for MPFR_RNDU and p=64\nx=");
296       mpfr_print_binary(x);
297       printf("\ny=");
298       mpfr_print_binary(y);
299       printf("\ngot      ");
300       mpfr_print_binary(z);
301       printf("\nexpected 0.1001001001101101010010100101011110000010111001001010100000000000E-529\n");
302       exit(1);
303     }
304 
305   mpfr_clears (x, y, z, (mpfr_ptr) 0);
306 }
307 
308 static void
309 check_convergence (void)
310 {
311   mpfr_t x, y; int i, j;
312 
313   mpfr_init2(x, 130);
314   mpfr_set_str_binary(x, "0.1011111101011010101000001010011111101000011100011101010011111011000011001010000000111100100111110011001010110100100001001000111001E6944");
315   mpfr_init2(y, 130);
316   mpfr_set_ui(y, 5, MPFR_RNDN);
317   test_div(x, x, y, MPFR_RNDD); /* exact division */
318 
319   mpfr_set_prec(x, 64);
320   mpfr_set_prec(y, 64);
321   mpfr_set_str_binary(x, "0.10010010011011010100101001010111100000101110010010101E55");
322   mpfr_set_str_binary(y, "0.1E585");
323   test_div(x, x, y, MPFR_RNDN);
324   mpfr_set_str_binary(y, "0.10010010011011010100101001010111100000101110010010101E-529");
325   if (mpfr_cmp (x, y))
326     {
327       printf ("Error in mpfr_div for prec=64, rnd=MPFR_RNDN\n");
328       printf ("got        "); mpfr_print_binary(x); puts ("");
329       printf ("instead of "); mpfr_print_binary(y); puts ("");
330       exit(1);
331     }
332 
333   for (i=32; i<=64; i+=32)
334     {
335       mpfr_set_prec(x, i);
336       mpfr_set_prec(y, i);
337       mpfr_set_ui(x, 1, MPFR_RNDN);
338       RND_LOOP(j)
339         {
340           mpfr_set_ui (y, 1, MPFR_RNDN);
341           test_div (y, x, y, (mpfr_rnd_t) j);
342           if (mpfr_cmp_ui (y, 1))
343             {
344               printf ("mpfr_div failed for x=1.0, y=1.0, prec=%d rnd=%s\n",
345                       i, mpfr_print_rnd_mode ((mpfr_rnd_t) j));
346               printf ("got "); mpfr_print_binary(y); puts ("");
347               exit (1);
348             }
349         }
350     }
351 
352   mpfr_clear (x);
353   mpfr_clear (y);
354 }
355 
356 #define KMAX 10000
357 
358 /* given y = o(x/u), x, u, find the inexact flag by
359    multiplying y by u */
360 static int
361 get_inexact (mpfr_t y, mpfr_t x, mpfr_t u)
362 {
363   mpfr_t xx;
364   int inex;
365   mpfr_init2 (xx, mpfr_get_prec (y) + mpfr_get_prec (u));
366   mpfr_mul (xx, y, u, MPFR_RNDN); /* exact */
367   inex = mpfr_cmp (xx, x);
368   mpfr_clear (xx);
369   return inex;
370 }
371 
372 static void
373 check_hard (void)
374 {
375   mpfr_t u, v, q, q2;
376   mpfr_prec_t precu, precv, precq;
377   int rnd;
378   int inex, inex2, i, j;
379 
380   mpfr_init (q);
381   mpfr_init (q2);
382   mpfr_init (u);
383   mpfr_init (v);
384 
385   for (precq = MPFR_PREC_MIN; precq <= 64; precq ++)
386     {
387       mpfr_set_prec (q, precq);
388       mpfr_set_prec (q2, precq + 1);
389       for (j = 0; j < 2; j++)
390         {
391           if (j == 0)
392             {
393               do
394                 {
395                   mpfr_urandomb (q2, RANDS);
396                 }
397               while (mpfr_cmp_ui (q2, 0) == 0);
398             }
399           else /* use q2=1 */
400             mpfr_set_ui (q2, 1, MPFR_RNDN);
401       for (precv = precq; precv <= 10 * precq; precv += precq)
402         {
403           mpfr_set_prec (v, precv);
404           do
405             {
406               mpfr_urandomb (v, RANDS);
407             }
408           while (mpfr_cmp_ui (v, 0) == 0);
409           for (precu = precq; precu <= 10 * precq; precu += precq)
410             {
411               mpfr_set_prec (u, precu);
412               mpfr_mul (u, v, q2, MPFR_RNDN);
413               mpfr_nextbelow (u);
414               for (i = 0; i <= 2; i++)
415                 {
416                   RND_LOOP(rnd)
417                     {
418                       inex = test_div (q, u, v, (mpfr_rnd_t) rnd);
419                       inex2 = get_inexact (q, u, v);
420                       if (inex_cmp (inex, inex2))
421                         {
422                           printf ("Wrong inexact flag for rnd=%s: expected %d, got %d\n",
423                                   mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), inex2, inex);
424                           printf ("u=  "); mpfr_dump (u);
425                           printf ("v=  "); mpfr_dump (v);
426                           printf ("q=  "); mpfr_dump (q);
427                           mpfr_set_prec (q2, precq + precv);
428                           mpfr_mul (q2, q, v, MPFR_RNDN);
429                           printf ("q*v="); mpfr_dump (q2);
430                           exit (1);
431                         }
432                     }
433                   mpfr_nextabove (u);
434                 }
435             }
436         }
437         }
438     }
439 
440   mpfr_clear (q);
441   mpfr_clear (q2);
442   mpfr_clear (u);
443   mpfr_clear (v);
444 }
445 
446 static void
447 check_lowr (void)
448 {
449   mpfr_t x, y, z, z2, z3, tmp;
450   int k, c, c2;
451 
452 
453   mpfr_init2 (x, 1000);
454   mpfr_init2 (y, 100);
455   mpfr_init2 (tmp, 850);
456   mpfr_init2 (z, 10);
457   mpfr_init2 (z2, 10);
458   mpfr_init2 (z3, 50);
459 
460   for (k = 1; k < KMAX; k++)
461     {
462       do
463         {
464           mpfr_urandomb (z, RANDS);
465         }
466       while (mpfr_cmp_ui (z, 0) == 0);
467       do
468         {
469           mpfr_urandomb (tmp, RANDS);
470         }
471       while (mpfr_cmp_ui (tmp, 0) == 0);
472       mpfr_mul (x, z, tmp, MPFR_RNDN); /* exact */
473       c = test_div (z2, x, tmp, MPFR_RNDN);
474 
475       if (c || mpfr_cmp (z2, z))
476         {
477           printf ("Error in mpfr_div rnd=MPFR_RNDN\n");
478           printf ("got        "); mpfr_print_binary(z2); puts ("");
479           printf ("instead of "); mpfr_print_binary(z); puts ("");
480           printf ("inex flag = %d, expected 0\n", c);
481           exit (1);
482         }
483     }
484 
485   /* x has still precision 1000, z precision 10, and tmp prec 850 */
486   mpfr_set_prec (z2, 9);
487   for (k = 1; k < KMAX; k++)
488     {
489       mpfr_urandomb (z, RANDS);
490       do
491         {
492           mpfr_urandomb (tmp, RANDS);
493         }
494       while (mpfr_cmp_ui (tmp, 0) == 0);
495       mpfr_mul (x, z, tmp, MPFR_RNDN); /* exact */
496       c = test_div (z2, x, tmp, MPFR_RNDN);
497       /* since z2 has one less bit that z, either the division is exact
498          if z is representable on 9 bits, or we have an even round case */
499 
500       c2 = get_inexact (z2, x, tmp);
501       if ((mpfr_cmp (z2, z) == 0 && c) || inex_cmp (c, c2))
502         {
503           printf ("Error in mpfr_div rnd=MPFR_RNDN\n");
504           printf ("got        "); mpfr_print_binary(z2); puts ("");
505           printf ("instead of "); mpfr_print_binary(z); puts ("");
506           printf ("inex flag = %d, expected %d\n", c, c2);
507           exit (1);
508         }
509       else if (c == 2)
510         {
511           mpfr_nexttoinf (z);
512           if (mpfr_cmp(z2, z))
513             {
514               printf ("Error in mpfr_div [even rnd?] rnd=MPFR_RNDN\n");
515               printf ("Dividing ");
516               printf ("got        "); mpfr_print_binary(z2); puts ("");
517               printf ("instead of "); mpfr_print_binary(z); puts ("");
518               printf ("inex flag = %d\n", 1);
519               exit (1);
520             }
521         }
522       else if (c == -2)
523         {
524           mpfr_nexttozero (z);
525           if (mpfr_cmp(z2, z))
526             {
527               printf ("Error in mpfr_div [even rnd?] rnd=MPFR_RNDN\n");
528               printf ("Dividing ");
529               printf ("got        "); mpfr_print_binary(z2); puts ("");
530               printf ("instead of "); mpfr_print_binary(z); puts ("");
531               printf ("inex flag = %d\n", 1);
532               exit (1);
533             }
534         }
535     }
536 
537   mpfr_set_prec(x, 1000);
538   mpfr_set_prec(y, 100);
539   mpfr_set_prec(tmp, 850);
540   mpfr_set_prec(z, 10);
541   mpfr_set_prec(z2, 10);
542 
543   /* almost exact divisions */
544   for (k = 1; k < KMAX; k++)
545     {
546       do
547         {
548           mpfr_urandomb (z, RANDS);
549         }
550       while (mpfr_cmp_ui (z, 0) == 0);
551       do
552         {
553           mpfr_urandomb (tmp, RANDS);
554         }
555       while (mpfr_cmp_ui (tmp, 0) == 0);
556       mpfr_mul(x, z, tmp, MPFR_RNDN);
557       mpfr_set(y, tmp, MPFR_RNDD);
558       mpfr_nexttoinf (x);
559 
560       c = test_div(z2, x, y, MPFR_RNDD);
561       test_div(z3, x, y, MPFR_RNDD);
562       mpfr_set(z, z3, MPFR_RNDD);
563 
564       if (c != -1 || mpfr_cmp(z2, z))
565         {
566           printf ("Error in mpfr_div rnd=MPFR_RNDD\n");
567           printf ("got        "); mpfr_print_binary(z2); puts ("");
568           printf ("instead of "); mpfr_print_binary(z); puts ("");
569           printf ("inex flag = %d\n", c);
570           exit (1);
571         }
572 
573       mpfr_set (y, tmp, MPFR_RNDU);
574       test_div (z3, x, y, MPFR_RNDU);
575       mpfr_set (z, z3, MPFR_RNDU);
576       c = test_div (z2, x, y, MPFR_RNDU);
577       if (c != 1 || mpfr_cmp (z2, z))
578         {
579           printf ("Error in mpfr_div rnd=MPFR_RNDU\n");
580           printf ("u="); mpfr_dump (x);
581           printf ("v="); mpfr_dump (y);
582           printf ("got        "); mpfr_print_binary (z2); puts ("");
583           printf ("instead of "); mpfr_print_binary (z); puts ("");
584           printf ("inex flag = %d\n", c);
585           exit (1);
586         }
587     }
588 
589   mpfr_clear (x);
590   mpfr_clear (y);
591   mpfr_clear (z);
592   mpfr_clear (z2);
593   mpfr_clear (z3);
594   mpfr_clear (tmp);
595 }
596 
597 #define MAX_PREC 128
598 
599 static void
600 check_inexact (void)
601 {
602   mpfr_t x, y, z, u;
603   mpfr_prec_t px, py, pu;
604   int inexact, cmp;
605   mpfr_rnd_t rnd;
606 
607   mpfr_init (x);
608   mpfr_init (y);
609   mpfr_init (z);
610   mpfr_init (u);
611 
612   mpfr_set_prec (x, 28);
613   mpfr_set_prec (y, 28);
614   mpfr_set_prec (z, 1023);
615   mpfr_set_str_binary (x, "0.1000001001101101111100010011E0");
616   mpfr_set_str (z, "48284762641021308813686974720835219181653367326353400027913400579340343320519877153813133510034402932651132854764198688352364361009429039801248971901380781746767119334993621199563870113045276395603170432175354501451429471578325545278975153148347684600400321033502982713296919861760382863826626093689036010394", 10, MPFR_RNDN);
617   mpfr_div (x, x, z, MPFR_RNDN);
618   mpfr_set_str_binary (y, "0.1111001011001101001001111100E-1023");
619   if (mpfr_cmp (x, y))
620     {
621       printf ("Error in mpfr_div for prec=28, RNDN\n");
622       printf ("Expected "); mpfr_dump (y);
623       printf ("Got      "); mpfr_dump (x);
624       exit (1);
625     }
626 
627   mpfr_set_prec (x, 53);
628   mpfr_set_str_binary (x, "0.11101100110010100011011000000100001111011111110010101E0");
629   mpfr_set_prec (u, 127);
630   mpfr_set_str_binary (u, "0.1000001100110110110101110110101101111000110000001111111110000000011111001010110100110010111111111101000001011011101011101101000E-2");
631   mpfr_set_prec (y, 95);
632   inexact = test_div (y, x, u, MPFR_RNDN);
633   if (inexact != (cmp = get_inexact (y, x, u)))
634     {
635       printf ("Wrong inexact flag (0): expected %d, got %d\n", cmp, inexact);
636       printf ("x="); mpfr_out_str (stdout, 10, 99, x, MPFR_RNDN); printf ("\n");
637       printf ("u="); mpfr_out_str (stdout, 10, 99, u, MPFR_RNDN); printf ("\n");
638       printf ("y="); mpfr_out_str (stdout, 10, 99, y, MPFR_RNDN); printf ("\n");
639       exit (1);
640     }
641 
642   mpfr_set_prec (x, 33);
643   mpfr_set_str_binary (x, "0.101111100011011101010011101100001E0");
644   mpfr_set_prec (u, 2);
645   mpfr_set_str_binary (u, "0.1E0");
646   mpfr_set_prec (y, 28);
647   if ((inexact = test_div (y, x, u, MPFR_RNDN) >= 0))
648     {
649       printf ("Wrong inexact flag (1): expected -1, got %d\n",
650               inexact);
651       exit (1);
652     }
653 
654   mpfr_set_prec (x, 129);
655   mpfr_set_str_binary (x, "0.111110101111001100000101011100101100110011011101010001000110110101100101000010000001110110100001101010001010100010001111001101010E-2");
656   mpfr_set_prec (u, 15);
657   mpfr_set_str_binary (u, "0.101101000001100E-1");
658   mpfr_set_prec (y, 92);
659   if ((inexact = test_div (y, x, u, MPFR_RNDN)) <= 0)
660     {
661       printf ("Wrong inexact flag for rnd=MPFR_RNDN(1): expected 1, got %d\n",
662               inexact);
663       mpfr_dump (x);
664       mpfr_dump (u);
665       mpfr_dump (y);
666       exit (1);
667     }
668 
669   for (px=2; px<MAX_PREC; px++)
670     {
671       mpfr_set_prec (x, px);
672       mpfr_urandomb (x, RANDS);
673       for (pu=2; pu<=MAX_PREC; pu++)
674         {
675           mpfr_set_prec (u, pu);
676           do { mpfr_urandomb (u, RANDS); } while (mpfr_cmp_ui (u, 0) == 0);
677             {
678               py = MPFR_PREC_MIN + (randlimb () % (MAX_PREC - MPFR_PREC_MIN));
679               mpfr_set_prec (y, py);
680               mpfr_set_prec (z, py + pu);
681                 {
682                   rnd = RND_RAND ();
683                   inexact = test_div (y, x, u, rnd);
684                   if (mpfr_mul (z, y, u, rnd))
685                     {
686                       printf ("z <- y * u should be exact\n");
687                       exit (1);
688                     }
689                   cmp = mpfr_cmp (z, x);
690                   if (((inexact == 0) && (cmp != 0)) ||
691                       ((inexact > 0) && (cmp <= 0)) ||
692                       ((inexact < 0) && (cmp >= 0)))
693                     {
694                       printf ("Wrong inexact flag for rnd=%s\n",
695                               mpfr_print_rnd_mode(rnd));
696                       printf ("expected %d, got %d\n", cmp, inexact);
697                       printf ("x="); mpfr_print_binary (x); puts ("");
698                       printf ("u="); mpfr_print_binary (u); puts ("");
699                       printf ("y="); mpfr_print_binary (y); puts ("");
700                       printf ("y*u="); mpfr_print_binary (z); puts ("");
701                       exit (1);
702                     }
703                 }
704             }
705         }
706     }
707 
708   mpfr_clear (x);
709   mpfr_clear (y);
710   mpfr_clear (z);
711   mpfr_clear (u);
712 }
713 
714 static void
715 check_special (void)
716 {
717   mpfr_t  a, d, q;
718   mpfr_exp_t emax, emin;
719   int i;
720 
721   mpfr_init2 (a, 100L);
722   mpfr_init2 (d, 100L);
723   mpfr_init2 (q, 100L);
724 
725   /* 1/nan == nan */
726   mpfr_set_ui (a, 1L, MPFR_RNDN);
727   MPFR_SET_NAN (d);
728   mpfr_clear_flags ();
729   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
730   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
731 
732   /* nan/1 == nan */
733   MPFR_SET_NAN (a);
734   mpfr_set_ui (d, 1L, MPFR_RNDN);
735   mpfr_clear_flags ();
736   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
737   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
738 
739   /* +inf/1 == +inf */
740   MPFR_SET_INF (a);
741   MPFR_SET_POS (a);
742   mpfr_set_ui (d, 1L, MPFR_RNDN);
743   mpfr_clear_flags ();
744   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
745   MPFR_ASSERTN (mpfr_inf_p (q));
746   MPFR_ASSERTN (mpfr_sgn (q) > 0);
747   MPFR_ASSERTN (__gmpfr_flags == 0);
748 
749   /* +inf/-1 == -inf */
750   MPFR_SET_INF (a);
751   MPFR_SET_POS (a);
752   mpfr_set_si (d, -1, MPFR_RNDN);
753   mpfr_clear_flags ();
754   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
755   MPFR_ASSERTN (mpfr_inf_p (q));
756   MPFR_ASSERTN (mpfr_sgn (q) < 0);
757   MPFR_ASSERTN (__gmpfr_flags == 0);
758 
759   /* -inf/1 == -inf */
760   MPFR_SET_INF (a);
761   MPFR_SET_NEG (a);
762   mpfr_set_ui (d, 1L, MPFR_RNDN);
763   mpfr_clear_flags ();
764   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
765   MPFR_ASSERTN (mpfr_inf_p (q));
766   MPFR_ASSERTN (mpfr_sgn (q) < 0);
767   MPFR_ASSERTN (__gmpfr_flags == 0);
768 
769   /* -inf/-1 == +inf */
770   MPFR_SET_INF (a);
771   MPFR_SET_NEG (a);
772   mpfr_set_si (d, -1, MPFR_RNDN);
773   mpfr_clear_flags ();
774   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
775   MPFR_ASSERTN (mpfr_inf_p (q));
776   MPFR_ASSERTN (mpfr_sgn (q) > 0);
777   MPFR_ASSERTN (__gmpfr_flags == 0);
778 
779   /* 1/+inf == +0 */
780   mpfr_set_ui (a, 1L, MPFR_RNDN);
781   MPFR_SET_INF (d);
782   MPFR_SET_POS (d);
783   mpfr_clear_flags ();
784   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
785   MPFR_ASSERTN (mpfr_number_p (q));
786   MPFR_ASSERTN (mpfr_sgn (q) == 0);
787   MPFR_ASSERTN (MPFR_IS_POS (q));
788   MPFR_ASSERTN (__gmpfr_flags == 0);
789 
790   /* 1/-inf == -0 */
791   mpfr_set_ui (a, 1L, MPFR_RNDN);
792   MPFR_SET_INF (d);
793   MPFR_SET_NEG (d);
794   mpfr_clear_flags ();
795   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
796   MPFR_ASSERTN (mpfr_number_p (q));
797   MPFR_ASSERTN (mpfr_sgn (q) == 0);
798   MPFR_ASSERTN (MPFR_IS_NEG (q));
799   MPFR_ASSERTN (__gmpfr_flags == 0);
800 
801   /* -1/+inf == -0 */
802   mpfr_set_si (a, -1, MPFR_RNDN);
803   MPFR_SET_INF (d);
804   MPFR_SET_POS (d);
805   mpfr_clear_flags ();
806   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
807   MPFR_ASSERTN (mpfr_number_p (q));
808   MPFR_ASSERTN (mpfr_sgn (q) == 0);
809   MPFR_ASSERTN (MPFR_IS_NEG (q));
810   MPFR_ASSERTN (__gmpfr_flags == 0);
811 
812   /* -1/-inf == +0 */
813   mpfr_set_si (a, -1, MPFR_RNDN);
814   MPFR_SET_INF (d);
815   MPFR_SET_NEG (d);
816   mpfr_clear_flags ();
817   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
818   MPFR_ASSERTN (mpfr_number_p (q));
819   MPFR_ASSERTN (mpfr_sgn (q) == 0);
820   MPFR_ASSERTN (MPFR_IS_POS (q));
821   MPFR_ASSERTN (__gmpfr_flags == 0);
822 
823   /* 0/0 == nan */
824   mpfr_set_ui (a, 0L, MPFR_RNDN);
825   mpfr_set_ui (d, 0L, MPFR_RNDN);
826   mpfr_clear_flags ();
827   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
828   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
829 
830   /* +inf/+inf == nan */
831   MPFR_SET_INF (a);
832   MPFR_SET_POS (a);
833   MPFR_SET_INF (d);
834   MPFR_SET_POS (d);
835   mpfr_clear_flags ();
836   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
837   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
838 
839   /* 1/+0 = +inf */
840   mpfr_set_ui (a, 1, MPFR_RNDZ);
841   mpfr_set_ui (d, 0, MPFR_RNDZ);
842   mpfr_clear_flags ();
843   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
844   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
845   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
846 
847   /* 1/-0 = -inf */
848   mpfr_set_ui (a, 1, MPFR_RNDZ);
849   mpfr_set_ui (d, 0, MPFR_RNDZ);
850   mpfr_neg (d, d, MPFR_RNDZ);
851   mpfr_clear_flags ();
852   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
853   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
854   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
855 
856   /* -1/+0 = -inf */
857   mpfr_set_si (a, -1, MPFR_RNDZ);
858   mpfr_set_ui (d, 0, MPFR_RNDZ);
859   mpfr_clear_flags ();
860   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
861   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
862   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
863 
864   /* -1/-0 = +inf */
865   mpfr_set_si (a, -1, MPFR_RNDZ);
866   mpfr_set_ui (d, 0, MPFR_RNDZ);
867   mpfr_neg (d, d, MPFR_RNDZ);
868   mpfr_clear_flags ();
869   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
870   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
871   MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
872 
873   /* +inf/+0 = +inf */
874   MPFR_SET_INF (a);
875   MPFR_SET_POS (a);
876   mpfr_set_ui (d, 0, MPFR_RNDZ);
877   mpfr_clear_flags ();
878   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
879   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
880   MPFR_ASSERTN (__gmpfr_flags == 0);
881 
882   /* +inf/-0 = -inf */
883   MPFR_SET_INF (a);
884   MPFR_SET_POS (a);
885   mpfr_set_ui (d, 0, MPFR_RNDZ);
886   mpfr_neg (d, d, MPFR_RNDZ);
887   mpfr_clear_flags ();
888   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
889   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
890   MPFR_ASSERTN (__gmpfr_flags == 0);
891 
892   /* -inf/+0 = -inf */
893   MPFR_SET_INF (a);
894   MPFR_SET_NEG (a);
895   mpfr_set_ui (d, 0, MPFR_RNDZ);
896   mpfr_clear_flags ();
897   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
898   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
899   MPFR_ASSERTN (__gmpfr_flags == 0);
900 
901   /* -inf/-0 = +inf */
902   MPFR_SET_INF (a);
903   MPFR_SET_NEG (a);
904   mpfr_set_ui (d, 0, MPFR_RNDZ);
905   mpfr_neg (d, d, MPFR_RNDZ);
906   mpfr_clear_flags ();
907   MPFR_ASSERTN (test_div (q, a, d, MPFR_RNDZ) == 0); /* exact */
908   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
909   MPFR_ASSERTN (__gmpfr_flags == 0);
910 
911   /* check overflow */
912   emax = mpfr_get_emax ();
913   set_emax (1);
914   mpfr_set_ui (a, 1, MPFR_RNDZ);
915   mpfr_set_ui (d, 1, MPFR_RNDZ);
916   mpfr_div_2exp (d, d, 1, MPFR_RNDZ);
917   mpfr_clear_flags ();
918   test_div (q, a, d, MPFR_RNDU); /* 1 / 0.5 = 2 -> overflow */
919   MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
920   MPFR_ASSERTN (__gmpfr_flags == (MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT));
921   set_emax (emax);
922 
923   /* check underflow */
924   emin = mpfr_get_emin ();
925   set_emin (-1);
926   mpfr_set_ui (a, 1, MPFR_RNDZ);
927   mpfr_div_2exp (a, a, 2, MPFR_RNDZ);
928   mpfr_set_prec (d, mpfr_get_prec (q) + 8);
929   for (i = -1; i <= 1; i++)
930     {
931       int sign;
932 
933       /* Test 2^(-2) / (+/- (2 + eps)), with eps < 0, eps = 0, eps > 0.
934          -> underflow.
935          With div.c r5513, this test fails for eps > 0 in MPFR_RNDN. */
936       mpfr_set_ui (d, 2, MPFR_RNDZ);
937       if (i < 0)
938         mpfr_nextbelow (d);
939       if (i > 0)
940         mpfr_nextabove (d);
941       for (sign = 0; sign <= 1; sign++)
942         {
943           mpfr_clear_flags ();
944           test_div (q, a, d, MPFR_RNDZ); /* result = 0 */
945           MPFR_ASSERTN (__gmpfr_flags ==
946                         (MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT));
947           MPFR_ASSERTN (sign ? MPFR_IS_NEG (q) : MPFR_IS_POS (q));
948           MPFR_ASSERTN (MPFR_IS_ZERO (q));
949           mpfr_clear_flags ();
950           test_div (q, a, d, MPFR_RNDN); /* result = 0 iff eps >= 0 */
951           MPFR_ASSERTN (__gmpfr_flags ==
952                         (MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT));
953           MPFR_ASSERTN (sign ? MPFR_IS_NEG (q) : MPFR_IS_POS (q));
954           if (i < 0)
955             mpfr_nexttozero (q);
956           MPFR_ASSERTN (MPFR_IS_ZERO (q));
957           mpfr_neg (d, d, MPFR_RNDN);
958         }
959     }
960   set_emin (emin);
961 
962   mpfr_clear (a);
963   mpfr_clear (d);
964   mpfr_clear (q);
965 }
966 
967 static void
968 consistency (void)
969 {
970   mpfr_t x, y, z1, z2;
971   int i;
972 
973   mpfr_inits (x, y, z1, z2, (mpfr_ptr) 0);
974 
975   for (i = 0; i < 10000; i++)
976     {
977       mpfr_rnd_t rnd;
978       mpfr_prec_t px, py, pz, p;
979       int inex1, inex2;
980 
981       rnd = RND_RAND ();
982       px = (randlimb () % 256) + 2;
983       py = (randlimb () % 128) + 2;
984       pz = (randlimb () % 256) + 2;
985       mpfr_set_prec (x, px);
986       mpfr_set_prec (y, py);
987       mpfr_set_prec (z1, pz);
988       mpfr_set_prec (z2, pz);
989       mpfr_urandomb (x, RANDS);
990       do
991         mpfr_urandomb (y, RANDS);
992       while (mpfr_zero_p (y));
993       inex1 = mpfr_div (z1, x, y, rnd);
994       MPFR_ASSERTN (!MPFR_IS_NAN (z1));
995       p = MAX (MAX (px, py), pz);
996       if (mpfr_prec_round (x, p, MPFR_RNDN) != 0 ||
997           mpfr_prec_round (y, p, MPFR_RNDN) != 0)
998         {
999           printf ("mpfr_prec_round error for i = %d\n", i);
1000           exit (1);
1001         }
1002       inex2 = mpfr_div (z2, x, y, rnd);
1003       MPFR_ASSERTN (!MPFR_IS_NAN (z2));
1004       if (inex1 != inex2 || mpfr_cmp (z1, z2) != 0)
1005         {
1006           printf ("Consistency error for i = %d\n", i);
1007           exit (1);
1008         }
1009     }
1010 
1011   mpfr_clears (x, y, z1, z2, (mpfr_ptr) 0);
1012 }
1013 
1014 /* Reported by Carl Witty on 2007-06-03 */
1015 static void
1016 test_20070603 (void)
1017 {
1018   mpfr_t n, d, q, c;
1019 
1020   mpfr_init2 (n, 128);
1021   mpfr_init2 (d, 128);
1022   mpfr_init2 (q, 31);
1023   mpfr_init2 (c, 31);
1024 
1025   mpfr_set_str (n, "10384593717069655257060992206846485", 10, MPFR_RNDN);
1026   mpfr_set_str (d, "10384593717069655257060992206847132", 10, MPFR_RNDN);
1027   mpfr_div (q, n, d, MPFR_RNDU);
1028 
1029   mpfr_set_ui (c, 1, MPFR_RNDN);
1030   if (mpfr_cmp (q, c) != 0)
1031     {
1032       printf ("Error in test_20070603\nGot        ");
1033       mpfr_dump (q);
1034       printf ("instead of ");
1035       mpfr_dump (c);
1036       exit (1);
1037     }
1038 
1039   /* same for 64-bit machines */
1040   mpfr_set_prec (n, 256);
1041   mpfr_set_prec (d, 256);
1042   mpfr_set_prec (q, 63);
1043   mpfr_set_str (n, "822752278660603021077484591278675252491367930877209729029898240", 10, MPFR_RNDN);
1044   mpfr_set_str (d, "822752278660603021077484591278675252491367930877212507873738752", 10, MPFR_RNDN);
1045   mpfr_div (q, n, d, MPFR_RNDU);
1046   if (mpfr_cmp (q, c) != 0)
1047     {
1048       printf ("Error in test_20070603\nGot        ");
1049       mpfr_dump (q);
1050       printf ("instead of ");
1051       mpfr_dump (c);
1052       exit (1);
1053     }
1054 
1055   mpfr_clear (n);
1056   mpfr_clear (d);
1057   mpfr_clear (q);
1058   mpfr_clear (c);
1059 }
1060 
1061 /* Bug found while adding tests for mpfr_cot */
1062 static void
1063 test_20070628 (void)
1064 {
1065   mpfr_exp_t old_emax;
1066   mpfr_t x, y;
1067   int inex, err = 0;
1068 
1069   old_emax = mpfr_get_emax ();
1070 
1071   if (mpfr_set_emax (256))
1072     {
1073       printf ("Can't change exponent range\n");
1074       exit (1);
1075     }
1076 
1077   mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
1078   mpfr_set_si (x, -1, MPFR_RNDN);
1079   mpfr_set_si_2exp (y, 1, -256, MPFR_RNDN);
1080   mpfr_clear_flags ();
1081   inex = mpfr_div (x, x, y, MPFR_RNDD);
1082   if (MPFR_SIGN (x) >= 0 || ! mpfr_inf_p (x))
1083     {
1084       printf ("Error in test_20070628: expected -Inf, got\n");
1085       mpfr_dump (x);
1086       err++;
1087     }
1088   if (inex >= 0)
1089     {
1090       printf ("Error in test_20070628: expected inex < 0, got %d\n", inex);
1091       err++;
1092     }
1093   if (! mpfr_overflow_p ())
1094     {
1095       printf ("Error in test_20070628: overflow flag is not set\n");
1096       err++;
1097     }
1098   mpfr_clears (x, y, (mpfr_ptr) 0);
1099   mpfr_set_emax (old_emax);
1100 }
1101 
1102 #define TEST_FUNCTION test_div
1103 #define TWO_ARGS
1104 #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), randlimb () % 100, RANDS)
1105 #include "tgeneric.c"
1106 
1107 int
1108 main (int argc, char *argv[])
1109 {
1110   tests_start_mpfr ();
1111 
1112   check_inexact ();
1113   check_hard ();
1114   check_special ();
1115   check_lowr ();
1116   check_float (); /* checks single precision */
1117   check_double ();
1118   check_convergence ();
1119   check_64 ();
1120 
1121   check4("4.0","4.503599627370496e15", MPFR_RNDZ, 62,
1122    "0.10000000000000000000000000000000000000000000000000000000000000E-49");
1123   check4("1.0","2.10263340267725788209e+187", MPFR_RNDU, 65,
1124    "0.11010011111001101011111001100111110100000001101001111100111000000E-622");
1125   check4("2.44394909079968374564e-150", "2.10263340267725788209e+187",MPFR_RNDU,
1126          65,
1127   "0.11010011111001101011111001100111110100000001101001111100111000000E-1119");
1128 
1129   consistency ();
1130   test_20070603 ();
1131   test_20070628 ();
1132   test_generic (2, 800, 50);
1133 
1134   tests_end_mpfr ();
1135   return 0;
1136 }
1137