1 /* Test file for mpfr_ui_div. 2 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. 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 /* checks that y/x gives the right result with 53 bits of precision */ 29 static void 30 check (unsigned long y, const char *xs, mpfr_rnd_t rnd_mode, const char *zs) 31 { 32 mpfr_t xx, zz; 33 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)) 38 { 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)); 43 exit (1); 44 } 45 mpfr_clears (xx, zz, (mpfr_ptr) 0); 46 } 47 48 static void 49 check_inexact (void) 50 { 51 mpfr_t x, y, z; 52 mpfr_prec_t px, py; 53 int inexact, cmp; 54 unsigned long int u; 55 int rnd; 56 57 mpfr_init (x); 58 mpfr_init (y); 59 mpfr_init (z); 60 61 for (px=2; px<300; px++) 62 { 63 mpfr_set_prec (x, px); 64 do 65 { 66 mpfr_urandomb (x, RANDS); 67 } 68 while (mpfr_cmp_ui (x, 0) == 0); 69 u = randlimb (); 70 for (py=2; py<300; py++) 71 { 72 mpfr_set_prec (y, py); 73 mpfr_set_prec (z, py + px); 74 for (rnd = 0; rnd < MPFR_RND_MAX; rnd++) 75 { 76 inexact = mpfr_ui_div (y, u, x, (mpfr_rnd_t) rnd); 77 if (mpfr_mul (z, y, x, (mpfr_rnd_t) rnd)) 78 { 79 printf ("z <- y * x should be exact\n"); 80 exit (1); 81 } 82 cmp = mpfr_cmp_ui (z, u); 83 if (((inexact == 0) && (cmp != 0)) || 84 ((inexact > 0) && (cmp <= 0)) || 85 ((inexact < 0) && (cmp >= 0))) 86 { 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 (""); 93 exit (1); 94 } 95 } 96 } 97 } 98 99 mpfr_clear (x); 100 mpfr_clear (y); 101 mpfr_clear (z); 102 } 103 104 static void 105 check_special (void) 106 { 107 mpfr_t d, q; 108 109 mpfr_init2 (d, 100L); 110 mpfr_init2 (q, 100L); 111 112 /* 1/+inf == 0 */ 113 MPFR_SET_INF (d); 114 MPFR_SET_POS (d); 115 mpfr_clear_flags (); 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); 120 121 /* 1/-inf == -0 */ 122 MPFR_SET_INF (d); 123 MPFR_SET_NEG (d); 124 mpfr_clear_flags (); 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); 129 130 /* 1/nan == nan */ 131 MPFR_SET_NAN (d); 132 mpfr_clear_flags (); 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); 136 137 /* 0/0 == nan */ 138 mpfr_set_ui (d, 0L, MPFR_RNDN); 139 mpfr_clear_flags (); 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); 143 144 /* 1/+0 = +inf */ 145 mpfr_set_ui (d, 0L, MPFR_RNDN); 146 mpfr_clear_flags (); 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); 150 151 /* 1/-0 = -inf */ 152 mpfr_set_ui (d, 0L, MPFR_RNDN); 153 mpfr_neg (d, d, MPFR_RNDN); 154 mpfr_clear_flags (); 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); 158 159 /* 0/1 = +0 */ 160 mpfr_set_ui (d, 1L, MPFR_RNDN); 161 mpfr_clear_flags (); 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); 165 166 /* 0/-1 = -0 */ 167 mpfr_set_si (d, -1, MPFR_RNDN); 168 mpfr_clear_flags (); 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); 172 173 mpfr_clear (d); 174 mpfr_clear (q); 175 } 176 177 static int 178 mpfr_inv (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t r) 179 { 180 return mpfr_ui_div (y, 1, x, r); 181 } 182 183 int 184 main (int argc, char *argv[]) 185 { 186 tests_start_mpfr (); 187 188 check_special (); 189 check_inexact (); 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"); 200 201 /* inv is for 1/x */ 202 data_check ("data/inv", mpfr_inv, "mpfr_ui_div(1,x)"); 203 204 tests_end_mpfr (); 205 return 0; 206 } 207