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