1 /* mpfr_strtofr -- set a floating-point number from a string
3 Copyright 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. */
23 #include <stdlib.h> /* For strtol */
24 #include <ctype.h> /* For isspace */
26 #define MPFR_NEED_LONGLONG_H
27 #include "mpfr-impl.h"
29 #define MPFR_MAX_BASE 62
31 struct parsed_string {
32 int negative; /* non-zero iff the number is negative */
33 int base; /* base of the string */
34 unsigned char *mantissa; /* raw significand (without any point) */
35 unsigned char *mant; /* stripped significand (without starting and
36 ending zeroes). This points inside the area
37 allocated for the mantissa field. */
38 size_t prec; /* length of mant (zero for +/-0) */
39 size_t alloc; /* allocation size of mantissa */
40 mpfr_exp_t exp_base; /* number of digits before the point */
41 mpfr_exp_t exp_bin; /* exponent in case base=2 or 16, and the pxxx
42 format is used (i.e., exponent is given in
46 /* This table has been generated by the following program.
47 For 2 <= b <= MPFR_MAX_BASE,
48 RedInvLog2Table[b-2][0] / RedInvLog2Table[b-2][1]
49 is an upper approximation of log(2)/log(b).
51 static const unsigned long RedInvLog2Table[MPFR_MAX_BASE-1][2] = {
117 unsigned long tab[N];
121 int overflow = 0, base_overflow;
128 for (base = 2 ; base < 63 ; base ++)
130 mpfr_set_ui (x, base, MPFR_RNDN);
131 mpfr_log2 (x, x, MPFR_RNDN);
132 mpfr_ui_div (x, 1, x, MPFR_RNDN);
133 printf ("Base: %d x=%e ", base, mpfr_get_d1 (x));
134 for (i = 0 ; i < N ; i++)
137 tab[i] = mpfr_get_ui (y, MPFR_RNDN);
138 mpfr_sub (x, x, y, MPFR_RNDN);
139 mpfr_ui_div (x, 1, x, MPFR_RNDN);
141 for (i = N-1 ; i >= 0 ; i--)
144 mpq_set_ui (q1, tab[i], 1);
145 for (i = i-1 ; i >= 0 ; i--)
148 mpq_set_ui (q2, tab[i], 1);
149 mpq_add (q1, q1, q2);
151 printf("Approx: ", base);
152 mpq_out_str (stdout, 10, q1);
153 printf (" = %e\n", mpq_get_d (q1) );
154 fprintf (stderr, "{");
155 mpz_out_str (stderr, 10, mpq_numref (q1));
156 fprintf (stderr, "UL, ");
157 mpz_out_str (stderr, 10, mpq_denref (q1));
158 fprintf (stderr, "UL},\n");
159 if (mpz_cmp_ui (mpq_numref (q1), 1<<16-1) >= 0
160 || mpz_cmp_ui (mpq_denref (q1), 1<<16-1) >= 0)
161 overflow = 1, base_overflow = base;
169 printf ("OVERFLOW for base =%d!\n", base_overflow);
174 /* Compatible with any locale, but one still assumes that 'a', 'b', 'c',
175 ..., 'z', and 'A', 'B', 'C', ..., 'Z' are consecutive values (like
176 in any ASCII-based character set). */
178 digit_value_in_base (int c, int base)
182 MPFR_ASSERTD (base > 0 && base <= MPFR_MAX_BASE);
184 if (c >= '0' && c <= '9')
186 else if (c >= 'a' && c <= 'z')
187 digit = (base >= 37) ? c - 'a' + 36 : c - 'a' + 10;
188 else if (c >= 'A' && c <= 'Z')
189 digit = c - 'A' + 10;
193 return MPFR_LIKELY (digit < base) ? digit : -1;
196 /* Compatible with any locale, but one still assumes that 'a', 'b', 'c',
197 ..., 'z', and 'A', 'B', 'C', ..., 'Z' are consecutive values (like
198 in any ASCII-based character set). */
199 /* TODO: support EBCDIC. */
201 fast_casecmp (const char *s1, const char *s2)
203 unsigned char c1, c2;
207 c2 = *(const unsigned char *) s2++;
210 c1 = *(const unsigned char *) s1++;
211 if (c1 >= 'A' && c1 <= 'Z')
218 /* Parse a string and fill pstr.
219 Return the advanced ptr too.
221 -1 if invalid string,
222 0 if special string (like nan),
223 1 if the string is ok.
225 So it doesn't return the ternary value
226 BUT if it returns 0 (NAN or INF), the ternary value is also '0'
227 (ie NAN and INF are exact) */
229 parse_string (mpfr_t x, struct parsed_string *pstr,
230 const char **string, int base)
232 const char *str = *string;
235 int res = -1; /* Invalid input return value */
236 const char *prefix_str;
239 decimal_point = (unsigned char) MPFR_DECIMAL_POINT;
242 pstr->mantissa = NULL;
244 /* Optional leading whitespace */
245 while (isspace((unsigned char) *str)) str++;
247 /* An optional sign `+' or `-' */
248 pstr->negative = (*str == '-');
249 if (*str == '-' || *str == '+')
252 /* Can be case-insensitive NAN */
253 if (fast_casecmp (str, "@nan@") == 0)
258 if (base <= 16 && fast_casecmp (str, "nan") == 0)
262 /* Check for "(dummychars)" */
266 for (s = str+1 ; *s != ')' ; s++)
267 if (!(*s >= 'A' && *s <= 'Z')
268 && !(*s >= 'a' && *s <= 'z')
269 && !(*s >= '0' && *s <= '9')
277 /* MPFR_RET_NAN not used as the return value isn't a ternary value */
278 __gmpfr_flags |= MPFR_FLAGS_NAN;
282 /* Can be case-insensitive INF */
283 if (fast_casecmp (str, "@inf@") == 0)
288 if (base <= 16 && fast_casecmp (str, "infinity") == 0)
293 if (base <= 16 && fast_casecmp (str, "inf") == 0)
299 (pstr->negative) ? MPFR_SET_NEG (x) : MPFR_SET_POS (x);
303 /* If base=0 or 16, it may include '0x' prefix */
305 if ((base == 0 || base == 16) && str[0]=='0'
306 && (str[1]=='x' || str[1] == 'X'))
312 /* If base=0 or 2, it may include '0b' prefix */
313 if ((base == 0 || base == 2) && str[0]=='0'
314 && (str[1]=='b' || str[1] == 'B'))
320 /* Else if base=0, we assume decimal base */
326 pstr->alloc = (size_t) strlen (str) + 1;
327 pstr->mantissa = (unsigned char*) (*__gmp_allocate_func) (pstr->alloc);
329 /* Read mantissa digits */
331 mant = pstr->mantissa;
336 for (;;) /* Loop until an invalid character is read */
338 int c = (unsigned char) *str++;
339 /* The cast to unsigned char is needed because of digit_value_in_base;
340 decimal_point uses this convention too. */
341 if (c == '.' || c == decimal_point)
343 if (MPFR_UNLIKELY(point)) /* Second '.': stop parsing */
348 c = digit_value_in_base (c, base);
351 MPFR_ASSERTN (c >= 0); /* c is representable in an unsigned char */
352 *mant++ = (unsigned char) c;
356 str--; /* The last read character was invalid */
358 /* Update the # of char in the mantissa */
359 pstr->prec = mant - pstr->mantissa;
360 /* Check if there are no characters in the mantissa (Invalid argument) */
363 /* Check if there was a prefix (in such a case, we have to read
364 again the mantissa without skipping the prefix)
365 The allocated mantissa is still big enough since we will
366 read only 0, and we alloc one more char than needed.
367 FIXME: Not really friendly. Maybe cleaner code? */
368 if (prefix_str != NULL)
379 MPFR_ASSERTD (pstr->exp_base >= 0);
381 /* an optional exponent (e or E, p or P, @) */
382 if ( (*str == '@' || (base <= 10 && (*str == 'e' || *str == 'E')))
383 && (!isspace((unsigned char) str[1])) )
386 /* the exponent digits are kept in ASCII */
388 long read_exp = strtol (str + 1, &endptr, 10);
392 read_exp < MPFR_EXP_MIN ? (str = endptr, MPFR_EXP_MIN) :
393 read_exp > MPFR_EXP_MAX ? (str = endptr, MPFR_EXP_MAX) :
394 (mpfr_exp_t) read_exp;
395 MPFR_SADD_OVERFLOW (sum, sum, pstr->exp_base,
396 mpfr_exp_t, mpfr_uexp_t,
397 MPFR_EXP_MIN, MPFR_EXP_MAX,
399 /* Since exp_base was positive, read_exp + exp_base can't
400 do a negative overflow. */
401 MPFR_ASSERTD (res != 3);
402 pstr->exp_base = sum;
404 else if ((base == 2 || base == 16)
405 && (*str == 'p' || *str == 'P')
406 && (!isspace((unsigned char) str[1])))
409 long read_exp = strtol (str + 1, &endptr, 10);
413 read_exp < MPFR_EXP_MIN ? (str = endptr, MPFR_EXP_MIN) :
414 read_exp > MPFR_EXP_MAX ? (str = endptr, MPFR_EXP_MAX) :
415 (mpfr_exp_t) read_exp;
418 /* Remove 0's at the beginning and end of mantissa[0..prec-1] */
419 mant = pstr->mantissa;
420 for ( ; (pstr->prec > 0) && (*mant == 0) ; mant++, pstr->prec--)
422 for ( ; (pstr->prec > 0) && (mant[pstr->prec - 1] == 0); pstr->prec--);
438 if (pstr->mantissa != NULL && res != 1)
439 (*__gmp_free_func) (pstr->mantissa, pstr->alloc);
443 /* Transform a parsed string to a mpfr_t according to the rounding mode
444 and the precision of x.
445 Returns the ternary value. */
447 parsed_string_to_mpfr (mpfr_t x, struct parsed_string *pstr, mpfr_rnd_t rnd)
451 mpfr_exp_t ysize_bits;
452 mp_limb_t *y, *result;
455 mp_size_t ysize, real_ysize;
457 MPFR_ZIV_DECL (loop);
458 MPFR_TMP_DECL (marker);
460 /* initialize the working precision */
461 prec = MPFR_PREC (x) + MPFR_INT_CEIL_LOG2 (MPFR_PREC (x));
463 /* compute the value y of the leading characters as long as rounding is not
465 MPFR_TMP_MARK(marker);
466 MPFR_ZIV_INIT (loop, prec);
469 /* Set y to the value of the ~prec most significant bits of pstr->mant
470 (as long as we guarantee correct rounding, we don't need to get
471 exactly prec bits). */
472 ysize = MPFR_PREC2LIMBS (prec);
473 /* prec bits corresponds to ysize limbs */
474 ysize_bits = ysize * GMP_NUMB_BITS;
475 /* and to ysize_bits >= prec > MPFR_PREC (x) bits */
476 y = MPFR_TMP_LIMBS_ALLOC (2 * ysize + 1);
477 y += ysize; /* y has (ysize+1) allocated limbs */
479 /* pstr_size is the number of characters we read in pstr->mant
480 to have at least ysize full limbs.
481 We must have base^(pstr_size-1) >= (2^(GMP_NUMB_BITS))^ysize
482 (in the worst case, the first digit is one and all others are zero).
483 i.e., pstr_size >= 1 + ysize*GMP_NUMB_BITS/log2(base)
484 Since ysize ~ prec/GMP_NUMB_BITS and prec < Umax/2 =>
485 ysize*GMP_NUMB_BITS can not overflow.
486 We compute pstr_size = 1 + ceil(ysize_bits * Num / Den)
487 where Num/Den >= 1/log2(base)
488 It is not exactly ceil(1/log2(base)) but could be one more (base 2)
489 Quite ugly since it tries to avoid overflow:
490 let Num = RedInvLog2Table[pstr->base-2][0]
491 and Den = RedInvLog2Table[pstr->base-2][1],
492 and ysize_bits = a*Den+b,
493 then ysize_bits * Num/Den = a*Num + (b * Num)/Den,
494 thus ceil(ysize_bits * Num/Den) = a*Num + floor(b * Num + Den - 1)/Den
497 unsigned long Num = RedInvLog2Table[pstr->base-2][0];
498 unsigned long Den = RedInvLog2Table[pstr->base-2][1];
499 pstr_size = ((ysize_bits / Den) * Num)
500 + (((ysize_bits % Den) * Num + Den - 1) / Den)
504 /* since pstr_size corresponds to at least ysize_bits full bits,
505 and ysize_bits > prec, the weight of the neglected part of
506 pstr->mant (if any) is < ulp(y) < ulp(x) */
508 /* if the number of wanted characters is more than what we have in
509 pstr->mant, round it down */
510 if (pstr_size >= pstr->prec)
511 pstr_size = pstr->prec;
512 MPFR_ASSERTD (pstr_size == (mpfr_exp_t) pstr_size);
514 /* convert str into binary: note that pstr->mant is big endian,
515 thus no offset is needed */
516 real_ysize = mpn_set_str (y, pstr->mant, pstr_size, pstr->base);
517 MPFR_ASSERTD (real_ysize <= ysize+1);
519 /* normalize y: warning we can even get ysize+1 limbs! */
520 MPFR_ASSERTD (y[real_ysize - 1] != 0); /* mpn_set_str guarantees this */
521 count_leading_zeros (count, y[real_ysize - 1]);
522 /* exact means that the number of limbs of the output of mpn_set_str
523 is less or equal to ysize */
524 exact = real_ysize <= ysize;
525 if (exact) /* shift y to the left in that case y should be exact */
527 /* we have enough limbs to store {y, real_ysize} */
528 /* shift {y, num_limb} for count bits to the left */
530 mpn_lshift (y + ysize - real_ysize, y, real_ysize, count);
531 if (real_ysize != ysize)
534 MPN_COPY_DECR (y + ysize - real_ysize, y, real_ysize);
535 MPN_ZERO (y, ysize - real_ysize);
537 /* for each bit shift decrease exponent of y */
538 /* (This should not overflow) */
539 exp = - ((ysize - real_ysize) * GMP_NUMB_BITS + count);
541 else /* shift y to the right, by doing this we might lose some
542 bits from the result of mpn_set_str (in addition to the
543 characters neglected from pstr->mant) */
545 /* shift {y, num_limb} for (GMP_NUMB_BITS - count) bits
546 to the right. FIXME: can we prove that count cannot be zero here,
547 since mpn_rshift does not accept a shift of GMP_NUMB_BITS? */
548 MPFR_ASSERTD (count != 0);
549 exact = mpn_rshift (y, y, real_ysize, GMP_NUMB_BITS - count) ==
551 /* for each bit shift increase exponent of y */
552 exp = GMP_NUMB_BITS - count;
555 /* compute base^(exp_base - pstr_size) on n limbs */
556 if (IS_POW2 (pstr->base))
558 /* Base: 2, 4, 8, 16, 32 */
562 count_leading_zeros (pow2, (mp_limb_t) pstr->base);
563 pow2 = GMP_NUMB_BITS - pow2 - 1; /* base = 2^pow2 */
564 MPFR_ASSERTD (0 < pow2 && pow2 <= 5);
565 /* exp += pow2 * (pstr->exp_base - pstr_size) + pstr->exp_bin
566 with overflow checking
567 and check that we can add/substract 2 to exp without overflow */
568 MPFR_SADD_OVERFLOW (tmp, pstr->exp_base, -(mpfr_exp_t) pstr_size,
569 mpfr_exp_t, mpfr_uexp_t,
570 MPFR_EXP_MIN, MPFR_EXP_MAX,
571 goto overflow, goto underflow);
572 /* On some FreeBsd/Alpha, LONG_MIN/1 produced an exception
573 so we used to check for this before doing the division.
574 Since this bug is closed now (Nov 26, 2009), we remove
575 that check (http://www.freebsd.org/cgi/query-pr.cgi?pr=72024) */
576 if (tmp > 0 && MPFR_EXP_MAX / pow2 <= tmp)
578 else if (tmp < 0 && MPFR_EXP_MIN / pow2 >= tmp)
581 MPFR_SADD_OVERFLOW (tmp, tmp, pstr->exp_bin,
582 mpfr_exp_t, mpfr_uexp_t,
583 MPFR_EXP_MIN, MPFR_EXP_MAX,
584 goto overflow, goto underflow);
585 MPFR_SADD_OVERFLOW (exp, exp, tmp,
586 mpfr_exp_t, mpfr_uexp_t,
587 MPFR_EXP_MIN+2, MPFR_EXP_MAX-2,
588 goto overflow, goto underflow);
592 /* case non-power-of-two-base, and pstr->exp_base > pstr_size */
593 else if (pstr->exp_base > (mpfr_exp_t) pstr_size)
598 result = MPFR_TMP_LIMBS_ALLOC (2 * ysize + 1);
600 /* z = base^(exp_base-sptr_size) using space allocated at y-ysize */
602 /* NOTE: exp_base-pstr_size can't overflow since pstr_size > 0 */
603 err = mpfr_mpn_exp (z, &exp_z, pstr->base,
604 pstr->exp_base - pstr_size, ysize);
607 exact = exact && (err == -1);
609 /* If exact is non zero, then z equals exactly the value of the
610 pstr_size most significant digits from pstr->mant, i.e., the
611 only difference can come from the neglected pstr->prec-pstr_size
612 least significant digits of pstr->mant.
613 If exact is zero, then z is rounded toward zero with respect
616 /* multiply(y = 0.mant[0]...mant[pr-1])_base by base^(exp-g):
617 since both y and z are rounded toward zero, so is "result" */
618 mpn_mul_n (result, y, z, ysize);
620 /* compute the error on the product */
625 /* compute the exponent of y */
626 /* exp += exp_z + ysize_bits with overflow checking
627 and check that we can add/substract 2 to exp without overflow */
628 MPFR_SADD_OVERFLOW (exp_z, exp_z, ysize_bits,
629 mpfr_exp_t, mpfr_uexp_t,
630 MPFR_EXP_MIN, MPFR_EXP_MAX,
631 goto overflow, goto underflow);
632 MPFR_SADD_OVERFLOW (exp, exp, exp_z,
633 mpfr_exp_t, mpfr_uexp_t,
634 MPFR_EXP_MIN+2, MPFR_EXP_MAX-2,
635 goto overflow, goto underflow);
637 /* normalize result */
638 if (MPFR_LIMB_MSB (result[2 * ysize - 1]) == 0)
640 mp_limb_t *r = result + ysize - 1;
641 mpn_lshift (r, r, ysize + 1, 1);
642 /* Overflow checking not needed */
646 /* if the low ysize limbs of {result, 2*ysize} are all zero,
647 then the result is still "exact" (if it was before) */
648 exact = exact && (mpn_scan1 (result, 0)
649 >= (unsigned long) ysize_bits);
652 /* case exp_base < pstr_size */
653 else if (pstr->exp_base < (mpfr_exp_t) pstr_size)
658 result = MPFR_TMP_LIMBS_ALLOC (3 * ysize + 1);
660 /* set y to y * K^ysize */
661 y = y - ysize; /* we have allocated ysize limbs at y - ysize */
664 /* pstr_size - pstr->exp_base can overflow */
665 MPFR_SADD_OVERFLOW (exp_z, (mpfr_exp_t) pstr_size, -pstr->exp_base,
666 mpfr_exp_t, mpfr_uexp_t,
667 MPFR_EXP_MIN, MPFR_EXP_MAX,
668 goto underflow, goto overflow);
670 /* (z, exp_z) = base^(exp_base-pstr_size) */
671 z = result + 2*ysize + 1;
672 err = mpfr_mpn_exp (z, &exp_z, pstr->base, exp_z, ysize);
673 /* Since we want y/z rounded toward zero, we must get an upper
674 bound of z. If err >= 0, the error on z is bounded by 2^err. */
678 unsigned long h = err / GMP_NUMB_BITS;
679 unsigned long l = err - h * GMP_NUMB_BITS;
681 if (h >= ysize) /* not enough precision in z */
683 cy = mpn_add_1 (z, z, ysize - h, MPFR_LIMB_ONE << l);
684 if (cy != 0) /* the code below requires z on ysize limbs */
687 exact = exact && (err == -1);
689 goto underflow; /* FIXME: Sure? */
694 /* result will be put into result + n, and remainder into result */
695 mpn_tdiv_qr (result + ysize, result, (mp_size_t) 0, y,
696 2 * ysize, z, ysize);
698 /* exp -= exp_z + ysize_bits with overflow checking
699 and check that we can add/substract 2 to exp without overflow */
700 MPFR_SADD_OVERFLOW (exp_z, exp_z, ysize_bits,
701 mpfr_exp_t, mpfr_uexp_t,
702 MPFR_EXP_MIN, MPFR_EXP_MAX,
703 goto underflow, goto overflow);
704 MPFR_SADD_OVERFLOW (exp, exp, -exp_z,
705 mpfr_exp_t, mpfr_uexp_t,
706 MPFR_EXP_MIN+2, MPFR_EXP_MAX-2,
707 goto overflow, goto underflow);
709 /* if the remainder of the division is zero, then the result is
710 still "exact" if it was before */
711 exact = exact && (mpn_popcount (result, ysize) == 0);
713 /* normalize result */
714 if (result[2 * ysize] == MPFR_LIMB_ONE)
716 mp_limb_t *r = result + ysize;
718 exact = exact && ((*r & MPFR_LIMB_ONE) == 0);
719 mpn_rshift (r, r, ysize + 1, 1);
720 /* Overflow Checking not needed */
725 /* case exp_base = pstr_size: no multiplication or division needed */
728 /* base^(exp-pr) = 1 nothing to compute */
733 /* If result is exact, we still have to consider the neglected part
734 of the input string. For a directed rounding, in that case we could
735 still correctly round, since the neglected part is less than
736 one ulp, but that would make the code more complex, and give a
737 speedup for rare cases only. */
738 exact = exact && (pstr_size == pstr->prec);
740 /* at this point, result is an approximation rounded toward zero
741 of the pstr_size most significant digits of pstr->mant, with
742 equality in case exact is non-zero. */
744 /* test if rounding is possible, and if so exit the loop */
745 if (exact || mpfr_can_round_raw (result, ysize,
746 (pstr->negative) ? -1 : 1,
747 ysize_bits - err - 1,
748 MPFR_RNDN, rnd, MPFR_PREC(x)))
752 /* update the prec for next loop */
753 MPFR_ZIV_NEXT (loop, prec);
755 MPFR_ZIV_FREE (loop);
758 if (mpfr_round_raw (MPFR_MANT (x), result,
760 pstr->negative, MPFR_PREC(x), rnd, &res ))
762 /* overflow when rounding y */
763 MPFR_MANT (x)[MPFR_LIMB_SIZE (x) - 1] = MPFR_LIMB_HIGHBIT;
764 /* Overflow Checking not needed */
768 if (res == 0) /* fix ternary value */
770 exact = exact && (pstr_size == pstr->prec);
772 res = (pstr->negative) ? 1 : -1;
775 /* Set sign of x before exp since check_range needs a valid sign */
776 (pstr->negative) ? MPFR_SET_NEG (x) : MPFR_SET_POS (x);
778 /* DO NOT USE MPFR_SET_EXP. The exp may be out of range! */
779 MPFR_SADD_OVERFLOW (exp, exp, ysize_bits,
780 mpfr_exp_t, mpfr_uexp_t,
781 MPFR_EXP_MIN, MPFR_EXP_MAX,
782 goto overflow, goto underflow);
784 res = mpfr_check_range (x, res, rnd);
788 /* This is called when there is a huge overflow
789 (Real expo < MPFR_EXP_MIN << __gmpfr_emin */
790 if (rnd == MPFR_RNDN)
792 res = mpfr_underflow (x, rnd, (pstr->negative) ? -1 : 1);
796 res = mpfr_overflow (x, rnd, (pstr->negative) ? -1 : 1);
799 MPFR_TMP_FREE (marker);
804 free_parsed_string (struct parsed_string *pstr)
806 (*__gmp_free_func) (pstr->mantissa, pstr->alloc);
810 mpfr_strtofr (mpfr_t x, const char *string, char **end, int base,
814 struct parsed_string pstr;
816 /* For base <= 36, parsing is case-insensitive. */
817 MPFR_ASSERTN (base == 0 || (base >= 2 && base <= 62));
819 /* If an error occured, it must return 0 */
823 MPFR_ASSERTN (MPFR_MAX_BASE >= 62);
824 res = parse_string (x, &pstr, &string, base);
825 /* If res == 0, then it was exact (NAN or INF),
826 so it is also the ternary value */
827 if (MPFR_UNLIKELY (res == -1)) /* invalid data */
828 res = 0; /* x is set to 0, which is exact, thus ternary value is 0 */
831 res = parsed_string_to_mpfr (x, &pstr, rnd);
832 free_parsed_string (&pstr);
835 res = mpfr_overflow (x, rnd, (pstr.negative) ? -1 : 1);
836 MPFR_ASSERTD (res != 3);
840 /* This is called when there is a huge overflow
841 (Real expo < MPFR_EXP_MIN << __gmpfr_emin */
842 if (rnd == MPFR_RNDN)
844 res = mpfr_underflow (x, rnd, (pstr.negative) ? -1 : 1);
849 *end = (char *) string;