1 /* Test file for mpfr_ui_div.
3 Copyright 2000, 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.
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. */
26 #include "mpfr-test.h"
28 /* checks that y/x gives the right result with 53 bits of precision */
30 check (unsigned long y, const char *xs, mpfr_rnd_t rnd_mode, const char *zs)
34 mpfr_inits2 (53, xx, zz, (mpfr_ptr) 0);
35 mpfr_set_str1 (xx, xs);
36 mpfr_ui_div (zz, y, xx, rnd_mode);
37 if (mpfr_cmp_str1(zz, zs))
39 printf ("expected quotient is %s, got ", zs);
40 mpfr_out_str (stdout, 10, 0, zz, MPFR_RNDN);
41 printf ("mpfr_ui_div failed for y=%lu x=%s with rnd_mode=%s\n",
42 y, xs, mpfr_print_rnd_mode (rnd_mode));
45 mpfr_clears (xx, zz, (mpfr_ptr) 0);
61 for (px=2; px<300; px++)
63 mpfr_set_prec (x, px);
66 mpfr_urandomb (x, RANDS);
68 while (mpfr_cmp_ui (x, 0) == 0);
70 for (py=2; py<300; py++)
72 mpfr_set_prec (y, py);
73 mpfr_set_prec (z, py + px);
74 for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
76 inexact = mpfr_ui_div (y, u, x, (mpfr_rnd_t) rnd);
77 if (mpfr_mul (z, y, x, (mpfr_rnd_t) rnd))
79 printf ("z <- y * x should be exact\n");
82 cmp = mpfr_cmp_ui (z, u);
83 if (((inexact == 0) && (cmp != 0)) ||
84 ((inexact > 0) && (cmp <= 0)) ||
85 ((inexact < 0) && (cmp >= 0)))
87 printf ("Wrong inexact flag for u=%lu, rnd=%s\n",
88 u, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
89 printf ("expected %d, got %d\n", cmp, inexact);
90 printf ("x="); mpfr_print_binary (x); puts ("");
91 printf ("y="); mpfr_print_binary (y); puts ("");
92 printf ("y*x="); mpfr_print_binary (z); puts ("");
109 mpfr_init2 (d, 100L);
110 mpfr_init2 (q, 100L);
116 MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, MPFR_RNDZ) == 0); /* exact */
117 MPFR_ASSERTN (mpfr_number_p (q));
118 MPFR_ASSERTN (mpfr_sgn (q) == 0);
119 MPFR_ASSERTN (__gmpfr_flags == 0);
125 MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, MPFR_RNDZ) == 0); /* exact */
126 MPFR_ASSERTN (mpfr_number_p (q));
127 MPFR_ASSERTN (mpfr_sgn (q) == 0);
128 MPFR_ASSERTN (__gmpfr_flags == 0);
133 MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, MPFR_RNDZ) == 0); /* exact */
134 MPFR_ASSERTN (mpfr_nan_p (q));
135 MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
138 mpfr_set_ui (d, 0L, MPFR_RNDN);
140 MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, MPFR_RNDZ) == 0); /* exact */
141 MPFR_ASSERTN (mpfr_nan_p (q));
142 MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
145 mpfr_set_ui (d, 0L, MPFR_RNDN);
147 MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, MPFR_RNDZ) == 0); /* exact */
148 MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) > 0);
149 MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
152 mpfr_set_ui (d, 0L, MPFR_RNDN);
153 mpfr_neg (d, d, MPFR_RNDN);
155 MPFR_ASSERTN (mpfr_ui_div (q, 1L, d, MPFR_RNDZ) == 0); /* exact */
156 MPFR_ASSERTN (mpfr_inf_p (q) && mpfr_sgn (q) < 0);
157 MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
160 mpfr_set_ui (d, 1L, MPFR_RNDN);
162 MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, MPFR_RNDZ) == 0); /* exact */
163 MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_POS (q));
164 MPFR_ASSERTN (__gmpfr_flags == 0);
167 mpfr_set_si (d, -1, MPFR_RNDN);
169 MPFR_ASSERTN (mpfr_ui_div (q, 0L, d, MPFR_RNDZ) == 0); /* exact */
170 MPFR_ASSERTN (mpfr_cmp_ui (q, 0) == 0 && MPFR_IS_NEG (q));
171 MPFR_ASSERTN (__gmpfr_flags == 0);
178 mpfr_inv (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t r)
180 return mpfr_ui_div (y, 1, x, r);
184 main (int argc, char *argv[])
190 check(948002822, "1.22191250737771397120e+20", MPFR_RNDN,
191 "7.758352715731357946e-12");
192 check(1976245324, "1.25296395864546893357e+232", MPFR_RNDZ,
193 "1.5772563211925444801e-223");
194 check(740454110, "2.11496253355831863313e+183", MPFR_RNDZ,
195 "3.5010270784996976041e-175");
196 check(1690540942, "1.28278599852446657468e-276", MPFR_RNDU,
197 "1.3178666932321966062e285");
198 check(1476599377, "-2.14191393656148625995e+305", MPFR_RNDD,
199 "-6.8938315017943889615e-297");
202 data_check ("data/inv", mpfr_inv, "mpfr_ui_div(1,x)");