1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
26 #include <sys/param.h>
29 #define FLOAT_TYPE double
31 #define FLOAT_FMT_FLAG
32 #define FLOAT_NAME_EXT
35 #define APPEND(a, b) APPEND2 (a, b)
36 #define APPEND2(a, b) a##b
38 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
39 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
40 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
41 #define EXP APPEND(exp, FLOAT_NAME_EXT)
42 #define ISINF APPEND(isinf, FLOAT_NAME_EXT)
43 #define ISNAN APPEND(isnan, FLOAT_NAME_EXT)
47 APPEND (FUNC_PREFIX
, fcvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
49 int ndigit
, *decpt
, *sign
;
63 if (!ISINF (value
) && !ISNAN (value
))
65 /* OK, the following is not entirely correct. -0.0 is not handled
66 correctly but glibc 2.0 does not have a portable function to
67 implement this test. */
74 /* Rounding to the left of the decimal point. */
77 FLOAT_TYPE new_value
= value
* 0.1;
92 /* Value is Inf or NaN. */
95 n
= snprintf (buf
, len
, "%.*" FLOAT_FMT_FLAG
"f", ndigit
, value
);
100 while (i
< n
&& isdigit (buf
[i
]))
105 /* Value is Inf or NaN. */
112 while (i
< n
&& !isdigit (buf
[i
]));
114 if (*decpt
== 1 && buf
[0] == '0' && value
!= 0.0)
116 /* We must not have leading zeroes. Strip them all out and
117 adjust *DECPT if necessary. */
119 while (i
< n
&& buf
[i
] == '0')
126 memmove (&buf
[MAX (*decpt
, 0)], &buf
[i
], n
- i
);
127 buf
[n
- (i
- MAX (*decpt
, 0))] = '\0';
135 while (left
-- > 0 && n
< len
)
144 #define weak_extern2(name) weak_extern (name)
145 weak_extern2 (FLOOR
) weak_extern2 (LOG10
) weak_extern2 (FABS
)
149 APPEND (FUNC_PREFIX
, ecvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
151 int ndigit
, *decpt
, *sign
;
157 if (!ISNAN (value
) && !ISINF (value
) && value
!= 0.0)
159 FLOAT_TYPE (*log10_function
) (FLOAT_TYPE
) = &LOG10
;
163 /* Use the reasonable code if -lm is included. */
164 FLOAT_TYPE dexponent
;
165 dexponent
= FLOOR (LOG10 (FABS (value
)));
166 value
*= EXP (dexponent
* -M_LN10
);
167 exponent
= (int) dexponent
;
171 /* Slow code that doesn't require -lm functions. */
201 else if (value
== 0.0)
202 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
203 This could be changed to -1 if we want to return 0. */
206 if (ndigit
<= 0 && len
> 0)
210 if (!ISINF (value
) && !ISNAN (value
))
216 if (APPEND (FUNC_PREFIX
, fcvt_r
) (value
, ndigit
- 1, decpt
, sign
,