From e3b51a1b1565cb82c0440f38f005f46b348ffa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Tschumperl=C3=A9?= Date: Sat, 9 Jun 2018 14:04:45 +0200 Subject: [PATCH] Fix NaN recognition in math parser. --- src/main/cpp/CImg.h | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/cpp/CImg.h b/src/main/cpp/CImg.h index 8efbdf1..3707263 100644 --- a/src/main/cpp/CImg.h +++ b/src/main/cpp/CImg.h @@ -15508,20 +15508,15 @@ namespace cimg_library_suffixed { CImgList l_opcode; // Look for a single value or a pre-defined variable. - int nb = cimg_sscanf(ss,"%lf%c%c",&val,&(sep=0),&(end=0)); - -#if cimg_OS==2 - // Check for +/-NaN and +/-inf as Microsoft's sscanf() version is not able - // to read those particular values. - if (!nb && (*ss=='+' || *ss=='-' || *ss=='i' || *ss=='I' || *ss=='n' || *ss=='N')) { - is_sth = true; - s = ss; - if (*s=='+') ++s; else if (*s=='-') { ++s; is_sth = false; } + int nb = 0; + s = ss + (*ss=='+' || *ss=='-'?1:0); + if (*s=='i' || *s=='I' || *s=='n' || *s=='N') { // Particular cases : +/-NaN and +/-Inf + is_sth = !(*ss=='-'); if (!cimg::strcasecmp(s,"inf")) { val = cimg::type::inf(); nb = 1; } else if (!cimg::strcasecmp(s,"nan")) { val = cimg::type::nan(); nb = 1; } if (nb==1 && !is_sth) val = -val; } -#endif + if (!nb) nb = cimg_sscanf(ss,"%lf%c%c",&val,&(sep=0),&(end=0)); if (nb==1) _cimg_mp_constant(val); if (nb==2 && sep=='%') _cimg_mp_constant(val/100); @@ -19807,12 +19802,12 @@ namespace cimg_library_suffixed { unsigned int constant(const double val) { // Search for built-in constant. + if (cimg::type::is_nan(val)) return _cimg_mp_slot_nan; if (val==(double)(int)val) { if (val>=0 && val<=10) return (unsigned int)val; if (val<0 && val>=-5) return (unsigned int)(10 - val); } if (val==0.5) return 16; - if (cimg::type::is_nan(val)) return _cimg_mp_slot_nan; // Search for constant already requested before (in const cache). unsigned int ind = ~0U;