summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2013-04-17 07:40:48 -0600
committerGregory Nutt <gnutt@nuttx.org>2013-04-17 07:40:48 -0600
commit7d4b026f84fa39efa1a650993c14149458c65931 (patch)
treee5ba7c5e46d1e5bf2faeb66e491b5cebf012be01
parent5b97e75ab0280ccfca66554ddfb4727fba3f3e2c (diff)
downloadnuttx-7d4b026f84fa39efa1a650993c14149458c65931.tar.gz
nuttx-7d4b026f84fa39efa1a650993c14149458c65931.tar.bz2
nuttx-7d4b026f84fa39efa1a650993c14149458c65931.zip
Support for printing NaN and infinity from Andrew Tridgell
-rw-r--r--nuttx/libc/stdio/lib_libdtoa.c43
1 files changed, 40 insertions, 3 deletions
diff --git a/nuttx/libc/stdio/lib_libdtoa.c b/nuttx/libc/stdio/lib_libdtoa.c
index a9a86817c..7f3970127 100644
--- a/nuttx/libc/stdio/lib_libdtoa.c
+++ b/nuttx/libc/stdio/lib_libdtoa.c
@@ -44,6 +44,8 @@
* Included Files
****************************************************************************/
+#include <math.h>
+
#include "lib_internal.h"
/****************************************************************************
@@ -85,6 +87,10 @@
****************************************************************************/
/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
* Name: zeroes
*
* Description:
@@ -103,9 +109,21 @@ static void zeroes(FAR struct lib_outstream_s *obj, int nzeroes)
}
/****************************************************************************
- * Private Functions
+ * Name: lib_dtoa_string
+ *
+ * Description:
+ * Print the specified string
+ *
****************************************************************************/
+static void lib_dtoa_string(FAR struct lib_outstream_s *obj, const char *str)
+{
+ while (*str)
+ {
+ obj->put(obj, *str++);
+ }
+}
+
/****************************************************************************
* Name: lib_dtoa
*
@@ -140,6 +158,25 @@ static void lib_dtoa(FAR struct lib_outstream_s *obj, int fmt, int prec,
int dsgn; /* Unused sign indicator */
int i;
+ /* Special handling for NaN and Infinity */
+
+ if (isnan(value))
+ {
+ lib_dtoa_string(obj, "NaN");
+ return;
+ }
+
+ if (isinf(value))
+ {
+ if (value < 0.0d)
+ {
+ obj->put(obj, '-');
+ }
+
+ lib_dtoa_string(obj, "Infinity");
+ return;
+ }
+
/* Non-zero... positive or negative */
if (value < 0)
@@ -173,7 +210,7 @@ static void lib_dtoa(FAR struct lib_outstream_s *obj, int fmt, int prec,
obj->put(obj, '0');
- /* A decimal point is printed only in the alternate form or if a
+ /* A decimal point is printed only in the alternate form or if a
* particular precision is requested.
*/
@@ -227,7 +264,7 @@ static void lib_dtoa(FAR struct lib_outstream_s *obj, int fmt, int prec,
for (i = expt; i > 0; i--)
{
if (*digits != '\0')
- {
+ {
obj->put(obj, *digits);
digits++;
}