chiark / gitweb /
alternate implementations for isinf/isfinite
authorAmro <amroamroamro@gmail.com>
Fri, 29 Jul 2016 12:30:02 +0000 (15:30 +0300)
committerAmro <amroamroamro@gmail.com>
Tue, 13 Sep 2016 18:24:31 +0000 (21:24 +0300)
util/stop.c

index 3cfb8d264a2e84ca21a327abbd6a5054bbe1e071..7349df154bcb4a83ccad46ebd49aa2b880e7ad0a 100644 (file)
@@ -144,7 +144,8 @@ char *nlopt_vsprintf(char *p, const char *format, va_list ap)
 
     p = (char *) realloc(p, len);
     if (!p) abort();
-    
+
+    /* TODO: check HAVE_VSNPRINTF, and fallback to vsprintf otherwise */
     while ((ret = vsnprintf(p, len, format, ap)) < 0 || (size_t)ret >= len) {
         /* C99 vsnprintf returns the required number of bytes (excluding \0)
            if the buffer is too small; older versions (e.g. MS) return -1 */
@@ -170,31 +171,39 @@ void nlopt_stop_msg(const nlopt_stopping *s, const char *format, ...)
 int nlopt_isinf(double x)
 {
     return (fabs(x) >= HUGE_VAL * 0.99)
-#ifdef HAVE_ISINF
+#if defined(HAVE_ISINF)
         || isinf(x)
+#else
+        || (!nlopt_isnan(x) && nlopt_isnan(x - x))
 #endif
     ;
 }
 
 int nlopt_isfinite(double x)
 {
-    return (fabs(x) <= DBL_MAX);
+    return (fabs(x) <= DBL_MAX)
+#if defined(HAVE_ISFINITE)
+        || isfinite(x)
+#elif defined(_WIN32)
+        || _finite(x)
+#endif
+    ;
 }
 
 int nlopt_istiny(double x)
 {
-#if defined(HAVE_FPCLASSIFY)
-    return x == 0.0 || fpclassify(x) == FP_SUBNORMAL;
-#elif defined(_WIN32)
     if (x == 0.0)
         return 1;
     else {
+#if defined(HAVE_FPCLASSIFY)
+        return fpclassify(x) == FP_SUBNORMAL;
+#elif defined(_WIN32)
         int c = _fpclass(x);
         return c == _FPCLASS_ND || c == _FPCLASS_PD;
-    }
 #else
-    return fabs(x) < 2.2250738585072014e-308; /* assume IEEE 754 double */
+        return fabs(x) < 2.2250738585072014e-308; /* assume IEEE 754 double */
 #endif
+    }
 }
 
 int nlopt_isnan(double x)