~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/mpfr/out_str.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpfr_out_str -- output a floating-point number to a stream
 
2
 
 
3
Copyright 1999, 2001, 2003 Free Software Foundation, Inc.
 
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 Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 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 Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser 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 <string.h>
 
24
#include <limits.h>
 
25
#include "gmp.h"
 
26
#include "gmp-impl.h"
 
27
#include "mpfr.h"
 
28
#include "mpfr-impl.h"
 
29
 
 
30
size_t 
 
31
mpfr_out_str (FILE *stream, int base, size_t n_digits, mpfr_srcptr op,
 
32
              mp_rnd_t rnd_mode)
 
33
{
 
34
  char *s, *s0;
 
35
  size_t l;
 
36
  mp_exp_t e;
 
37
 
 
38
  /* when stream=NULL, output to stdout */
 
39
  if (stream == NULL)
 
40
    stream = stdout;
 
41
 
 
42
  if (MPFR_IS_NAN(op))
 
43
    {
 
44
      fprintf (stream, "NaN");
 
45
      return 3;
 
46
    }
 
47
 
 
48
  if (MPFR_IS_INF(op)) 
 
49
    { 
 
50
      if (MPFR_SIGN(op) > 0)
 
51
        {
 
52
          fprintf (stream, "Inf");
 
53
          return 3;
 
54
        }
 
55
      else
 
56
        {
 
57
          fprintf (stream, "-Inf");
 
58
          return 4;
 
59
        }
 
60
    }
 
61
 
 
62
  if (MPFR_IS_ZERO(op))
 
63
    {
 
64
      if (MPFR_SIGN(op) > 0)
 
65
        {
 
66
          fprintf(stream, "0");
 
67
          return 1;
 
68
        }
 
69
      else
 
70
        {
 
71
          fprintf(stream, "-0");
 
72
          return 2;
 
73
        }
 
74
    }
 
75
 
 
76
  s = mpfr_get_str (NULL, &e, base, n_digits, op, rnd_mode);
 
77
 
 
78
  s0 = s;
 
79
  /* for op=3.1416 we have s = "31416" and e = 1 */
 
80
 
 
81
  l = strlen (s) + 1; /* size of allocated block returned by mpfr_get_str
 
82
                         - may be incorrect, as only an upper bound? */
 
83
  if (*s == '-')
 
84
    fputc (*s++, stream);
 
85
 
 
86
  /* outputs mantissa */
 
87
  fputc (*s++, stream); e--; /* leading digit */
 
88
  fputc ('.', stream);       /* decimal point */
 
89
  fputs (s, stream);         /* rest of mantissa */
 
90
  (*__gmp_free_func) (s0, l);
 
91
 
 
92
  /* outputs exponent */
 
93
  if (e)
 
94
    {
 
95
      MPFR_ASSERTN(e >= LONG_MIN);
 
96
      MPFR_ASSERTN(e <= LONG_MAX);
 
97
      l += fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), (long) e);
 
98
    }
 
99
 
 
100
  return l;
 
101
}