kernel - namecache - fix deadlock
[dragonfly.git] / contrib / mpfr / out_str.c
blob570dff005e93e1f07183d6c4d24064e322b6dd7d
1 /* mpfr_out_str -- output a floating-point number to a stream
3 Copyright 1999, 2001, 2002, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include <string.h>
25 #include "mpfr-impl.h"
27 size_t
28 mpfr_out_str (FILE *stream, int base, size_t n_digits, mpfr_srcptr op,
29 mp_rnd_t rnd_mode)
31 char *s, *s0;
32 size_t l;
33 mp_exp_t e;
35 MPFR_ASSERTN (base >= 2 && base <= 36);
37 /* when stream=NULL, output to stdout */
38 if (stream == NULL)
39 stream = stdout;
41 if (MPFR_IS_NAN(op))
43 fprintf (stream, "@NaN@");
44 return 3;
47 if (MPFR_IS_INF(op))
49 if (MPFR_SIGN(op) > 0)
51 fprintf (stream, "@Inf@");
52 return 3;
54 else
56 fprintf (stream, "-@Inf@");
57 return 4;
61 if (MPFR_IS_ZERO(op))
63 if (MPFR_SIGN(op) > 0)
65 fprintf(stream, "0");
66 return 1;
68 else
70 fprintf(stream, "-0");
71 return 2;
75 s = mpfr_get_str (NULL, &e, base, n_digits, op, rnd_mode);
77 s0 = s;
78 /* for op=3.1416 we have s = "31416" and e = 1 */
80 l = strlen (s) + 1; /* size of allocated block returned by mpfr_get_str
81 - may be incorrect, as only an upper bound? */
82 if (*s == '-')
83 fputc (*s++, stream);
85 /* outputs mantissa */
86 fputc (*s++, stream); e--; /* leading digit */
87 fputc ((unsigned char) MPFR_DECIMAL_POINT, stream);
88 fputs (s, stream); /* rest of mantissa */
89 (*__gmp_free_func) (s0, l);
91 /* outputs exponent */
92 if (e)
94 MPFR_ASSERTN(e >= LONG_MIN);
95 MPFR_ASSERTN(e <= LONG_MAX);
96 l += fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), (long) e);
99 return l;