From: Amro Date: Fri, 29 Jul 2016 12:30:02 +0000 (+0300) Subject: alternate implementations for isinf/isfinite X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=451b4f36e586cf5127dacef6e4efc4ba8f7bb307;p=nlopt.git alternate implementations for isinf/isfinite --- diff --git a/util/stop.c b/util/stop.c index 3cfb8d2..7349df1 100644 --- a/util/stop.c +++ b/util/stop.c @@ -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)