lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [5483] Improve treatment of infinities


From: Greg Chicares
Subject: [lmi-commits] [5483] Improve treatment of infinities
Date: Sun, 03 Jun 2012 19:50:14 +0000

Revision: 5483
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5483
Author:   chicares
Date:     2012-06-03 19:50:14 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
Improve treatment of infinities

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/numeric_io_traits.hpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2012-06-03 19:17:40 UTC (rev 5482)
+++ lmi/trunk/ChangeLog 2012-06-03 19:50:14 UTC (rev 5483)
@@ -30199,3 +30199,27 @@
   numeric_io_test.cpp
 Augment unit tests for infinities.
 
+20120603T1829Z <address@hidden> [578]
+
+  config.hpp
+  config_ming323.hpp
+  numeric_io_cast.hpp
+  numeric_io_traits.hpp
+Refactor.
+
+20120603T1902Z <address@hidden> [578]
+
+  numeric_io_traits.hpp
+Refactor.
+
+20120603T1917Z <address@hidden> [578]
+
+  numeric_io_traits.hpp
+Restructure, extending "INF" treatment to long double.
+
+20120603T1950Z <address@hidden> [578]
+
+  numeric_io_traits.hpp
+Improve treatment of infinities. See (VZ):
+  http://lists.nongnu.org/archive/html/lmi/2012-05/msg00030.html
+

Modified: lmi/trunk/numeric_io_traits.hpp
===================================================================
--- lmi/trunk/numeric_io_traits.hpp     2012-06-03 19:17:40 UTC (rev 5482)
+++ lmi/trunk/numeric_io_traits.hpp     2012-06-03 19:50:14 UTC (rev 5483)
@@ -31,7 +31,7 @@
 #include <algorithm>                    // std::max()
 #include <cmath>                        // C99 functions fabsl(), log10l(), 
strtold()
 #include <cstdlib>                      // std::strto*()
-#include <cstring>                      // std::strncmp()
+#include <cstring>                      // std::strcmp(), std::strlen()
 #include <limits>
 #include <stdexcept>
 #include <string>
@@ -330,26 +330,44 @@
 };
 
 #if defined LMI_MSVCRT
-/// COMPILER !! This C runtime's 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.
+/// COMPILER !! This C runtime's strtod() doesn't understand C99's
+/// "inf[inity]". Work around that, but don't worry about NaNs.
 
 double strtoFDL_msvc(char const* nptr, char** endptr)
 {
-    {
-    if(0 == std::strncmp(nptr, "inf", 3))
+    if(!nptr || !endptr)
         {
-        if(endptr)
+        throw std::runtime_error("Numeric conversion: precondition failure.");
+        }
+    char* rendptr; // Pointer to which second std::strtod() argument refers.
+    double z = std::strtod(nptr, &rendptr);
+    if('\0' == *rendptr)
+        {
+        *endptr = rendptr;
+        return z;
+        }
+    else
+        {
+        bool negative = '-' == *nptr;
+        if(negative) {++nptr;}
+        if
+            (  0 == std::strcmp(nptr, "inf")
+            || 0 == std::strcmp(nptr, "INF")
+            || 0 == std::strcmp(nptr, "infinity")
+            || 0 == std::strcmp(nptr, "INFINITY")
+            )
             {
-            *endptr = const_cast<char *>(nptr) + 3;
+            *endptr = const_cast<char*>(nptr) + std::strlen(nptr);
+            return negative
+                ? -std::numeric_limits<double>::infinity()
+                :  std::numeric_limits<double>::infinity()
+                ;
             }
-        return std::numeric_limits<double>::infinity();
+        else
+            {
+            throw std::invalid_argument("Numeric conversion failed.");
+            }
         }
-    return std::strtod(nptr, endptr);
-    }
 }
 #endif // defined LMI_MSVCRT
 




reply via email to

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