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. */
28 /* Format floating-point number and print them.
29 Return number of chars printed, or EOF on error.
31 sign_mode == '+' : print "-" or "+"
32 sign_mode == ' ' : print "-" or " "
33 sign_mode == '\0' : print "-' or ""
37 _IO_outfloat (value
, sb
, type
, width
, precision
, flags
, sign_mode
, fill
)
48 #define PUT(x) do {if (_IO_putc(x, sb) < 0) goto error; count++;} while (0)
50 do {int _n=n; count+=_n; if (_IO_sputn(sb, p,_n) != _n) goto error;} while(0)
51 #define PADN(fill, n) \
52 do {int _n = n; count+=_n; if (_IO_padn(sb, fill, _n) != _n) goto error;} while (0)
53 int pad_kind
= flags
& (_IO_LEFT
|_IO_RIGHT
|_IO_INTERNAL
);
55 int show_dot
= (flags
& _IO_SHOWPOINT
) != 0;
61 int trailing_zeroes
, useful_digits
;
62 int padding
, unpadded_width
;
67 #define EBUF_END &ebuf[EBUF_SIZE]
81 precision
++; /* Add one to include digit before decimal point. */
85 exp
= type
== 'g' ? 'e' : 'E';
86 if (precision
== 0) precision
= 1;
87 if (!(flags
& _IO_SHOWPOINT
))
93 /* Do the actual convension */
94 if (precision
== 999 && mode
!= 3)
96 p
= _IO_dtoa(value
, mode
, precision
, &decpt
, &sign
, &end
);
97 useful_digits
= end
-p
;
98 exponent_start
= EBUF_END
;
100 precision
= useful_digits
;
101 /* Check if we need to emit an exponent. */
102 if (mode
!= 3 && decpt
!= 9999)
105 if ((type
!= 'g' && type
!= 'F') || i
< -4 || i
>= precision
)
107 /* Print the exponent into ebuf.
108 We write ebuf in reverse order (right-to-left). */
114 /* Note: ANSI requires at least 2 exponent digits. */
116 *--exponent_start
= (i
% 10) + '0';
119 *--exponent_start
= i
+ '0';
120 *--exponent_start
= sign
;
121 *--exponent_start
= exp
;
124 exponent_size
= EBUF_END
- exponent_start
;
127 /* If we print an exponent, always show just one digit before point. */
131 { /* Infinity or NaN */
132 decpt
= useful_digits
;
137 /* dtoa truncates trailing zeroes. Set the variable trailing_zeroes to
138 the number of 0's we have to add (after the decimal point). */
141 else if (type
== 'f')
142 trailing_zeroes
= useful_digits
<= decpt
? precision
143 : precision
-(useful_digits
-decpt
);
144 else if (exponent_size
) /* 'e' 'E' or 'g' format using exponential notation*/
145 trailing_zeroes
= precision
- useful_digits
;
146 else /* 'g' format not using exponential notation. */
147 trailing_zeroes
= useful_digits
<= decpt
? precision
- decpt
148 : precision
-useful_digits
;
149 if (trailing_zeroes
< 0) trailing_zeroes
= 0;
151 if (trailing_zeroes
!= 0 || useful_digits
> decpt
)
154 print_sign
= sign
? '-' : 0;
155 else if (sign_mode
== '+')
156 print_sign
= sign
? '-' : '+';
157 else /* if (sign_mode == ' ') */
158 print_sign
= sign
? '-' : ' ';
160 /* Calculate the width (before padding). */
162 (print_sign
!= 0) + trailing_zeroes
+ exponent_size
+ show_dot
164 + (decpt
> useful_digits
? decpt
- useful_digits
165 : decpt
> 0 ? 0 : 1 - decpt
);
167 padding
= width
> unpadded_width
? width
- unpadded_width
: 0;
168 if (padding
> 0 && pad_kind
!= _IO_LEFT
&& pad_kind
!= _IO_INTERNAL
)
169 PADN(fill
, padding
); /* Default (right) adjust */
172 if (pad_kind
== _IO_INTERNAL
&& padding
> 0)
176 if (useful_digits
>= decpt
)
180 PUTN(p
, useful_digits
);
181 PADN('0', decpt
-useful_digits
);
186 /* Print digits after the decimal point. */
187 if (useful_digits
> decpt
)
188 PUTN(p
+ decpt
, useful_digits
-decpt
);
198 /* Print digits after the decimal point. */
199 PUTN(p
, useful_digits
);
202 PADN('0', trailing_zeroes
);
204 PUTN(exponent_start
, exponent_size
);
205 if (pad_kind
== _IO_LEFT
&& padding
> 0) /* Left adjustment*/