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