3 * LICENSE: See COPYING in the top level directory
5 * FILE: lib/sdk/crt/stdlib/ecvt.c
6 * PROGRAMERS: Gregor Schneider (parts based on ecvtbuf.c by DJ Delorie)
15 _ecvt (double value
, int ndigits
, int *decpt
, int *sign
)
17 static char ecvtbuf
[DBL_MAX_10_EXP
+ 10];
20 s
= cvtbuf
= (char*)malloc(ndigits
+ 18); /* sign, dot, null, 15 for alignment */
31 sprintf(cvtbuf
, "%-+.*E", ndigits
, value
);
32 /* Treat special values */
33 if (strncmp(s
, "NaN", 3) == 0)
35 memcpy(ecvtbuf
, s
, 4);
37 else if (strncmp(s
+ 1, "Inf", 3) == 0)
39 memcpy(ecvtbuf
, s
, 5);
49 /* Copy the first digit */
52 if (d
- ecvtbuf
< ndigits
)
61 /* Skip the decimal point */
66 /* Copy fractional digits */
67 while (*s
&& *s
!= 'E')
69 if (d
- ecvtbuf
< ndigits
)
78 /* Skip the exponent */
83 /* Set the decimal point to the exponent value plus the one digit we copied */
85 /* Handle special decimal point cases */
92 /* Need enhanced precision*/
93 char* tbuf
= (char*)malloc(ndigits
+ 18);
99 sprintf(tbuf
, "%-+.*E", ndigits
+ 2, value
);
106 /* Pad with zeroes */
107 while (d
- ecvtbuf
< ndigits
)