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

Annotation of OpenXM_contrib/gmp/mpfr/print_raw.c, Revision 1.1.1.2

1.1.1.2 ! ohara       1: /* mpfr_print_binary -- print the internal binary representation of a
1.1       maekawa     2:                      floating-point number
                      3:
1.1.1.2 ! ohara       4: Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
1.1       maekawa     5:
                      6: This file is part of the MPFR Library.
                      7:
                      8: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara       9: it under the terms of the GNU Lesser General Public License as published by
        !            10: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    11: option) any later version.
                     12:
                     13: The MPFR Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara      15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! ohara      18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    19: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA. */
                     22:
                     23: #include <stdio.h>
                     24: #include "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "mpfr.h"
1.1.1.2 ! ohara      27: #include "mpfr-impl.h"
1.1       maekawa    28:
1.1.1.2 ! ohara      29: static void mpfr_get_str_raw _PROTO ((char *, mpfr_srcptr));
        !            30:
        !            31: static void
        !            32: mpfr_get_str_raw (char *digit_ptr, mpfr_srcptr x)
1.1       maekawa    33: {
                     34:   mp_limb_t *mx, wd, t; long ex, sx, k, l, p;
                     35:
1.1.1.2 ! ohara      36:   mx = MPFR_MANT(x);
        !            37:   ex = MPFR_EXP(x);
        !            38:   p = MPFR_PREC(x);
1.1       maekawa    39:
1.1.1.2 ! ohara      40:   if (MPFR_SIGN(x) < 0) { *digit_ptr = '-'; digit_ptr++; }
1.1       maekawa    41:   sprintf(digit_ptr, "0."); digit_ptr += 2;
                     42:
1.1.1.2 ! ohara      43:   sx = 1+(p-1)/BITS_PER_MP_LIMB; /* number of significant limbs */
1.1       maekawa    44:   for (k = sx - 1; k >= 0 ; k--)
                     45:     {
                     46:       wd = mx[k];
1.1.1.2 ! ohara      47:       t = GMP_LIMB_HIGHBIT;
1.1       maekawa    48:       for (l = BITS_PER_MP_LIMB - 1; l>=0; l--)
                     49:        {
                     50:          if (wd & t)
                     51:            { *digit_ptr = '1'; digit_ptr++; }
                     52:          else
                     53:            { *digit_ptr = '0'; digit_ptr++; }
                     54:          t >>= 1;
                     55:          if (--p==0) { *digit_ptr = '['; digit_ptr++; }
                     56:        }
                     57:     }
                     58:   sprintf(digit_ptr, "]E%ld", ex);
                     59: }
                     60:
                     61: void
1.1.1.2 ! ohara      62: mpfr_print_binary (mpfr_srcptr x)
1.1       maekawa    63: {
1.1.1.2 ! ohara      64:   char *str;
        !            65:   unsigned long alloc_size;
1.1       maekawa    66:
1.1.1.2 ! ohara      67:   if (MPFR_IS_NAN(x)) printf("NaN");
        !            68:   else if (MPFR_IS_INF(x)) {
        !            69:     if (MPFR_SIGN(x) == 1) { printf("Inf"); } else printf("-Inf");
        !            70:   }
        !            71:   else if (!MPFR_NOTZERO(x)) {
        !            72:     if (MPFR_SIGN(x) < 0) printf("-");
        !            73:     printf("0");
        !            74:   }
1.1       maekawa    75:   else {
                     76:      /* 3 char for sign + 0 + binary point
1.1.1.2 ! ohara      77:        + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB for mantissa
1.1       maekawa    78:        + 2 for brackets in mantissa
                     79:        + 1 for 'E'
                     80:        + 11 for exponent (including sign)
1.1.1.2 ! ohara      81:        = 17 + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB
1.1       maekawa    82:       */
1.1.1.2 ! ohara      83:     alloc_size = 17 + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB;
        !            84:      str = (char *) (*__gmp_allocate_func) (alloc_size * sizeof(char));
1.1       maekawa    85:      mpfr_get_str_raw(str, x);
                     86:
                     87:      printf("%s", str);
1.1.1.2 ! ohara      88:      (*__gmp_free_func) (str, alloc_size * sizeof(char));
1.1       maekawa    89:   }
                     90: }
                     91:

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