xref: /dragonfly/contrib/gmp/mpf/out_str.c (revision d4ef6694)
1 /* mpf_out_str (stream, base, n_digits, op) -- Print N_DIGITS digits from
2    the float OP to STREAM in base BASE.  Return the number of characters
3    written, or 0 if an error occurred.
4 
5 Copyright 1996, 1997, 2001, 2002, 2005 Free Software Foundation, Inc.
6 
7 This file is part of the GNU MP Library.
8 
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 The GNU MP Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
21 
22 #define _GNU_SOURCE    /* for DECIMAL_POINT in langinfo.h */
23 
24 #include "config.h"
25 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #if HAVE_LANGINFO_H
30 #include <langinfo.h>  /* for nl_langinfo */
31 #endif
32 
33 #if HAVE_LOCALE_H
34 #include <locale.h>    /* for localeconv */
35 #endif
36 
37 #include "gmp.h"
38 #include "gmp-impl.h"
39 
40 
41 size_t
42 mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op)
43 {
44   char *str;
45   mp_exp_t exp;
46   size_t written;
47   TMP_DECL;
48 
49   TMP_MARK;
50 
51   if (base == 0)
52     base = 10;
53   if (n_digits == 0)
54     MPF_SIGNIFICANT_DIGITS (n_digits, base, op->_mp_prec);
55 
56   if (stream == 0)
57     stream = stdout;
58 
59   /* Consider these changes:
60      * Don't allocate memory here for huge n_digits; pass NULL to mpf_get_str.
61      * Make mpf_get_str allocate extra space when passed NULL, to avoid
62        allocating two huge string buffers.
63      * Implement more/other allocation reductions tricks.  */
64 
65   str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */
66 
67   mpf_get_str (str, &exp, base, n_digits, op);
68   n_digits = strlen (str);
69 
70   written = 0;
71 
72   /* Write sign */
73   if (str[0] == '-')
74     {
75       str++;
76       fputc ('-', stream);
77       written = 1;
78       n_digits--;
79     }
80 
81   {
82     const char  *point = GMP_DECIMAL_POINT;
83     size_t      pointlen = strlen (point);
84     putc ('0', stream);
85     fwrite (point, 1, pointlen, stream);
86     written += pointlen + 1;
87   }
88 
89   /* Write mantissa */
90   {
91     size_t fwret;
92     fwret = fwrite (str, 1, n_digits, stream);
93     written += fwret;
94   }
95 
96   /* Write exponent */
97   {
98     int fpret;
99     fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp);
100     written += fpret;
101   }
102 
103   TMP_FREE;
104   return ferror (stream) ? 0 : written;
105 }
106