1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <sys/param.h>
30 # define FLOAT_TYPE double
32 # define FLOAT_FMT_FLAG
33 # define FLOAT_NAME_EXT
34 # if DBL_MANT_DIG == 53
35 # define NDIGIT_MAX 17
36 # elif DBL_MANT_DIG == 24
38 # elif DBL_MANT_DIG == 56
39 # define NDIGIT_MAX 18
41 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
42 compile time constant here, so we cannot use it. */
43 # error "NDIGIT_MAX must be precomputed"
44 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
48 #define APPEND(a, b) APPEND2 (a, b)
49 #define APPEND2(a, b) a##b
51 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
52 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
53 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
54 #define EXP APPEND(exp, FLOAT_NAME_EXT)
58 APPEND (FUNC_PREFIX
, fcvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
60 int ndigit
, *decpt
, *sign
;
77 *sign
= signbit (value
) != 0;
83 /* Rounding to the left of the decimal point. */
86 FLOAT_TYPE new_value
= value
* 0.1;
101 /* Value is Inf or NaN. */
104 n
= __snprintf (buf
, len
, "%.*" FLOAT_FMT_FLAG
"f", MIN (ndigit
, NDIGIT_MAX
),
106 /* Check for a too small buffer. */
107 if (n
>= (ssize_t
) len
)
111 while (i
< n
&& isdigit (buf
[i
]))
116 /* Value is Inf or NaN. */
123 while (i
< n
&& !isdigit (buf
[i
]));
125 if (*decpt
== 1 && buf
[0] == '0' && value
!= 0.0)
127 /* We must not have leading zeroes. Strip them all out and
128 adjust *DECPT if necessary. */
130 while (i
< n
&& buf
[i
] == '0')
137 memmove (&buf
[MAX (*decpt
, 0)], &buf
[i
], n
- i
);
138 buf
[n
- (i
- MAX (*decpt
, 0))] = '\0';
146 while (left
-- > 0 && n
< len
)
156 APPEND (FUNC_PREFIX
, ecvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
158 int ndigit
, *decpt
, *sign
;
164 if (isfinite (value
) && value
!= 0.0)
166 /* Slow code that doesn't require -lm functions. */
191 while (d
>= f
* 10.0);
196 else if (value
== 0.0)
197 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
198 This could be changed to -1 if we want to return 0. */
201 if (ndigit
<= 0 && len
> 0)
205 *sign
= isfinite (value
) ? signbit (value
) != 0 : 0;
208 if (APPEND (FUNC_PREFIX
, fcvt_r
) (value
, MIN (ndigit
, NDIGIT_MAX
) - 1,
209 decpt
, sign
, buf
, len
))