kernel - CAM cleanup 1/N - Remove ancient scsi pccard drivers ncv, nsp, stg
[dragonfly.git] / contrib / gmp / mpf / out_str.c
blobafccdbb031a302f354538112e00f409ca6033d95
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.
5 Copyright 1996, 1997, 2001, 2002, 2005 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
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.
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.
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/. */
22 #define _GNU_SOURCE /* for DECIMAL_POINT in langinfo.h */
24 #include "config.h"
26 #include <stdio.h>
27 #include <string.h>
29 #if HAVE_LANGINFO_H
30 #include <langinfo.h> /* for nl_langinfo */
31 #endif
33 #if HAVE_LOCALE_H
34 #include <locale.h> /* for localeconv */
35 #endif
37 #include "gmp.h"
38 #include "gmp-impl.h"
41 size_t
42 mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_srcptr op)
44 char *str;
45 mp_exp_t exp;
46 size_t written;
47 TMP_DECL;
49 TMP_MARK;
51 if (base == 0)
52 base = 10;
53 if (n_digits == 0)
54 MPF_SIGNIFICANT_DIGITS (n_digits, base, op->_mp_prec);
56 if (stream == 0)
57 stream = stdout;
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. */
65 str = (char *) TMP_ALLOC (n_digits + 2); /* extra for minus sign and \0 */
67 mpf_get_str (str, &exp, base, n_digits, op);
68 n_digits = strlen (str);
70 written = 0;
72 /* Write sign */
73 if (str[0] == '-')
75 str++;
76 fputc ('-', stream);
77 written = 1;
78 n_digits--;
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;
89 /* Write mantissa */
91 size_t fwret;
92 fwret = fwrite (str, 1, n_digits, stream);
93 written += fwret;
96 /* Write exponent */
98 int fpret;
99 fpret = fprintf (stream, (base <= 10 ? "e%ld" : "@%ld"), exp);
100 written += fpret;
103 TMP_FREE;
104 return ferror (stream) ? 0 : written;