1 /* Floating point output for `printf'.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* The gmp headers need some configuration frobs. */
32 #include <gmp-mparam.h>
33 #include <stdlib/gmp.h>
34 #include <stdlib/gmp-impl.h>
35 #include <stdlib/longlong.h>
36 #include <stdlib/fpioconst.h>
37 #include <locale/localeinfo.h>
46 # define NDEBUG /* Undefine this for debugging assertions. */
50 /* This defines make it possible to use the same code for GNU C library and
51 the GNU I/O library. */
53 # define PUT(f, s, n) _IO_sputn (f, s, n)
54 # define PAD(f, c, n) _IO_padn (f, c, n)
55 /* We use this file GNU C library and GNU I/O library. So make
58 # define putc(c, f) _IO_putc_unlocked (c, f)
59 # define size_t _IO_size_t
60 # define FILE _IO_FILE
61 #else /* ! USE_IN_LIBIO */
62 # define PUT(f, s, n) fwrite (s, 1, n, f)
63 # define PAD(f, c, n) __printf_pad (f, c, n)
64 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
65 #endif /* USE_IN_LIBIO */
67 /* Macros for doing the actual output. */
72 register const int outc = (ch); \
73 if (putc (outc, fp) == EOF) \
78 #define PRINT(ptr, len) \
81 register size_t outlen = (len); \
84 if (PUT (fp, ptr, outlen) != outlen) \
91 while (outlen-- > 0) \
96 #define PADN(ch, len) \
99 if (PAD (fp, ch, len) != len) \
105 /* We use the GNU MP library to handle large numbers.
107 An MP variable occupies a varying number of entries in its array. We keep
108 track of this number for efficiency reasons. Otherwise we would always
109 have to process the whole array. */
110 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
112 #define MPN_ASSIGN(dst,src) \
113 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
114 #define MPN_GE(u,v) \
115 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
117 extern int __isinfl (long double), __isnanl (long double);
119 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
120 int *expt
, int *is_neg
,
122 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
123 int *expt
, int *is_neg
,
125 extern unsigned int __guess_grouping (unsigned int intdig_max
,
126 const char *grouping
, wchar_t sepchar
);
129 static char *group_number (char *buf
, char *bufend
, unsigned int intdig_no
,
130 const char *grouping
, wchar_t thousands_sep
)
135 __printf_fp (FILE *fp
,
136 const struct printf_info
*info
,
137 const void *const *args
)
139 /* The floating-point value to output. */
143 __long_double_t ldbl
;
147 /* Locale-dependent representation of decimal point. */
150 /* Locale-dependent thousands separator and grouping specification. */
151 wchar_t thousands_sep
;
152 const char *grouping
;
154 /* "NaN" or "Inf" for the special cases. */
155 const char *special
= NULL
;
157 /* We need just a few limbs for the input before shifting to the right
159 mp_limb_t fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
160 /* We need to shift the contents of fp_input by this amount of bits. */
163 /* The fraction of the floting-point value in question */
165 /* and the exponent. */
167 /* Sign of the exponent. */
169 /* Sign of float number. */
172 /* Scaling factor. */
175 /* Temporary bignum value. */
178 /* Digit which is result of last hack_digit() call. */
181 /* The type of output format that will be used: 'e'/'E' or 'f'. */
184 /* Counter for number of written characters. */
187 /* General helper (carry limb). */
190 char hack_digit (void)
194 if (expsign
!= 0 && type
== 'f' && exponent
-- > 0)
196 else if (scalesize
== 0)
198 hi
= frac
[fracsize
- 1];
199 cy
= __mpn_mul_1 (frac
, frac
, fracsize
- 1, 10);
200 frac
[fracsize
- 1] = cy
;
204 if (fracsize
< scalesize
)
208 hi
= mpn_divmod (tmp
, frac
, fracsize
, scale
, scalesize
);
209 tmp
[fracsize
- scalesize
] = hi
;
212 fracsize
= scalesize
;
213 while (fracsize
!= 0 && frac
[fracsize
- 1] == 0)
217 /* We're not prepared for an mpn variable with zero
224 cy
= __mpn_mul_1 (frac
, frac
, fracsize
, 10);
226 frac
[fracsize
++] = cy
;
233 /* Figure out the decimal point character. */
234 if (info
->extra
== 0)
236 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
237 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
238 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
242 if (mbtowc (&decimal
, _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
),
243 strlen (_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
))) <= 0)
244 decimal
= (wchar_t) *_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
246 /* Give default value. */
247 if (decimal
== L
'\0')
253 if (info
->extra
== 0)
254 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
256 grouping
= _NL_CURRENT (LC_MONETARY
, MON_GROUPING
);
258 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
262 /* Figure out the thousands separator character. */
263 if (info
->extra
== 0)
265 if (mbtowc (&thousands_sep
, _NL_CURRENT (LC_NUMERIC
,
267 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
)))
269 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
,
274 if (mbtowc (&thousands_sep
, _NL_CURRENT (LC_MONETARY
,
276 strlen (_NL_CURRENT (LC_MONETARY
,
277 MON_THOUSANDS_SEP
))) <= 0)
278 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_MONETARY
,
282 if (thousands_sep
== L
'\0')
289 /* Fetch the argument value. */
290 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
292 fpnum
.ldbl
= *(const long double *) args
[0];
294 /* Check for special values: not a number or infinity. */
295 if (__isnanl (fpnum
.ldbl
))
297 special
= isupper (info
->spec
) ? "NAN" : "nan";
300 else if (__isinfl (fpnum
.ldbl
))
302 special
= isupper (info
->spec
) ? "INF" : "inf";
303 is_neg
= fpnum
.ldbl
< 0;
307 fracsize
= __mpn_extract_long_double (fp_input
,
309 sizeof (fp_input
[0])),
312 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
317 fpnum
.dbl
= *(const double *) args
[0];
319 /* Check for special values: not a number or infinity. */
320 if (__isnan (fpnum
.dbl
))
322 special
= isupper (info
->spec
) ? "NAN" : "nan";
325 else if (__isinf (fpnum
.dbl
))
327 special
= isupper (info
->spec
) ? "INF" : "inf";
328 is_neg
= fpnum
.dbl
< 0;
332 fracsize
= __mpn_extract_double (fp_input
,
334 / sizeof (fp_input
[0])),
335 &exponent
, &is_neg
, fpnum
.dbl
);
336 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
342 int width
= info
->width
;
344 if (is_neg
|| info
->showsign
|| info
->space
)
348 if (!info
->left
&& width
> 0)
353 else if (info
->showsign
)
355 else if (info
->space
)
360 if (info
->left
&& width
> 0)
367 /* We need three multiprecision variables. Now that we have the exponent
368 of the number we can allocate the needed memory. It would be more
369 efficient to use variables of the fixed maximum size but because this
370 would be really big it could lead to memory problems. */
372 mp_size_t bignum_size
= ((ABS (exponent
) + BITS_PER_MP_LIMB
- 1)
373 / BITS_PER_MP_LIMB
+ 4) * sizeof (mp_limb_t
);
374 frac
= (mp_limb_t
*) alloca (bignum_size
);
375 tmp
= (mp_limb_t
*) alloca (bignum_size
);
376 scale
= (mp_limb_t
*) alloca (bignum_size
);
379 /* We now have to distinguish between numbers with positive and negative
380 exponents because the method used for the one is not applicable/efficient
387 int explog
= LDBL_MAX_10_EXP_LOG
;
389 const struct mp_power
*tens
= &_fpioconst_pow10
[explog
+ 1];
392 if ((exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
394 MPN_COPY_DECR (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
396 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
400 cy
= __mpn_lshift (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
402 (exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
403 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
405 frac
[fracsize
++] = cy
;
407 MPN_ZERO (frac
, (exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
409 assert (tens
> &_fpioconst_pow10
[0]);
414 /* The number of the product of two binary numbers with n and m
415 bits respectively has m+n or m+n-1 bits. */
416 if (exponent
>= scaleexpo
+ tens
->p_expo
- 1)
419 MPN_ASSIGN (tmp
, tens
->array
);
422 cy
= __mpn_mul (tmp
, scale
, scalesize
,
423 &tens
->array
[_FPIO_CONST_OFFSET
],
424 tens
->arraysize
- _FPIO_CONST_OFFSET
);
425 tmpsize
= scalesize
+ tens
->arraysize
- _FPIO_CONST_OFFSET
;
430 if (MPN_GE (frac
, tmp
))
433 MPN_ASSIGN (scale
, tmp
);
434 count_leading_zeros (cnt
, scale
[scalesize
- 1]);
435 scaleexpo
= (scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
436 exp10
|= 1 << explog
;
441 while (tens
> &_fpioconst_pow10
[0]);
444 /* Optimize number representations. We want to represent the numbers
445 with the lowest number of bytes possible without losing any
446 bytes. Also the highest bit in the scaling factor has to be set
447 (this is a requirement of the MPN division routines). */
450 /* Determine minimum number of zero bits at the end of
452 for (i
= 0; scale
[i
] == 0 && frac
[i
] == 0; i
++)
455 /* Determine number of bits the scaling factor is misplaced. */
456 count_leading_zeros (cnt_h
, scale
[scalesize
- 1]);
460 /* The highest bit of the scaling factor is already set. So
461 we only have to remove the trailing empty limbs. */
464 MPN_COPY_INCR (scale
, scale
+ i
, scalesize
- i
);
466 MPN_COPY_INCR (frac
, frac
+ i
, fracsize
- i
);
474 count_trailing_zeros (cnt_l
, scale
[i
]);
478 count_trailing_zeros (cnt_l2
, frac
[i
]);
484 count_trailing_zeros (cnt_l
, frac
[i
]);
486 /* Now shift the numbers to their optimal position. */
487 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
489 /* We cannot save any memory. So just roll both numbers
490 so that the scaling factor has its highest bit set. */
492 (void) __mpn_lshift (scale
, scale
, scalesize
, cnt_h
);
493 cy
= __mpn_lshift (frac
, frac
, fracsize
, cnt_h
);
495 frac
[fracsize
++] = cy
;
497 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
499 /* We can save memory by removing the trailing zero limbs
500 and by packing the non-zero limbs which gain another
503 (void) __mpn_rshift (scale
, scale
+ i
, scalesize
- i
,
504 BITS_PER_MP_LIMB
- cnt_h
);
506 (void) __mpn_rshift (frac
, frac
+ i
, fracsize
- i
,
507 BITS_PER_MP_LIMB
- cnt_h
);
508 fracsize
-= frac
[fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
512 /* We can only save the memory of the limbs which are zero.
513 The non-zero parts occupy the same number of limbs. */
515 (void) __mpn_rshift (scale
, scale
+ (i
- 1),
517 BITS_PER_MP_LIMB
- cnt_h
);
519 (void) __mpn_rshift (frac
, frac
+ (i
- 1),
521 BITS_PER_MP_LIMB
- cnt_h
);
522 fracsize
-= frac
[fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
527 else if (exponent
< 0)
531 int explog
= LDBL_MAX_10_EXP_LOG
;
532 const struct mp_power
*tens
= &_fpioconst_pow10
[explog
+ 1];
533 mp_size_t used_limbs
= fracsize
- 1;
535 /* Now shift the input value to its right place. */
536 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, to_shift
);
537 frac
[fracsize
++] = cy
;
538 assert (cy
== 1 || (frac
[fracsize
- 2] == 0 && frac
[0] == 0));
541 exponent
= -exponent
;
543 assert (tens
!= &_fpioconst_pow10
[0]);
548 if (exponent
>= tens
->m_expo
)
550 int i
, incr
, cnt_h
, cnt_l
;
553 /* The __mpn_mul function expects the first argument to be
554 bigger than the second. */
555 if (fracsize
< tens
->arraysize
- _FPIO_CONST_OFFSET
)
556 cy
= __mpn_mul (tmp
, &tens
->array
[_FPIO_CONST_OFFSET
],
557 tens
->arraysize
- _FPIO_CONST_OFFSET
,
560 cy
= __mpn_mul (tmp
, frac
, fracsize
,
561 &tens
->array
[_FPIO_CONST_OFFSET
],
562 tens
->arraysize
- _FPIO_CONST_OFFSET
);
563 tmpsize
= fracsize
+ tens
->arraysize
- _FPIO_CONST_OFFSET
;
567 count_leading_zeros (cnt_h
, tmp
[tmpsize
- 1]);
568 incr
= (tmpsize
- fracsize
) * BITS_PER_MP_LIMB
569 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
571 assert (incr
<= tens
->p_expo
);
573 /* If we increased the exponent by exactly 3 we have to test
574 for overflow. This is done by comparing with 10 shifted
575 to the right position. */
576 if (incr
== exponent
+ 3)
578 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
582 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
586 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
588 (void) __mpn_lshift (topval
, topval
, 2,
589 BITS_PER_MP_LIMB
- cnt_h
);
593 /* We have to be careful when multiplying the last factor.
594 If the result is greater than 1.0 be have to test it
595 against 10.0. If it is greater or equal to 10.0 the
596 multiplication was not valid. This is because we cannot
597 determine the number of bits in the result in advance. */
598 if (incr
< exponent
+ 3
599 || (incr
== exponent
+ 3 &&
600 (tmp
[tmpsize
- 1] < topval
[1]
601 || (tmp
[tmpsize
- 1] == topval
[1]
602 && tmp
[tmpsize
- 2] < topval
[0]))))
604 /* The factor is right. Adapt binary and decimal
607 exp10
|= 1 << explog
;
609 /* If this factor yields a number greater or equal to
610 1.0, we must not shift the non-fractional digits down. */
614 /* Now we optimize the number representation. */
615 for (i
= 0; tmp
[i
] == 0; ++i
);
616 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
618 MPN_COPY (frac
, tmp
+ i
, tmpsize
- i
);
619 fracsize
= tmpsize
- i
;
623 count_trailing_zeros (cnt_l
, tmp
[i
]);
625 /* Now shift the numbers to their optimal position. */
626 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
628 /* We cannot save any memory. Just roll the
629 number so that the leading digit is in a
632 cy
= __mpn_lshift (frac
, tmp
, tmpsize
, cnt_h
+ 1);
633 fracsize
= tmpsize
+ 1;
634 frac
[fracsize
- 1] = cy
;
636 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
638 (void) __mpn_rshift (frac
, tmp
+ i
, tmpsize
- i
,
639 BITS_PER_MP_LIMB
- 1 - cnt_h
);
640 fracsize
= tmpsize
- i
;
644 /* We can only save the memory of the limbs which
645 are zero. The non-zero parts occupy the same
648 (void) __mpn_rshift (frac
, tmp
+ (i
- 1),
650 BITS_PER_MP_LIMB
- 1 - cnt_h
);
651 fracsize
= tmpsize
- (i
- 1);
654 used_limbs
= fracsize
- 1;
659 while (tens
!= &_fpioconst_pow10
[1] && exponent
> 0);
660 /* All factors but 10^-1 are tested now. */
665 cy
= __mpn_mul_1 (tmp
, frac
, fracsize
, 10);
667 assert (cy
== 0 || tmp
[tmpsize
- 1] < 20);
669 count_trailing_zeros (cnt_l
, tmp
[0]);
670 if (cnt_l
< MIN (4, exponent
))
672 cy
= __mpn_lshift (frac
, tmp
, tmpsize
,
673 BITS_PER_MP_LIMB
- MIN (4, exponent
));
675 frac
[tmpsize
++] = cy
;
678 (void) __mpn_rshift (frac
, tmp
, tmpsize
, MIN (4, exponent
));
681 assert (frac
[fracsize
- 1] < 10);
687 /* This is a special case. We don't need a factor because the
688 numbers are in the range of 0.0 <= fp < 8.0. We simply
689 shift it to the right place and divide it by 1.0 to get the
690 leading digit. (Of course this division is not really made.) */
691 assert (0 <= exponent
&& exponent
< 3 &&
692 exponent
+ to_shift
< BITS_PER_MP_LIMB
);
694 /* Now shift the input value to its right place. */
695 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, (exponent
+ to_shift
));
696 frac
[fracsize
++] = cy
;
701 int width
= info
->width
;
702 char *buffer
, *startp
, *cp
;
705 int intdig_max
, intdig_no
= 0;
706 int fracdig_min
, fracdig_max
, fracdig_no
= 0;
710 if (tolower (info
->spec
) == 'e')
714 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
715 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
716 /* d . ddd e +- ddd */
717 dig_max
= INT_MAX
; /* Unlimited. */
718 significant
= 1; /* Does not matter here. */
720 else if (info
->spec
== 'f')
723 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
726 intdig_max
= exponent
+ 1;
727 /* This can be really big! */ /* XXX Maybe malloc if too big? */
728 chars_needed
= exponent
+ 1 + 1 + fracdig_max
;
733 chars_needed
= 1 + 1 + fracdig_max
;
735 dig_max
= INT_MAX
; /* Unlimited. */
736 significant
= 1; /* Does not matter here. */
740 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
741 if ((expsign
== 0 && exponent
>= dig_max
)
742 || (expsign
!= 0 && exponent
> 4))
744 type
= isupper (info
->spec
) ? 'E' : 'e';
745 fracdig_max
= dig_max
- 1;
747 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
752 intdig_max
= expsign
== 0 ? exponent
+ 1 : 0;
753 fracdig_max
= dig_max
- intdig_max
;
754 /* We need space for the significant digits and perhaps for
755 leading zeros when < 1.0. Pessimistic guess: dig_max. */
756 chars_needed
= dig_max
+ dig_max
+ 1;
758 fracdig_min
= info
->alt
? fracdig_max
: 0;
759 significant
= 0; /* We count significant digits. */
763 /* Guess the number of groups we will make, and thus how
764 many spaces we need for separator characters. */
765 chars_needed
+= __guess_grouping (intdig_max
, grouping
, thousands_sep
);
767 /* Allocate buffer for output. We need two more because while rounding
768 it is possible that we need two more characters in front of all the
770 buffer
= alloca (2 + chars_needed
);
771 cp
= startp
= buffer
+ 2; /* Let room for rounding. */
773 /* Do the real work: put digits in allocated buffer. */
774 if (expsign
== 0 || type
!= 'f')
776 assert (expsign
== 0 || intdig_max
== 1);
777 while (intdig_no
< intdig_max
)
780 *cp
++ = hack_digit ();
785 || (fracdig_max
> 0 && (fracsize
> 1 || frac
[0] != 0)))
790 /* |fp| < 1.0 and the selected type is 'f', so put "0."
797 /* Generate the needed number of fractional digits. */
798 while (fracdig_no
< fracdig_min
799 || (fracdig_no
< fracdig_max
&& (fracsize
> 1 || frac
[0] != 0)))
805 else if (significant
== 0)
815 digit
= hack_digit ();
820 if (digit
== '5' && (*(cp
- 1) & 1) == 0)
822 /* This is the critical case. */
823 if (fracsize
== 1 && frac
[0] == 0)
824 /* Rest of the number is zero -> round to even.
825 (IEEE 754-1985 4.1 says this is the default rounding.) */
827 else if (scalesize
== 0)
829 /* Here we have to see whether all limbs are zero since no
830 normalization happened. */
831 size_t lcnt
= fracsize
;
832 while (lcnt
>= 1 && frac
[lcnt
- 1] == 0)
835 /* Rest of the number is zero -> round to even.
836 (IEEE 754-1985 4.1 says this is the default rounding.) */
843 /* Process fractional digits. Terminate if not rounded or
844 radix character is reached. */
845 while (*--tp
!= decimal
&& *tp
== '9')
852 if (fracdig_no
== 0 || *tp
== decimal
)
854 /* Round the integer digits. */
855 if (*(tp
- 1) == decimal
)
858 while (--tp
>= startp
&& *tp
== '9')
865 /* It is more critical. All digits were 9's. */
870 exponent
+= expsign
== 0 ? 1 : -1;
872 else if (intdig_no
== dig_max
)
874 /* This is the case where for type %g the number fits
875 really in the range for %f output but after rounding
876 the number of digits is too big. */
880 if (info
->alt
|| fracdig_no
> 0)
882 /* Overwrite the old radix character. */
883 startp
[intdig_no
+ 2] = '0';
887 fracdig_no
+= intdig_no
;
889 fracdig_max
= intdig_max
- intdig_no
;
891 /* Now we must print the exponent. */
892 type
= isupper (info
->spec
) ? 'E' : 'e';
896 /* We can simply add another another digit before the
902 /* While rounding the number of digits can change.
903 If the number now exceeds the limits remove some
904 fractional digits. */
905 if (intdig_no
+ fracdig_no
> dig_max
)
907 cp
-= intdig_no
+ fracdig_no
- dig_max
;
908 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
915 /* Now remove unnecessary '0' at the end of the string. */
916 while (fracdig_no
> fracdig_min
&& *(cp
- 1) == '0')
921 /* If we eliminate all fractional digits we perhaps also can remove
922 the radix character. */
923 if (fracdig_no
== 0 && !info
->alt
&& *(cp
- 1) == decimal
)
927 /* Add in separator characters, overwriting the same buffer. */
928 cp
= group_number (startp
, cp
, intdig_no
, grouping
, thousands_sep
);
930 /* Write the exponent if it is needed. */
934 *cp
++ = expsign
? '-' : '+';
936 /* Find the magnitude of the exponent. */
938 while (expscale
<= exponent
)
942 /* Exponent always has at least two digits. */
948 *cp
++ = '0' + (exponent
/ expscale
);
949 exponent
%= expscale
;
951 while (expscale
> 10);
952 *cp
++ = '0' + exponent
;
955 /* Compute number of characters which must be filled with the padding
957 if (is_neg
|| info
->showsign
|| info
->space
)
959 width
-= cp
- startp
;
961 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
962 PADN (info
->pad
, width
);
966 else if (info
->showsign
)
968 else if (info
->space
)
971 if (!info
->left
&& info
->pad
== '0' && width
> 0)
974 PRINT (startp
, cp
- startp
);
976 if (info
->left
&& width
> 0)
977 PADN (info
->pad
, width
);
982 /* Return the number of extra grouping characters that will be inserted
983 into a number with INTDIG_MAX integer digits. */
986 __guess_grouping (unsigned int intdig_max
, const char *grouping
,
991 /* We treat all negative values like CHAR_MAX. */
993 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
994 /* No grouping should be done. */
998 while (intdig_max
> (unsigned int) *grouping
)
1001 intdig_max
-= *grouping
++;
1003 if (*grouping
== CHAR_MAX
1008 /* No more grouping should be done. */
1010 else if (*grouping
== 0)
1012 /* Same grouping repeats. */
1013 groups
+= (intdig_max
- 1) / grouping
[-1];
1021 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1022 There is guaranteed enough space past BUFEND to extend it.
1023 Return the new end of buffer. */
1027 group_number (char *buf
, char *bufend
, unsigned int intdig_no
,
1028 const char *grouping
, wchar_t thousands_sep
)
1030 unsigned int groups
= __guess_grouping (intdig_no
, grouping
, thousands_sep
);
1036 /* Move the fractional part down. */
1037 memmove (buf
+ intdig_no
+ groups
, buf
+ intdig_no
,
1038 bufend
- (buf
+ intdig_no
));
1040 p
= buf
+ intdig_no
+ groups
- 1;
1043 unsigned int len
= *grouping
++;
1045 *p
-- = buf
[--intdig_no
];
1047 *p
-- = thousands_sep
;
1049 if (*grouping
== CHAR_MAX
1054 /* No more grouping should be done. */
1056 else if (*grouping
== 0)
1057 /* Same grouping repeats. */
1059 } while (intdig_no
> (unsigned int) *grouping
);
1061 /* Copy the remaining ungrouped digits. */
1063 *p
-- = buf
[--intdig_no
];
1066 return bufend
+ groups
;