1 /* Test file for mpfr_set_str. 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 <stdlib.h> 24 25 #include "mpfr-test.h" 26 27 #define N 30000 28 29 #define CHECK53(y, s, r, x, t, n) \ 30 mpfr_set_str (y, s, 10, r); \ 31 mpfr_set_str_binary (x, t); \ 32 if (mpfr_cmp (x, y)) \ 33 { \ 34 printf ("Error in mpfr_set_str (%d):\n", n); \ 35 mpfr_print_binary (x); \ 36 puts (""); \ 37 mpfr_print_binary (y); \ 38 puts (""); \ 39 mpfr_clear (x); \ 40 mpfr_clear (y); \ 41 exit (1); \ 42 } 43 44 static void 45 check_underflow (void) 46 { 47 mpfr_t a; 48 mpfr_exp_t emin, emax; 49 int res; 50 51 mpfr_init (a); 52 53 /* Check underflow */ 54 emin = mpfr_get_emin (); 55 set_emin (-20); 56 res = mpfr_set_str (a, "0.00000000001", 10, MPFR_RNDZ); 57 if (!MPFR_IS_ZERO (a)) 58 { 59 printf("ERROR for mpfr_set_str (a, \"0.00000000001\", 10, MPFR_RNDN)\n" 60 " with emin=-20\n" 61 "res=%d\n", res); 62 mpfr_dump (a); 63 exit (1); 64 } 65 set_emin (emin); 66 67 /* check overflow */ 68 emax = mpfr_get_emax (); 69 set_emax (1073741823); /* 2^30-1 */ 70 mpfr_set_str (a, "2E1000000000", 10, MPFR_RNDN); 71 if (!mpfr_inf_p (a) || mpfr_sgn (a) < 0) 72 { 73 printf("ERROR for mpfr_set_str (a, \"2E1000000000\", 10, MPFR_RNDN);\n"); 74 exit (1); 75 } 76 set_emax (emax); 77 78 mpfr_clear (a); 79 } 80 81 /* Bug found by Christoph Lauter. */ 82 static void 83 bug20081028 (void) 84 { 85 mpfr_t x; 86 const char *s = "0.10000000000000000000000000000001E1"; 87 88 mpfr_init2 (x, 32); 89 mpfr_set_str (x, "1.00000000000000000006", 10, MPFR_RNDU); 90 if (! mpfr_greater_p (x, __gmpfr_one)) 91 { 92 printf ("Error in bug20081028:\nExpected %s\nGot ", s); 93 mpfr_dump (x); 94 exit (1); 95 } 96 mpfr_clear (x); 97 } 98 99 int 100 main (int argc, char *argv[]) 101 { 102 mpfr_t x, y; 103 unsigned long k, bd, nc, i; 104 char *str, *str2; 105 mpfr_exp_t e; 106 int base, logbase, prec, baseprec, ret, obase; 107 108 tests_start_mpfr (); 109 110 if (argc >= 2) /* tset_str <string> [<prec>] [<ibase>] [<obase>] */ 111 { 112 prec = (argc >= 3) ? atoi (argv[2]) : 53; 113 base = (argc >= 4) ? atoi (argv[3]) : 2; 114 obase = (argc >= 5) ? atoi (argv[4]) : 10; 115 mpfr_init2 (x, prec); 116 mpfr_set_str (x, argv[1], base, MPFR_RNDN); 117 mpfr_out_str (stdout, obase, 0, x, MPFR_RNDN); 118 puts (""); 119 mpfr_clear (x); 120 return 0; 121 } 122 123 mpfr_init2 (x, 2); 124 125 nc = (argc > 1) ? atoi(argv[1]) : 53; 126 if (nc < 100) 127 nc = 100; 128 129 bd = randlimb () & 8; 130 131 str2 = str = (char*) (*__gmp_allocate_func) (nc); 132 133 if (bd) 134 { 135 for(k = 1; k <= bd; k++) 136 *(str2++) = (randlimb () & 1) + '0'; 137 } 138 else 139 *(str2++) = '0'; 140 141 *(str2++) = '.'; 142 143 for (k = 1; k < nc - 17 - bd; k++) 144 *(str2++) = '0' + (char) (randlimb () & 1); 145 146 *(str2++) = 'e'; 147 sprintf (str2, "%d", (int) (randlimb () & INT_MAX) + INT_MIN/2); 148 149 mpfr_set_prec (x, nc + 10); 150 mpfr_set_str_binary (x, str); 151 152 mpfr_set_prec (x, 54); 153 mpfr_set_str_binary (x, "0.100100100110110101001010010101111000001011100100101010E-529"); 154 mpfr_init2 (y, 54); 155 mpfr_set_str (y, "4.936a52bc17254@-133", 16, MPFR_RNDN); 156 if (mpfr_cmp (x, y)) 157 { 158 printf ("Error in mpfr_set_str (1a):\n"); 159 mpfr_print_binary (x); 160 puts (""); 161 mpfr_print_binary (y); 162 puts (""); 163 mpfr_clear (x); 164 mpfr_clear (y); 165 exit (1); 166 } 167 168 mpfr_set_str_binary (x, "0.111111101101110010111010100110000111011001010100001101E-529"); 169 mpfr_set_str (y, "0.fedcba98765434P-529", 16, MPFR_RNDN); 170 if (mpfr_cmp (x, y)) 171 { 172 printf ("Error in mpfr_set_str (1b):\n"); 173 mpfr_print_binary (x); 174 puts (""); 175 mpfr_print_binary (y); 176 puts (""); 177 mpfr_clear (x); 178 mpfr_clear (y); 179 exit (1); 180 } 181 182 (*__gmp_free_func) (str, nc); 183 184 mpfr_set_prec (x, 53); 185 mpfr_set_str_binary (x, "+110101100.01010000101101000000100111001000101011101110E00"); 186 187 mpfr_set_str_binary (x, "1.0"); 188 if (mpfr_cmp_ui (x, 1)) 189 { 190 printf ("Error in mpfr_set_str_binary for s=1.0\n"); 191 mpfr_clear(x); 192 mpfr_clear(y); 193 exit(1); 194 } 195 196 mpfr_set_str_binary (x, "+0000"); 197 mpfr_set_str_binary (x, "+0000E0"); 198 mpfr_set_str_binary (x, "0000E0"); 199 if (mpfr_cmp_ui (x, 0)) 200 { 201 printf ("Error in mpfr_set_str_binary for s=0.0\n"); 202 mpfr_clear (x); 203 mpfr_clear (y); 204 exit (1); 205 } 206 207 mpfr_set_str (x, "+243495834958.53452345E1", 10, MPFR_RNDN); 208 mpfr_set_str (x, "9007199254740993", 10, MPFR_RNDN); 209 mpfr_set_str (x, "9007199254740992", 10, MPFR_RNDU); 210 mpfr_set_str (x, "9007199254740992", 10, MPFR_RNDD); 211 mpfr_set_str (x, "9007199254740992", 10, MPFR_RNDZ); 212 213 /* check a random number printed and read is not modified */ 214 prec = 53; 215 mpfr_set_prec (x, prec); 216 mpfr_set_prec (y, prec); 217 for (i=0;i<N;i++) 218 { 219 mpfr_rnd_t rnd; 220 221 mpfr_urandomb (x, RANDS); 222 rnd = RND_RAND (); 223 logbase = (randlimb () % 5) + 1; 224 base = 1 << logbase; 225 /* Warning: the number of bits needed to print exactly a number of 226 'prec' bits in base 2^logbase may be greater than ceil(prec/logbase), 227 for example 0.11E-1 in base 2 cannot be written exactly with only 228 one digit in base 4 */ 229 if (base == 2) 230 baseprec = prec; 231 else 232 baseprec = 1 + (prec - 2 + logbase) / logbase; 233 str = mpfr_get_str (NULL, &e, base, baseprec, x, rnd); 234 mpfr_set_str (y, str, base, rnd); 235 MPFR_EXP(y) += logbase * (e - strlen (str)); 236 if (mpfr_cmp (x, y)) 237 { 238 printf ("mpfr_set_str o mpfr_get_str <> id for rnd_mode=%s\n", 239 mpfr_print_rnd_mode (rnd)); 240 printf ("x="); 241 mpfr_print_binary (x); 242 puts (""); 243 printf ("s=%s, exp=%d, base=%d\n", str, (int) e, base); 244 printf ("y="); 245 mpfr_print_binary (y); 246 puts (""); 247 mpfr_clear (x); 248 mpfr_clear (y); 249 exit (1); 250 } 251 (*__gmp_free_func) (str, strlen (str) + 1); 252 } 253 254 for (i = 2; i <= 62; i++) 255 { 256 if (mpfr_set_str (x, "@NaN@(garbage)", i, MPFR_RNDN) != 0 || 257 !mpfr_nan_p(x)) 258 { 259 printf ("mpfr_set_str failed on @NaN@(garbage)\n"); 260 exit (1); 261 } 262 263 /* 264 if (mpfr_set_str (x, "@Inf@garbage", i, MPFR_RNDN) != 0 || 265 !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) 266 { 267 printf ("mpfr_set_str failed on @Inf@garbage\n"); 268 exit (1); 269 } 270 271 if (mpfr_set_str (x, "-@Inf@garbage", i, MPFR_RNDN) != 0 || 272 !mpfr_inf_p(x) || MPFR_SIGN(x) > 0) 273 { 274 printf ("mpfr_set_str failed on -@Inf@garbage\n"); 275 exit (1); 276 } 277 278 if (mpfr_set_str (x, "+@Inf@garbage", i, MPFR_RNDN) != 0 || 279 !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) 280 { 281 printf ("mpfr_set_str failed on +@Inf@garbage\n"); 282 exit (1); 283 } 284 */ 285 286 if (i > 16) 287 continue; 288 289 if (mpfr_set_str (x, "NaN", i, MPFR_RNDN) != 0 || 290 !mpfr_nan_p(x)) 291 { 292 printf ("mpfr_set_str failed on NaN\n"); 293 exit (1); 294 } 295 296 if (mpfr_set_str (x, "Inf", i, MPFR_RNDN) != 0 || 297 !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) 298 { 299 printf ("mpfr_set_str failed on Inf\n"); 300 exit (1); 301 } 302 303 if (mpfr_set_str (x, "-Inf", i, MPFR_RNDN) != 0 || 304 !mpfr_inf_p(x) || MPFR_SIGN(x) > 0) 305 { 306 printf ("mpfr_set_str failed on -Inf\n"); 307 exit (1); 308 } 309 310 if (mpfr_set_str (x, "+Inf", i, MPFR_RNDN) != 0 || 311 !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) 312 { 313 printf ("mpfr_set_str failed on +Inf\n"); 314 exit (1); 315 } 316 } 317 318 /* check that mpfr_set_str works for uppercase letters too */ 319 mpfr_set_prec (x, 10); 320 mpfr_set_str (x, "B", 16, MPFR_RNDN); 321 if (mpfr_cmp_ui (x, 11) != 0) 322 { 323 printf ("mpfr_set_str does not work for uppercase letters\n"); 324 exit (1); 325 } 326 327 /* start of tests added by Alain Delplanque */ 328 329 /* in this example an overflow can occur */ 330 mpfr_set_prec (x, 64); 331 mpfr_set_prec (y, 64); 332 mpfr_set_str_binary (x, "1.0E-532"); 333 mpfr_set_str (y, "0.71128279983522479470@-160", 10, MPFR_RNDU); 334 if (mpfr_cmp (x, y)) 335 { 336 printf ("Error in mpfr_set_str (2):\n"); 337 mpfr_print_binary (x); 338 puts (""); 339 mpfr_print_binary (y); 340 puts (""); 341 mpfr_clear (x); 342 mpfr_clear (y); 343 exit (1); 344 } 345 346 /* in this example, I think there was a pb in the old function : 347 result of mpfr_set_str_old for the same number , but with more 348 precision is: 1.111111111110000000000000000111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100111000100001100000010101100111010e184 349 this result is the same as mpfr_set_str */ 350 mpfr_set_prec (x, 64); 351 mpfr_set_prec (y, 64); 352 mpfr_set_str_binary (x, "1.111111111110000000000000000111111111111111111111111110000000001E184"); 353 mpfr_set_str (y, "0.jo08hg31hc5mmpj5mjjmgn55p2h35g@39", 27, MPFR_RNDU); 354 /* y = 49027884868983130654865109690613178467841148597221480052 */ 355 if (mpfr_cmp (x, y)) 356 { 357 printf ("Error in mpfr_set_str (3):\n"); 358 mpfr_print_binary (x); 359 puts (""); 360 mpfr_print_binary (y); 361 puts (""); 362 mpfr_clear (x); 363 mpfr_clear (y); 364 exit (1); 365 } 366 367 /* not exact rounding in mpfr_set_str 368 same number with more precision is : 1.111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011111101000001101110110010101101000010100110011101110010001110e195 369 this result is the same as mpfr_set_str */ 370 /* problem was : can_round was call with MPFR_RNDN round mode, 371 so can_round use an error : 1/2 * 2^err * ulp(y) 372 instead of 2^err * ulp(y) 373 I have increase err by 1 */ 374 mpfr_set_prec (x, 64); /* it was round down instead of up */ 375 mpfr_set_prec (y, 64); 376 mpfr_set_str_binary (x, "1.111111111111111111111111111000000000000000000000000000000000001e195"); 377 mpfr_set_str (y, "0.6e23ekb6acgh96abk10b6c9f2ka16i@45", 21, MPFR_RNDU); 378 /* y = 100433627392042473064661483711179345482301462325708736552078 */ 379 if (mpfr_cmp (x, y)) 380 { 381 printf ("Error in mpfr_set_str (4):\n"); 382 mpfr_print_binary (x); 383 puts (""); 384 mpfr_print_binary (y); 385 puts (""); 386 mpfr_clear (x); 387 mpfr_clear (y); 388 exit (1); 389 } 390 391 /* may be an error in mpfr_set_str_old 392 with more precision : 1.111111100000001111110000000000011111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111101010001110111011000010111001011100110110e180 */ 393 mpfr_set_prec (x, 64); /* it was round down instead of up */ 394 mpfr_set_prec (y, 64); 395 mpfr_set_str_binary (x, "1.111111100000001111110000000000011111011111111111111111111111111e180"); 396 mpfr_set_str (y, "0.10j8j2k82ehahha56390df0a1de030@41", 23, MPFR_RNDZ); 397 /* y = 3053110535624388280648330929253842828159081875986159414 */ 398 if (mpfr_cmp (x, y)) 399 { 400 printf ("Error in mpfr_set_str (5):\n"); 401 mpfr_print_binary (x); 402 puts (""); 403 mpfr_print_binary (y); 404 puts (""); 405 mpfr_clear (x); 406 mpfr_clear (y); 407 exit (1); 408 } 409 410 mpfr_set_prec (x, 64); 411 mpfr_set_prec (y, 64); 412 mpfr_set_str (y, "0.jrchfhpp9en7hidqm9bmcofid9q3jg@39", 28, MPFR_RNDU); 413 /* y = 196159429139499688661464718784226062699788036696626429952 */ 414 mpfr_set_str_binary (x, "0.1111111111111111111111111111111000000000000011100000001111100001E187"); 415 if (mpfr_cmp (x, y)) 416 { 417 printf ("Error in mpfr_set_str (6):\n"); 418 mpfr_print_binary (x); 419 puts (""); 420 mpfr_print_binary (y); 421 puts (""); 422 mpfr_clear (x); 423 mpfr_clear (y); 424 exit (1); 425 } 426 427 mpfr_set_prec (x, 64); 428 mpfr_set_prec (y, 64); 429 mpfr_set_str (y, "0.h148m5ld5cf8gk1kd70b6ege92g6ba@47", 24, MPFR_RNDZ); 430 /* y = 52652933527468502324759448399183654588831274530295083078827114496 */ 431 mpfr_set_str_binary (x, "0.1111111111111100000000001000000000000000000011111111111111101111E215"); 432 if (mpfr_cmp (x, y)) 433 { 434 printf ("Error in mpfr_set_str (7):\n"); 435 mpfr_print_binary (x); 436 puts (""); 437 mpfr_print_binary (y); 438 puts (""); 439 mpfr_clear (x); 440 mpfr_clear (y); 441 exit (1); 442 } 443 444 /* worst cases for rounding to nearest in double precision */ 445 mpfr_set_prec (x, 53); 446 mpfr_set_prec (y, 53); 447 448 mpfr_set_str (y, "5e125", 10, MPFR_RNDN); 449 mpfr_set_str_binary (x, "0.10111101000101110110011000100000101001010000000111111E418"); 450 if (mpfr_cmp (x, y)) 451 { 452 printf ("Error in mpfr_set_str (8):\n"); 453 mpfr_print_binary (x); 454 puts (""); 455 mpfr_print_binary (y); 456 puts (""); 457 mpfr_clear (x); 458 mpfr_clear (y); 459 exit (1); 460 } 461 462 mpfr_set_str (y, "69e267", 10, MPFR_RNDN); 463 mpfr_set_str_binary (x, "0.10000101101111100101101100000110010011001010011011010E894"); 464 if (mpfr_cmp (x, y)) 465 { 466 printf ("Error in mpfr_set_str (9):\n"); 467 mpfr_print_binary (x); 468 puts (""); 469 mpfr_print_binary (y); 470 puts (""); 471 mpfr_clear (x); 472 mpfr_clear (y); 473 exit (1); 474 } 475 476 mpfr_set_str (y, "623e100", 10, MPFR_RNDN); 477 mpfr_set_str_binary (x, "0.10110010000001010011000101111001110101000001111011111E342"); 478 if (mpfr_cmp (x, y)) 479 { 480 printf ("Error in mpfr_set_str (10):\n"); 481 mpfr_print_binary (x); 482 puts (""); 483 mpfr_print_binary (y); 484 puts (""); 485 mpfr_clear (x); 486 mpfr_clear (y); 487 exit (1); 488 } 489 490 mpfr_set_str (y, "3571e263", 10, MPFR_RNDN); 491 mpfr_set_str_binary (x, "0.10110001001100100010011000110000111010100000110101010E886"); 492 if (mpfr_cmp (x, y)) 493 { 494 printf ("Error in mpfr_set_str (11):\n"); 495 mpfr_print_binary (x); 496 puts (""); 497 mpfr_print_binary (y); 498 puts (""); 499 mpfr_clear (x); 500 mpfr_clear (y); 501 exit (1); 502 } 503 504 mpfr_set_str (y, "75569e-254", 10, MPFR_RNDN); 505 mpfr_set_str_binary (x, "0.10101101001000110001011011001000111000110101010110011E-827"); 506 if (mpfr_cmp (x, y)) 507 { 508 printf ("Error in mpfr_set_str (12):\n"); 509 mpfr_print_binary (x); 510 puts (""); 511 mpfr_print_binary (y); 512 puts (""); 513 mpfr_clear (x); 514 mpfr_clear (y); 515 exit (1); 516 } 517 518 mpfr_set_str (y, "920657e-23", 10, MPFR_RNDN); 519 mpfr_set_str_binary (x, "0.10101001110101001100110000101110110111101111001101100E-56"); 520 if (mpfr_cmp (x, y)) 521 { 522 printf ("Error in mpfr_set_str (13):\n"); 523 mpfr_print_binary (x); 524 puts (""); 525 mpfr_print_binary (y); 526 puts (""); 527 mpfr_clear (x); 528 mpfr_clear (y); 529 exit (1); 530 } 531 532 mpfr_set_str (y, "9210917e80", 10, MPFR_RNDN); 533 mpfr_set_str_binary (x, "0.11101101000100011001000110100011111100110000000110010E289"); 534 if (mpfr_cmp (x, y)) 535 { 536 printf ("Error in mpfr_set_str (14):\n"); 537 mpfr_print_binary (x); 538 puts (""); 539 mpfr_print_binary (y); 540 puts (""); 541 mpfr_clear (x); 542 mpfr_clear (y); 543 exit (1); 544 } 545 546 mpfr_set_str (y, "87575437e-309", 10, MPFR_RNDN); 547 mpfr_set_str_binary (x, "0.11110000001110011001000000110000000100000010101101100E-1000"); 548 if (mpfr_cmp (x, y)) 549 { 550 printf ("Error in mpfr_set_str (15):\n"); 551 mpfr_print_binary (x); 552 puts (""); 553 mpfr_print_binary (y); 554 puts (""); 555 mpfr_clear (x); 556 mpfr_clear (y); 557 exit (1); 558 } 559 560 mpfr_set_str (y, "245540327e122", 10, MPFR_RNDN); 561 mpfr_set_str_binary (x, "0.10001101101100010001100011110000110001100010111001011E434"); 562 if (mpfr_cmp (x, y)) 563 { 564 printf ("Error in mpfr_set_str (16):\n"); 565 mpfr_print_binary (x); 566 puts (""); 567 mpfr_print_binary (y); 568 puts (""); 569 mpfr_clear (x); 570 mpfr_clear (y); 571 exit (1); 572 } 573 574 mpfr_set_str (y, "491080654e122", 10, MPFR_RNDN); 575 mpfr_set_str_binary (x, "0.10001101101100010001100011110000110001100010111001011E435"); 576 if (mpfr_cmp (x, y)) 577 { 578 printf ("Error in mpfr_set_str (17):\n"); 579 mpfr_print_binary (x); 580 puts (""); 581 mpfr_print_binary (y); 582 puts (""); 583 mpfr_clear (x); 584 mpfr_clear (y); 585 exit (1); 586 } 587 588 mpfr_set_str (y, "83356057653e193", 10, MPFR_RNDN); 589 mpfr_set_str_binary (x, "0.10101010001001110011011011010111011100010101000011000E678"); 590 if (mpfr_cmp (x, y)) 591 { 592 printf ("Error in mpfr_set_str (18):\n"); 593 mpfr_print_binary (x); 594 puts (""); 595 mpfr_print_binary (y); 596 puts (""); 597 mpfr_clear (x); 598 mpfr_clear (y); 599 exit (1); 600 } 601 602 CHECK53(y, "83356057653e193", MPFR_RNDN, x, 603 "0.10101010001001110011011011010111011100010101000011000E678", 604 18); 605 606 CHECK53(y, "619534293513e124", MPFR_RNDN, x, 607 "0.10001000011000010000000110000001111111110000011110001e452", 608 19); 609 610 CHECK53(y, "3142213164987e-294", MPFR_RNDN, x, 611 "0.11101001101000000100111011111101111001010001001101111e-935", 612 20); 613 614 CHECK53(y, "36167929443327e-159", MPFR_RNDN, x, 615 "0.11100111001110111110000101011001100110010100011111100e-483", 616 21); 617 618 CHECK53(y, "904198236083175e-161", MPFR_RNDN, x, 619 "0.11100111001110111110000101011001100110010100011111100e-485", 620 22); 621 622 CHECK53(y, "3743626360493413e-165", MPFR_RNDN, x, 623 "0.11000100000100011101001010111101011011011111011111001e-496", 624 23); 625 626 CHECK53(y, "94080055902682397e-242", MPFR_RNDN, x, 627 "0.10110010010011000000111100011100111100110011011001010e-747", 628 24); 629 630 CHECK53(y, "7e-303", MPFR_RNDD, x, 631 "0.10011001100111001000100110001110001000110111110001011e-1003", 632 25); 633 CHECK53(y, "7e-303", MPFR_RNDU, x, 634 "0.10011001100111001000100110001110001000110111110001100e-1003", 635 26); 636 637 CHECK53(y, "93e-234", MPFR_RNDD, x, 638 "0.10010011110110010111001001111001000010000000001110101E-770", 639 27); 640 CHECK53(y, "93e-234", MPFR_RNDU, x, 641 "0.10010011110110010111001001111001000010000000001110110E-770", 642 28); 643 644 CHECK53(y, "755e174", MPFR_RNDD, x, 645 "0.10111110110010011000110010011111101111000111111000101E588", 646 29); 647 CHECK53(y, "755e174", MPFR_RNDU, x, 648 "0.10111110110010011000110010011111101111000111111000110E588", 649 30); 650 651 CHECK53(y, "8699e-276", MPFR_RNDD, x, 652 "0.10010110100101101111100100100011011101100110100101100E-903", 653 31); 654 CHECK53(y, "8699e-276", MPFR_RNDU, x, 655 "0.10010110100101101111100100100011011101100110100101101E-903", 656 32); 657 658 CHECK53(y, "82081e41", MPFR_RNDD, x, 659 "0.10111000000010000010111011111001111010100011111001011E153", 660 33); 661 CHECK53(y, "82081e41", MPFR_RNDU, x, 662 "0.10111000000010000010111011111001111010100011111001100E153", 663 34); 664 665 CHECK53(y, "584169e229", MPFR_RNDD, x, 666 "0.11101011001010111000001011001110111000111100110101010E780", 667 35); 668 CHECK53(y, "584169e229", MPFR_RNDU, x, 669 "0.11101011001010111000001011001110111000111100110101011E780", 670 36); 671 672 CHECK53(y, "5783893e-128", MPFR_RNDD, x, 673 "0.10011000111100000110011110000101100111110011101110100E-402", 674 37); 675 CHECK53(y, "5783893e-128", MPFR_RNDU, x, 676 "0.10011000111100000110011110000101100111110011101110101E-402", 677 38); 678 679 CHECK53(y, "87575437e-310", MPFR_RNDD, x, 680 "0.11000000001011100000110011110011010000000010001010110E-1003", 681 39); 682 CHECK53(y, "87575437e-310", MPFR_RNDU, x, 683 "0.11000000001011100000110011110011010000000010001010111E-1003", 684 40); 685 686 CHECK53(y, "245540327e121", MPFR_RNDD, x, 687 "0.11100010101101001111010010110100011100000100101000100E430", 688 41); 689 CHECK53(y, "245540327e121", MPFR_RNDU, x, 690 "0.11100010101101001111010010110100011100000100101000101E430", 691 42); 692 693 CHECK53(y, "9078555839e-109", MPFR_RNDD, x, 694 "0.11111110001010111010110000110011100110001010011101101E-329", 695 43); 696 CHECK53(y, "9078555839e-109", MPFR_RNDU, x, 697 "0.11111110001010111010110000110011100110001010011101110E-329", 698 44); 699 700 CHECK53(y, "42333842451e201", MPFR_RNDD, x, 701 "0.10000000110001001101000100110110111110101011101011111E704", 702 45); 703 CHECK53(y, "42333842451e201", MPFR_RNDU, x, 704 "0.10000000110001001101000100110110111110101011101100000E704", 705 46); 706 707 CHECK53(y, "778380362293e218", MPFR_RNDD, x, 708 "0.11001101010111000001001100001100110010000001010010010E764", 709 47); 710 CHECK53(y, "778380362293e218", MPFR_RNDU, x, 711 "0.11001101010111000001001100001100110010000001010010011E764", 712 48); 713 714 CHECK53(y, "7812878489261e-179", MPFR_RNDD, x, 715 "0.10010011011011010111001111011101111101101101001110100E-551", 716 49); 717 CHECK53(y, "7812878489261e-179", MPFR_RNDU, x, 718 "0.10010011011011010111001111011101111101101101001110101E-551", 719 50); 720 721 CHECK53(y, "77003665618895e-73", MPFR_RNDD, x, 722 "0.11000101111110111111001111111101001101111000000101001E-196", 723 51); 724 CHECK53(y, "77003665618895e-73", MPFR_RNDU, x, 725 "0.11000101111110111111001111111101001101111000000101010E-196", 726 52); 727 728 CHECK53(y, "834735494917063e-300", MPFR_RNDD, x, 729 "0.11111110001101100001001101111100010011001110111010001E-947", 730 53); 731 CHECK53(y, "834735494917063e-300", MPFR_RNDU, x, 732 "0.11111110001101100001001101111100010011001110111010010E-947", 733 54); 734 735 CHECK53(y, "6182410494241627e-119", MPFR_RNDD, x, 736 "0.10001101110010110010001011000010001000101110100000111E-342", 737 55); 738 CHECK53(y, "6182410494241627e-119", MPFR_RNDU, x, 739 "0.10001101110010110010001011000010001000101110100001000E-342", 740 56); 741 742 CHECK53(y, "26153245263757307e49", MPFR_RNDD, x, 743 "0.10011110111100000000001011011110101100010000011011110E218", 744 57); 745 CHECK53(y, "26153245263757307e49", MPFR_RNDU, x, 746 "0.10011110111100000000001011011110101100010000011011111E218", 747 58); 748 749 /* to check this problem : I convert limb (10--0 or 101--1) into base b 750 with more than mp_bits_per_limb digits, 751 so when convert into base 2 I should have 752 the limb that I have choose */ 753 /* this use mpfr_get_str */ 754 { 755 size_t nb_digit = mp_bits_per_limb; 756 mp_limb_t check_limb[2] = {MPFR_LIMB_HIGHBIT, ~(MPFR_LIMB_HIGHBIT >> 1)}; 757 int base[3] = {10, 16, 19}; 758 mpfr_rnd_t rnd[3] = {MPFR_RNDU, MPFR_RNDN, MPFR_RNDD}; 759 int cbase, climb, crnd; 760 char *str; 761 762 mpfr_set_prec (x, mp_bits_per_limb); /* x and y have only one limb */ 763 mpfr_set_prec (y, mp_bits_per_limb); 764 765 str = (char*) (*__gmp_allocate_func) (N + 20); 766 767 mpfr_set_ui (x, 1, MPFR_RNDN); /* ensures that x is not NaN or Inf */ 768 for (; nb_digit < N; nb_digit *= 10) 769 for (cbase = 0; cbase < 3; cbase++) 770 for (climb = 0; climb < 2; climb++) 771 for (crnd = 0; crnd < 3; crnd++) 772 { 773 char *str1; 774 mpfr_exp_t exp; 775 776 *(MPFR_MANT(x)) = check_limb[climb]; 777 MPFR_EXP(x) = 0; 778 779 mpfr_get_str (str + 2, &exp, base[cbase], 780 nb_digit, x, rnd[crnd]); 781 str[0] = '-'; 782 str[(str[2] == '-')] = '0'; 783 str[(str[2] == '-') + 1] = '.'; 784 785 for (str1 = str; *str1 != 0; str1++) 786 ; 787 sprintf (str1, "@%i", (int) exp); 788 789 mpfr_set_str (y, str, base[cbase], rnd[2 - crnd]); 790 791 if (mpfr_cmp (x, y) != 0) 792 { 793 printf ("Error in mpfr_set_str for nb_digit=%u, base=%d, " 794 "rnd=%s:\n", (unsigned int) nb_digit, base[cbase], 795 mpfr_print_rnd_mode (rnd[crnd])); 796 printf ("instead of: "); 797 mpfr_print_binary (x); 798 puts (""); 799 printf ("return : "); 800 mpfr_print_binary (y); 801 puts (""); 802 exit (1); 803 } 804 } 805 806 (*__gmp_free_func) (str, N + 20); 807 } 808 809 /* end of tests added by Alain Delplanque */ 810 811 /* check that flags are correctly cleared */ 812 mpfr_set_nan (x); 813 mpfr_set_str (x, "+0.0", 10, MPFR_RNDN); 814 if (!mpfr_number_p(x) || mpfr_cmp_ui (x, 0) != 0 || mpfr_sgn (x) < 0) 815 { 816 printf ("x <- +0.0 failed after x=NaN\n"); 817 exit (1); 818 } 819 mpfr_set_str (x, "-0.0", 10, MPFR_RNDN); 820 if (!mpfr_number_p(x) || mpfr_cmp_ui (x, 0) != 0 || mpfr_sgn (x) > 0) 821 { 822 printf ("x <- -0.0 failed after x=NaN\n"); 823 exit (1); 824 } 825 826 /* check invalid input */ 827 ret = mpfr_set_str (x, "1E10toto", 10, MPFR_RNDN); 828 MPFR_ASSERTN (ret == -1); 829 ret = mpfr_set_str (x, "1p10toto", 16, MPFR_RNDN); 830 MPFR_ASSERTN (ret == -1); 831 ret = mpfr_set_str (x, "", 16, MPFR_RNDN); 832 MPFR_ASSERTN (ret == -1); 833 ret = mpfr_set_str (x, "+", 16, MPFR_RNDN); 834 MPFR_ASSERTN (ret == -1); 835 ret = mpfr_set_str (x, "-", 16, MPFR_RNDN); 836 MPFR_ASSERTN (ret == -1); 837 ret = mpfr_set_str (x, "this_is_an_invalid_number_in_base_36", 36, MPFR_RNDN); 838 MPFR_ASSERTN (ret == -1); 839 ret = mpfr_set_str (x, "1.2.3", 10, MPFR_RNDN); 840 MPFR_ASSERTN (ret == -1); 841 mpfr_set_prec (x, 135); 842 ret = mpfr_set_str (x, "thisisavalidnumberinbase36", 36, MPFR_RNDN); 843 mpfr_set_prec (y, 135); 844 mpfr_set_str (y, "23833565676460972739462619524519814462546", 10, MPFR_RNDN); 845 MPFR_ASSERTN (mpfr_cmp (x, y) == 0 && ret == 0); 846 847 /* coverage test for set_str_binary */ 848 mpfr_set_str_binary (x, "NaN"); 849 MPFR_ASSERTN(mpfr_nan_p (x)); 850 mpfr_set_str_binary (x, "Inf"); 851 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 852 mpfr_set_str_binary (x, "+Inf"); 853 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); 854 mpfr_set_str_binary (x, "-Inf"); 855 MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) < 0); 856 mpfr_set_prec (x, 3); 857 mpfr_set_str_binary (x, "0.01E2"); 858 MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0); 859 mpfr_set_str_binary (x, "-0.01E2"); 860 MPFR_ASSERTN(mpfr_cmp_si (x, -1) == 0); 861 862 mpfr_clear (x); 863 mpfr_clear (y); 864 865 check_underflow (); 866 bug20081028 (); 867 868 tests_end_mpfr (); 869 return 0; 870 } 871