summaryrefslogtreecommitdiff
path: root/print_raw.c
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2003-10-02 17:17:59 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2003-10-02 17:17:59 +0000
commit29963d6597d7fdc221eef8df533a8ad55bd7425f (patch)
tree27570a058b82424e0e0b08eb142ff248d4fb17ae /print_raw.c
parentc4681b7961763f6c05e2ce4858787d146cde7219 (diff)
downloadmpfr-29963d6597d7fdc221eef8df533a8ad55bd7425f.tar.gz
Updated documentation. In particular, mpfr_set_str_raw renamed
as mpfr_set_str_binary. This function and mpfr_print_binary are now internal functions. mpfr_print_binary no longer prints the non-significant 0 bits. Updated the source to match the manual. mpfr_print_binary has been completely rewritten (now directly prints to stdout, without using an intermediate string). In mpfr_set_str_binary, replaced atol by strtol + error checking. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@2466 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'print_raw.c')
-rw-r--r--print_raw.c101
1 files changed, 44 insertions, 57 deletions
diff --git a/print_raw.c b/print_raw.c
index a04413987..a7427ee58 100644
--- a/print_raw.c
+++ b/print_raw.c
@@ -1,4 +1,4 @@
-/* mpfr_print_binary -- print the internal binary representation of a
+/* mpfr_print_binary -- print the internal binary representation of a
floating-point number
Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
@@ -21,71 +21,58 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <stdio.h>
+#include <limits.h>
+
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
-static void mpfr_get_str_raw _PROTO ((char *, mpfr_srcptr));
-
-static void
-mpfr_get_str_raw (char *digit_ptr, mpfr_srcptr x)
+void
+mpfr_print_binary (mpfr_srcptr x)
{
- mp_limb_t *mx, wd, t; long ex, sx, k, l, p;
+ if (MPFR_IS_NAN (x))
+ {
+ printf ("@NaN@");
+ return;
+ }
- mx = MPFR_MANT(x);
- ex = MPFR_GET_EXP (x);
- p = MPFR_PREC(x);
+ if (MPFR_SIGN (x) < 0)
+ printf ("-");
- if (MPFR_SIGN(x) < 0) { *digit_ptr = '-'; digit_ptr++; }
- sprintf(digit_ptr, "0."); digit_ptr += 2;
+ if (MPFR_IS_INF (x))
+ printf ("@Inf@");
+ else if (MPFR_IS_ZERO (x))
+ printf ("0");
+ else
+ {
+ mp_limb_t *mx;
+ mp_prec_t px;
+ mp_size_t n;
- sx = 1+(p-1)/BITS_PER_MP_LIMB; /* number of significant limbs */
- for (k = sx - 1; k >= 0 ; k--)
- {
- wd = mx[k];
- t = MPFR_LIMB_HIGHBIT;
- for (l = BITS_PER_MP_LIMB - 1; l>=0; l--)
- {
- if (wd & t)
- { *digit_ptr = '1'; digit_ptr++; }
- else
- { *digit_ptr = '0'; digit_ptr++; }
- t >>= 1;
- if (--p==0) { *digit_ptr = '['; digit_ptr++; }
- }
- }
- sprintf(digit_ptr, "]E%ld", ex);
-}
-
-void
-mpfr_print_binary (mpfr_srcptr x)
-{
- char *str;
- unsigned long alloc_size;
+ mx = MPFR_MANT (x);
+ px = MPFR_PREC (x);
- if (MPFR_IS_NAN(x)) printf("@NaN@");
- else if (MPFR_IS_INF(x)) {
- if (MPFR_SIGN(x) == 1) { printf("@Inf@"); } else printf("-@Inf@");
- }
- else if (!MPFR_NOTZERO(x)) {
- if (MPFR_SIGN(x) < 0) printf("-");
- printf("0");
- }
- else {
- /* 3 char for sign + 0 + binary point
- + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB for mantissa
- + 2 for brackets in mantissa
- + 1 for 'E'
- + 11 for exponent (including sign)
- = 17 + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB
- */
- alloc_size = 17 + MPFR_ABSSIZE(x) * BITS_PER_MP_LIMB;
- str = (char *) (*__gmp_allocate_func) (alloc_size * sizeof(char));
- mpfr_get_str_raw(str, x);
+ printf ("0.");
+ for (n = (px - 1) / BITS_PER_MP_LIMB; ; n--)
+ {
+ mp_limb_t wd, t;
- printf("%s", str);
- (*__gmp_free_func) (str, alloc_size * sizeof(char));
- }
-}
+ MPFR_ASSERTN (n >= 0);
+ wd = mx[n];
+ for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1)
+ {
+ printf ((wd & t) == 0 ? "0" : "1");
+ if (--px == 0)
+ {
+ mp_exp_t ex;
+ ex = MPFR_GET_EXP (x);
+ MPFR_ASSERTN (ex >= LONG_MIN && ex <= LONG_MAX);
+ printf ("E%ld", (long) ex);
+ return;
+ }
+ }
+ }
+ }
+}