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 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 2.1 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; see the file COPYING.LIB. If not, write to |
---|
21 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
22 | MA 02111-1307, USA. */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #if HAVE_LOCALE_H |
---|
30 | #include <locale.h> /* for localeconv */ |
---|
31 | #endif |
---|
32 | |
---|
33 | #include "gmp.h" |
---|
34 | #include "gmp-impl.h" |
---|
35 | |
---|
36 | size_t |
---|
37 | mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op) |
---|
38 | { |
---|
39 | char *str; |
---|
40 | mp_exp_t exp; |
---|
41 | size_t written; |
---|
42 | TMP_DECL (marker); |
---|
43 | |
---|
44 | TMP_MARK (marker); |
---|
45 | |
---|
46 | if (base == 0) |
---|
47 | base = 10; |
---|
48 | if (n_digits == 0) |
---|
49 | MPF_SIGNIFICANT_DIGITS (n_digits, base, op->_mp_prec); |
---|
50 | |
---|
51 | if (stream == 0) |
---|
52 | stream = stdout; |
---|
53 | |
---|
54 | str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */ |
---|
55 | |
---|
56 | mpf_get_str (str, &exp, base, n_digits, op); |
---|
57 | n_digits = strlen (str); |
---|
58 | |
---|
59 | written = 0; |
---|
60 | |
---|
61 | /* Write sign */ |
---|
62 | if (str[0] == '-') |
---|
63 | { |
---|
64 | str++; |
---|
65 | fputc ('-', stream); |
---|
66 | written = 1; |
---|
67 | n_digits--; |
---|
68 | } |
---|
69 | |
---|
70 | #if HAVE_LOCALECONV |
---|
71 | { |
---|
72 | const char *point = localeconv()->decimal_point; |
---|
73 | size_t pointlen = strlen (point); |
---|
74 | putc ('0', stream); |
---|
75 | fwrite (point, 1, pointlen, stream); |
---|
76 | written += pointlen + 1; |
---|
77 | } |
---|
78 | #else |
---|
79 | fwrite ("0.", 1, 2, stream); |
---|
80 | written += 2; |
---|
81 | #endif |
---|
82 | |
---|
83 | /* Write mantissa */ |
---|
84 | { |
---|
85 | size_t fwret; |
---|
86 | fwret = fwrite (str, 1, n_digits, stream); |
---|
87 | written += fwret; |
---|
88 | } |
---|
89 | |
---|
90 | /* Write exponent */ |
---|
91 | { |
---|
92 | int fpret; |
---|
93 | fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp); |
---|
94 | written += fpret; |
---|
95 | } |
---|
96 | |
---|
97 | TMP_FREE (marker); |
---|
98 | return ferror (stream) ? 0 : written; |
---|
99 | } |
---|