2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
27 /* Format floating-point number and print them.
28 Return number of chars printed, or EOF on error.
30 sign_mode == '+' : print "-" or "+"
31 sign_mode == ' ' : print "-" or " "
32 sign_mode == '\0' : print "-' or ""
36 DEFUN(_IO_outfloat, (value, sb, type, width, precision, flags,
38 double value AND _IO_FILE *sb AND int type AND int width
39 AND int precision AND int flags AND int sign_mode AND int fill)
42 #define PUT(x) do {if (_IO_putc(x, sb) < 0) goto error; count++;} while (0)
44 do {int _n=n; count+=_n; if (_IO_sputn(sb, p,_n) != _n) goto error;} while(0)
45 #define PADN(fill, n) \
46 do {int _n = n; count+=_n; if (_IO_padn(sb, fill, _n) != _n) goto error;} while (0)
47 int pad_kind = flags & (_IO_LEFT|_IO_RIGHT|_IO_INTERNAL);
49 int show_dot = (flags & _IO_SHOWPOINT) != 0;
55 int trailing_zeroes, useful_digits;
56 int padding, unpadded_width;
61 #define EBUF_END &ebuf[EBUF_SIZE]
75 precision++; /* Add one to include digit before decimal point. */
79 exp = type == 'g' ? 'e' : 'E';
80 if (precision == 0) precision = 1;
81 if (!(flags & _IO_SHOWPOINT))
87 /* Do the actual convension */
88 if (precision == 999 && mode != 3)
90 p = _IO_dtoa(value, mode, precision, &decpt, &sign, &end);
91 useful_digits = end-p;
92 exponent_start = EBUF_END;
94 precision = useful_digits;
95 /* Check if we need to emit an exponent. */
96 if (mode != 3 && decpt != 9999)
99 if ((type != 'g' && type != 'F') || i < -4 || i >= precision)
101 /* Print the exponent into ebuf.
102 We write ebuf in reverse order (right-to-left). */
108 /* Note: ANSI requires at least 2 exponent digits. */
110 *--exponent_start = (i % 10) + '0';
113 *--exponent_start = i + '0';
114 *--exponent_start = sign;
115 *--exponent_start = exp;
118 exponent_size = EBUF_END - exponent_start;
121 /* If we print an exponent, always show just one digit before point. */
125 { /* Infinity or NaN */
126 decpt = useful_digits;
131 /* dtoa truncates trailing zeroes. Set the variable trailing_zeroes to
132 the number of 0's we have to add (after the decimal point). */
135 else if (type == 'f')
136 trailing_zeroes = useful_digits <= decpt ? precision
137 : precision-(useful_digits-decpt);
138 else if (exponent_size) /* 'e' 'E' or 'g' format using exponential notation*/
139 trailing_zeroes = precision - useful_digits;
140 else /* 'g' format not using exponential notation. */
141 trailing_zeroes = useful_digits <= decpt ? precision - decpt
142 : precision-useful_digits;
143 if (trailing_zeroes < 0) trailing_zeroes = 0;
145 if (trailing_zeroes != 0 || useful_digits > decpt)
148 print_sign = sign ? '-' : 0;
149 else if (sign_mode == '+')
150 print_sign = sign ? '-' : '+';
151 else /* if (sign_mode == ' ') */
152 print_sign = sign ? '-' : ' ';
154 /* Calculate the width (before padding). */
156 (print_sign != 0) + trailing_zeroes + exponent_size + show_dot
158 + (decpt > useful_digits ? decpt - useful_digits
159 : decpt > 0 ? 0 : 1 - decpt);
161 padding = width > unpadded_width ? width - unpadded_width : 0;
162 if (padding > 0 && pad_kind != _IO_LEFT && pad_kind != _IO_INTERNAL)
163 PADN(fill, padding); /* Default (right) adjust */
166 if (pad_kind == _IO_INTERNAL && padding > 0)
170 if (useful_digits >= decpt)
174 PUTN(p, useful_digits);
175 PADN('0', decpt-useful_digits);
180 /* Print digits after the decimal point. */
181 if (useful_digits > decpt)
182 PUTN(p + decpt, useful_digits-decpt);
192 /* Print digits after the decimal point. */
193 PUTN(p, useful_digits);
196 PADN('0', trailing_zeroes);
198 PUTN(exponent_start, exponent_size);
199 if (pad_kind == _IO_LEFT && padding > 0) /* Left adjustment*/