2 * (c) Copyright 1998-2001 by Rob Braun
3 * All rights reserved. The file named COPYRIGHT specifies the terms
4 * and conditions for redistribution.
17 #define FLOAT_TYPE double
19 #define FLOAT_FMT_FLAG
20 #define FLOAT_NAME_EXT
28 #define APPEND(a, b) APPEND2 (a, b)
32 #define APPEND2(a, b) a##b
36 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
39 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
42 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
45 #define EXP APPEND(exp, FLOAT_NAME_EXT)
48 #define ISINF APPEND(isinf, FLOAT_NAME_EXT)
51 #define ISNAN APPEND(isnan, FLOAT_NAME_EXT)
59 #define MAX(a,b) ((a)>(b)?(a):(b))
63 #define __set_errno(x) errno = (x)
68 APPEND (FUNC_PREFIX, fcvt_r) (FLOAT_TYPE value,
84 if (!ISINF (value) && !ISNAN (value)) {
85 /* OK, the following is not entirely correct. -0.0 is not handled
86 * correctly but glibc 2.0 does not have a portable function to
87 * implement this test.
94 /* Rounding to the left of the decimal point. */
96 FLOAT_TYPE new_value = value * 0.1;
98 if (new_value < 1.0) {
109 /* Value is Inf or NaN. */
113 n = strx_nprint (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
118 while (i < n && isdigit (buf[i]))
123 /* Value is Inf or NaN. */
129 while (i < n && !isdigit (buf[i]));
131 if (*decpt == 1 && buf[0] == '0' && value != 0.0) {
132 /* We must not have leading zeroes. Strip them all out and
133 * adjust *DECPT if necessary. */
135 while (i < n && buf[i] == '0')
142 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
143 buf[n - (i - MAX (*decpt, 0))] = '\0';
149 while (left-- > 0 && n < len)
157 #endif /* HAVE_FCVT */
159 #define weak_extern2(name)
160 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
165 APPEND (FUNC_PREFIX, ecvt_r) (FLOAT_TYPE value,
174 if (!ISNAN (value) && !ISINF (value) && value != 0.0) {
175 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
177 if (log10_function) {
178 /* Use the reasonable code if -lm is included. */
179 FLOAT_TYPE dexponent;
180 dexponent = FLOOR (LOG10 (FABS (value)));
181 value *= EXP (dexponent * -M_LN10);
182 exponent = (int) dexponent;
184 /* Slow code that doesn't require -lm functions. */
195 } else if (d >= 10.0) {
206 } else if (value == 0.0)
207 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
208 * This could be changed to -1 if we want to return 0. */
211 if (ndigit <= 0 && len > 0) {
214 if (!ISINF (value) && !ISNAN (value))
219 if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign,
226 #endif /* HAVE_ECVT */
230 APPEND (FUNC_PREFIX, fcvt) (FLOAT_TYPE value,
235 static char buf[MAXDIG];
237 (void) APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
242 #endif /* HAVE_FCVT */
246 APPEND (FUNC_PREFIX, ecvt) (FLOAT_TYPE value,
251 static char buf[MAXDIG];
253 (void) APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
258 #endif /* HAVE_ECVT */
262 APPEND (FUNC_PREFIX, gcvt) (FLOAT_TYPE value,
266 sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", ndigit, value);
269 #endif /* HAVE_GCVT */