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