lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] strtod("inf") and MSVC


From: Vadim Zeitlin
Subject: [lmi] strtod("inf") and MSVC
Date: Mon, 27 Jun 2011 01:37:54 +0200

 Hello,

 LMI relies on strtod() accepting string "inf" as a valid input but this
is, AFAIR[*], C99 extension and probably because of this, MSVC doesn't
implement it and returns an error in this case, even in its latest version:
quoting http://msdn.microsoft.com/en-us/library/kxsfc1ab%28v=VS.100%29.aspx

        strtod expects nptr to point to a string of the following form:

        [whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits]

This prevents creating new census from working with MSVC right now as it
fails to parse "inf"s in the sample data files. For now I've applied the
smallest possible patch to make the program work for me with MSVC:

--- a/numeric_io_traits.hpp
+++ b/numeric_io_traits.hpp
@@ -341,7 +341,25 @@ template<> struct numeric_conversion_traits<double>
     static int digits(T t) {return floating_point_decimals(t);}
     static char const* fmt() {return "%#.*f";}
     static T strtoT(char const* nptr, char** endptr)
-        {return std::strtod(nptr, endptr);}
+        {
+#ifdef LMI_MSC
+            // COMPILER !! MSVC strtod() doesn't support C99 "inf[inity]" nor
+            // "nan[(...)]" strings nor hexadecimal notation so provide our
+            // work around for at least the first one of them which we actually
+            // need. This workaround is, of course, incomplete as it doesn't
+            // even support "-inf" without mentioning long and non-lower-case
+            // versions or NaN support.
+            if(strncmp(nptr, "inf", 3) == 0)
+                {
+                if(endptr)
+                    {
+                    *endptr = const_cast<char *>(nptr) + 3;
+                    }
+                return std::numeric_limits<T>::infinity();
+                }
+#endif // LMI_MSC
+            return std::strtod(nptr, endptr);
+        }
 };

 #if !defined LMI_COMPILER_PROVIDES_STRTOLD

But I could extend/prettify it a little if you thought it would be useful
to have. Would it?

 Thanks,
VZ

[*] I do know that C99 mandates this behaviour (7.20.1.3/3) but I only
    believe that it was a new addition in it. I don't have C89 standard
    anymore however so I can't check it.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]