diff --git a/src/web/WebUtils.C b/src/web/WebUtils.C
index 8047ac0..ceb2e44 100644
--- a/src/web/WebUtils.C
+++ b/src/web/WebUtils.C
@@ -31,6 +31,9 @@
 #ifdef SPIRIT_FLOAT_FORMAT
 #include <boost/config/warning_disable.hpp>
 #include <boost/spirit/include/karma.hpp>
+#else
+#include <boost/math/special_functions/fpclassify.hpp>
+#include <boost/math/special_functions/sign.hpp>
 #endif // SPIRIT_FLOAT_FORMAT
 
 namespace Wt {
@@ -230,7 +233,21 @@ static inline char *generic_double_to_str(double d, char *buf)
 #else
 static inline char *generic_double_to_str(double d, char *buf)
 {
-  sprintf(buf, "%f", (float)d);
+  if (boost::math::isnan(d)) {
+    if (boost::math::sign(d) == -1) {
+      sprintf(buf, "-NaN");
+    } else {
+      sprintf(buf, "NaN");
+    }
+  } else if (boost::math::isinf(d)) {
+    if (boost::math::sign(d) == -1) {
+      sprintf(buf, "-Infinity");
+    } else {
+      sprintf(buf, "Infinity");
+    }
+  } else {
+    sprintf(buf, "%f", (float)d);
+  }
   return buf;
 }
 #endif
