Fix a floating point presentation error
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5012 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
2ba5d2de82
commit
495c45d4f6
@ -3125,3 +3125,8 @@
|
|||||||
* lib/stdio/lib_libvsprintf.c: Fieldwidth and justification were not
|
* lib/stdio/lib_libvsprintf.c: Fieldwidth and justification were not
|
||||||
supported for the %s format. As a result, %s, %12s, and %-12s all
|
supported for the %s format. As a result, %s, %12s, and %-12s all
|
||||||
produced the same output.
|
produced the same output.
|
||||||
|
* lib/stdio/lib_libdtoa.c: Fix several issues with presenting floating
|
||||||
|
point numbers (conversions are fine, but presentation was bad). This
|
||||||
|
is a critical bug fix if you use printf or sprintf to deal with floating
|
||||||
|
point numbers.
|
||||||
|
|
||||||
|
8
TODO
8
TODO
@ -15,7 +15,7 @@ nuttx/
|
|||||||
(5) Binary loaders (binfmt/)
|
(5) Binary loaders (binfmt/)
|
||||||
(17) Network (net/, drivers/net)
|
(17) Network (net/, drivers/net)
|
||||||
(3) USB (drivers/usbdev, drivers/usbhost)
|
(3) USB (drivers/usbdev, drivers/usbhost)
|
||||||
(9) Libraries (lib/)
|
(10) Libraries (lib/)
|
||||||
(10) File system/Generic drivers (fs/, drivers/)
|
(10) File system/Generic drivers (fs/, drivers/)
|
||||||
(5) Graphics subystem (graphics/)
|
(5) Graphics subystem (graphics/)
|
||||||
(1) Pascal add-on (pcode/)
|
(1) Pascal add-on (pcode/)
|
||||||
@ -685,6 +685,12 @@ o Libraries (lib/)
|
|||||||
Status: Open
|
Status: Open
|
||||||
Priority: Low -- more of a roadmap
|
Priority: Low -- more of a roadmap
|
||||||
|
|
||||||
|
Title: FLOATING POINT FORMATS
|
||||||
|
Description: Only the %f floating point format is supported. Others are accepted
|
||||||
|
but treated like %f.
|
||||||
|
Status: Open
|
||||||
|
Priority: Medium (this might important to someone.
|
||||||
|
|
||||||
o File system / Generic drivers (fs/, drivers/)
|
o File system / Generic drivers (fs/, drivers/)
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@ -48,7 +48,13 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define MAXEXP 308
|
#ifndef MIN
|
||||||
|
# define MIN(a,b) (a < b ? a : b)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MAX
|
||||||
|
# define MAX(a,b) (a > b ? a : b)
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Type Declarations
|
* Private Type Declarations
|
||||||
@ -58,10 +64,6 @@
|
|||||||
* Private Function Prototypes
|
* Private Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static char* cvt(double value, int ndigits, int flags, char *sign,
|
|
||||||
int *decpt, int ch, int *length);
|
|
||||||
static int exponent(char *p0, int exp, int fmtch);
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Global Constant Data
|
* Global Constant Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -78,269 +80,123 @@ static int exponent(char *p0, int exp, int fmtch);
|
|||||||
* Private Variables
|
* Private Variables
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: zeroes
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Print the specified number of zeres
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void zeroes(FAR struct lib_outstream_s *obj, int nzeroes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = nzeroes; i > 0; i--)
|
||||||
|
{
|
||||||
|
obj->put(obj, '0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: cvt
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
static char* cvt(double value, int ndigits, int flags, char *sign,
|
|
||||||
int *decpt, int ch, int *length)
|
|
||||||
{
|
|
||||||
int mode, dsgn;
|
|
||||||
char *digits, *bp, *rve;
|
|
||||||
|
|
||||||
if (ch == 'f')
|
|
||||||
{
|
|
||||||
mode = 3; /* ndigits after the decimal point */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* To obtain ndigits after the decimal point for the 'e' and 'E'
|
|
||||||
* formats, round to ndigits + 1 significant figures.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (ch == 'e' || ch == 'E')
|
|
||||||
{
|
|
||||||
ndigits++;
|
|
||||||
}
|
|
||||||
mode = 2; /* ndigits significant digits */
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value < 0)
|
|
||||||
{
|
|
||||||
value = -value;
|
|
||||||
*sign = '-';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*sign = '\000';
|
|
||||||
}
|
|
||||||
|
|
||||||
digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
|
|
||||||
if ((ch != 'g' && ch != 'G') || IS_ALTFORM(flags))
|
|
||||||
{
|
|
||||||
/* Print trailing zeros */
|
|
||||||
|
|
||||||
bp = digits + ndigits;
|
|
||||||
if (ch == 'f')
|
|
||||||
{
|
|
||||||
if (*digits == '0' && value)
|
|
||||||
{
|
|
||||||
*decpt = -ndigits + 1;
|
|
||||||
}
|
|
||||||
bp += *decpt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value == 0)
|
|
||||||
{
|
|
||||||
/* kludge for __dtoa irregularity */
|
|
||||||
|
|
||||||
rve = bp;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (rve < bp)
|
|
||||||
{
|
|
||||||
*rve++ = '0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*length = rve - digits;
|
|
||||||
return digits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: exponent
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
static int exponent(FAR char *p0, int exp, int fmtch)
|
|
||||||
{
|
|
||||||
FAR char *p;
|
|
||||||
FAR char *t;
|
|
||||||
char expbuf[MAXEXP];
|
|
||||||
|
|
||||||
p = p0;
|
|
||||||
*p++ = fmtch;
|
|
||||||
if (exp < 0)
|
|
||||||
{
|
|
||||||
exp = -exp;
|
|
||||||
*p++ = '-';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*p++ = '+';
|
|
||||||
}
|
|
||||||
|
|
||||||
t = expbuf + MAXEXP;
|
|
||||||
if (exp > 9)
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
*--t = (exp % 10) + '0';
|
|
||||||
}
|
|
||||||
while ((exp /= 10) > 9);
|
|
||||||
*--t = exp + '0';
|
|
||||||
for (; t < expbuf + MAXEXP; *p++ = *t++);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*p++ = '0';
|
|
||||||
*p++ = exp + '0';
|
|
||||||
}
|
|
||||||
return (p - p0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: lib_dtoa
|
* Name: lib_dtoa
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* This is part of lib_vsprintf(). It handles the floating point formats.
|
* This is part of lib_vsprintf(). It handles the floating point formats.
|
||||||
|
* This version supports only the &f (with precision).
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static void lib_dtoa(FAR struct lib_outstream_s *obj, int ch, int prec,
|
static void lib_dtoa(FAR struct lib_outstream_s *obj, int fmt, int prec,
|
||||||
uint8_t flags, double _double)
|
uint8_t flags, double value)
|
||||||
{
|
{
|
||||||
FAR char *cp; /* Handy char pointer (short term usage) */
|
FAR char *digits; /* String returned by __dtoa */
|
||||||
FAR char *cp_free = NULL; /* BIONIC: copy of cp to be freed after usage */
|
FAR char *digalloc; /* Copy of digits to be freed after usage */
|
||||||
char expstr[7]; /* Buffer for exponent string */
|
FAR char *rve; /* Points to the end of the return value */
|
||||||
char sign; /* Temporary negative sign for floats */
|
char sign; /* Temporary negative sign for floats */
|
||||||
int expt; /* Integer value of exponent */
|
int expt; /* Integer value of exponent */
|
||||||
int expsize = 0; /* Character count for expstr */
|
int numlen; /* Actual number of digits returned by cvt */
|
||||||
int ndig; /* Actual number of digits returned by cvt */
|
int nchars; /* Number of characters to print */
|
||||||
int size; /* Size of converted field or string */
|
int dsgn; /* Unused sign indicator */
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
cp = cvt(_double, prec, flags, &sign, &expt, ch, &ndig);
|
/* Non-zero... positive or negative */
|
||||||
cp_free = cp;
|
|
||||||
|
|
||||||
if (ch == 'g' || ch == 'G')
|
if (value < 0)
|
||||||
{
|
{
|
||||||
/* 'g' or 'G' fmt */
|
value = -value;
|
||||||
|
sign = '-';
|
||||||
if (expt <= -4 || expt > prec)
|
|
||||||
{
|
|
||||||
ch = (ch == 'g') ? 'e' : 'E';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ch = 'g';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ch <= 'e')
|
|
||||||
{
|
|
||||||
/* 'e' or 'E' fmt */
|
|
||||||
|
|
||||||
--expt;
|
|
||||||
expsize = exponent(expstr, expt, ch);
|
|
||||||
size = expsize + ndig;
|
|
||||||
if (ndig > 1 || IS_ALTFORM(flags))
|
|
||||||
{
|
|
||||||
++size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (ch == 'f')
|
|
||||||
{
|
|
||||||
/* f fmt */
|
|
||||||
|
|
||||||
if (expt > 0)
|
|
||||||
{
|
|
||||||
size = expt;
|
|
||||||
if (prec || IS_ALTFORM(flags))
|
|
||||||
{
|
|
||||||
size += prec + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else /* "0.X" */
|
|
||||||
{
|
|
||||||
size = prec + 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (expt >= ndig)
|
|
||||||
{
|
|
||||||
/* fixed g fmt */
|
|
||||||
|
|
||||||
size = expt;
|
|
||||||
if (IS_ALTFORM(flags))
|
|
||||||
{
|
|
||||||
++size;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
size = ndig + (expt > 0 ? 1 : 2 - expt);
|
sign = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Perform the conversion */
|
||||||
|
|
||||||
|
digits = __dtoa(value, 3, prec, &expt, &dsgn, &rve);
|
||||||
|
digalloc = digits;
|
||||||
|
numlen = rve - digits;
|
||||||
|
|
||||||
if (sign)
|
if (sign)
|
||||||
{
|
{
|
||||||
obj->put(obj, '-');
|
obj->put(obj, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_double == 0)
|
/* Always print at least one digit to the right of the decimal point. */
|
||||||
|
|
||||||
|
prec = MAX(1, prec);
|
||||||
|
|
||||||
|
/* Special case exact zero or the case where the number is smaller than
|
||||||
|
* the print precision.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (value == 0 || expt < -prec)
|
||||||
{
|
{
|
||||||
/* kludge for __dtoa irregularity */
|
/* kludge for __dtoa irregularity */
|
||||||
|
|
||||||
obj->put(obj, '0');
|
obj->put(obj, '0');
|
||||||
if (expt < ndig || IS_ALTFORM(flags))
|
obj->put(obj, '.');
|
||||||
{
|
|
||||||
obj->put(obj, '.');
|
|
||||||
|
|
||||||
i = ndig - 1;
|
|
||||||
while (i > 0)
|
|
||||||
{
|
|
||||||
obj->put(obj, '0');
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (expt <= 0)
|
else if (expt <= 0)
|
||||||
{
|
{
|
||||||
obj->put(obj, '0');
|
obj->put(obj, '0');
|
||||||
obj->put(obj, '.');
|
obj->put(obj, '.');
|
||||||
|
|
||||||
i = ndig;
|
/* Print leading zeros */
|
||||||
while (i > 0)
|
|
||||||
|
if (expt < 0)
|
||||||
{
|
{
|
||||||
obj->put(obj, *cp);
|
nchars = MIN(-expt, prec);
|
||||||
i--;
|
zeroes(obj, nchars);
|
||||||
cp++;
|
prec -= nchars;
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (expt >= ndig)
|
|
||||||
{
|
|
||||||
i = ndig;
|
|
||||||
while (i > 0)
|
|
||||||
{
|
|
||||||
obj->put(obj, *cp);
|
|
||||||
i--;
|
|
||||||
cp++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i = expt - ndig;
|
/* Print the significant digits */
|
||||||
while (i > 0)
|
|
||||||
|
nchars = MIN(numlen, prec);
|
||||||
|
for (i = nchars; i > 0; i--)
|
||||||
{
|
{
|
||||||
obj->put(obj, '0');
|
obj->put(obj, *digits);
|
||||||
i--;
|
digits++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_ALTFORM(flags))
|
/* Decremnt to get the number of trailing zeroes to print */
|
||||||
{
|
|
||||||
obj->put(obj, '.');
|
prec -= nchars;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Print the integer */
|
/* Print the integer part */
|
||||||
|
|
||||||
i = expt;
|
for (i = expt; i > 0; i--)
|
||||||
while (i > 0)
|
|
||||||
{
|
{
|
||||||
obj->put(obj, *cp);
|
obj->put(obj, *digits);
|
||||||
i--;
|
digits++;
|
||||||
cp++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print the decimal place */
|
/* Print the decimal place */
|
||||||
@ -349,14 +205,32 @@ static void lib_dtoa(FAR struct lib_outstream_s *obj, int ch, int prec,
|
|||||||
|
|
||||||
/* Print the decimal */
|
/* Print the decimal */
|
||||||
|
|
||||||
i = ndig - expt;
|
numlen -= expt;
|
||||||
while (i > 0)
|
nchars = MIN(numlen, prec);
|
||||||
|
|
||||||
|
for (i = nchars; i > 0; i--)
|
||||||
{
|
{
|
||||||
obj->put(obj, *cp);
|
obj->put(obj, *digits);
|
||||||
i--;
|
digits++;
|
||||||
cp++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Decremnt to get the number of trailing zeroes to print */
|
||||||
|
|
||||||
|
prec -= nchars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Finally, print any trailing zeroes */
|
||||||
|
|
||||||
|
zeroes(obj, prec);
|
||||||
|
|
||||||
|
/* Is this memory supposed to be freed or not? */
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
if (digalloc)
|
||||||
|
{
|
||||||
|
free(digalloc);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
Loading…
Reference in New Issue
Block a user