[BACK]Return to set_str_raw.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr

Annotation of OpenXM_contrib/gmp/mpfr/set_str_raw.c, Revision 1.1.1.1

1.1       maekawa     1: /* mpfr_set_str_raw -- set a floating-point number from a binary string
                      2:
                      3: Copyright (C) 1999 PolKA project, Inria Lorraine and Loria
                      4:
                      5: This file is part of the MPFR Library.
                      6:
                      7: The MPFR Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Library General Public License as published by
                      9: the Free Software Foundation; either version 2 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The MPFR Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Library General Public License
                     18: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #ifdef HAS_STRING_H
                     25: #include <string.h>
                     26: #else
                     27: #include <strings.h>
                     28: #endif
                     29: #include "gmp.h"
                     30: #include "gmp-impl.h"
                     31: #include "longlong.h"
                     32: #include "mpfr.h"
                     33:
                     34: /* Currently the number should be of the form +/- xxxx.xxxxxxEyy, with
                     35:    decimal exponent. The mantissa of x is supposed to be large enough
                     36:    to hold all the bits of str. */
                     37:
                     38: void
                     39: #if __STDC__
                     40: mpfr_set_str_raw(mpfr_ptr x, char *str)
                     41: #else
                     42: mpfr_set_str_raw(x, str)
                     43:      mpfr_ptr x;
                     44:      char *str;
                     45: #endif
                     46: {
                     47:   char *str2, *str0, negative = 0;
                     48:   unsigned long j, l, k = 0, xsize, cnt; mp_limb_t *xp;
                     49:   long expn = 0, e; char *endstr2;
                     50:
                     51:   xp = x -> _mp_d;
                     52:   xsize = 1 + (PREC(x)-1)/BITS_PER_MP_LIMB;
                     53:   str0 = str2 = (char *) malloc((strlen(str)+1)*sizeof(char));
                     54:
                     55:   if (*str == '-') { negative = 1; str++; }
                     56:   else if (*str == '+') str++;
                     57:
                     58:   while (*str == '0') { str++; }
                     59:
                     60:   while (*str == '0' || *str == '1')
                     61:     { *(str2++) = *(str++); k++; }
                     62:
                     63:   if (*str == '.')
                     64:     {
                     65:       str++;
                     66:       while (*str == '0' || *str == '1')
                     67:        { *(str2++) = *(str++); }
                     68:
                     69:       if (*str == '[') { while (*str != ']') str++; }
                     70:     }
                     71:
                     72:   if (*str == '[') { while (*str != ']') str++; }
                     73:
                     74:   if (*str == 'e' || *str == 'E')
                     75:     {
                     76:       e = atol(++str); /* signed exponent after 'e' or 'E' */
                     77:       expn = k + e;
                     78:       if (expn < e)
                     79:          fprintf(stderr, "Warning: overflow in exponent in Str -> mpfr\n");
                     80:     }
                     81:   else expn=k;
                     82:
                     83:   endstr2 = str2;
                     84:   *str2 = (char) 0; /* end of string */
                     85:   l = (strlen(str0) - 1) / BITS_PER_MP_LIMB + 1; str2 = str0;
                     86:
                     87:   /* str2[0]..endstr2[-1] contains the mantissa */
                     88:   for (k = 1; k <= l; k++)
                     89:     {
                     90:       j = 0;
                     91:       xp[xsize - k] = 0;
                     92:       while (str2<endstr2 && j < BITS_PER_MP_LIMB)
                     93:        {
                     94:          xp[xsize - k] = (xp[xsize - k] << 1) + (*str2 - '0');
                     95:          str2++; j++;
                     96:        }
                     97:       xp[xsize - k] <<= (BITS_PER_MP_LIMB - j);
                     98:     }
                     99:
                    100:   for (; k <= xsize; k++) { xp[xsize - k] = 0; }
                    101:
                    102:   count_leading_zeros(cnt, xp[xsize - 1]);
                    103:   if (cnt) mpn_lshift(xp, xp, xsize, cnt);
                    104:
                    105:   x -> _mp_exp = expn - cnt;
                    106:   x -> _mp_size = xsize; if (negative) CHANGE_SIGN(x);
                    107:
                    108:   free(str0);
                    109:
                    110:   /* May change to take into account : syntax errors, overflow in exponent,
                    111:      string truncated because of size of x */
                    112: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>