From: Paul Eggert Date: Sun, 17 Sep 2017 22:25:44 +0000 (-0700) Subject: Fix bug with min and max and NaNs X-Git-Tag: emacs-26.0.90~145 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=5f28f0db73c03b98b27e04a458ebb209b5d9acde;p=emacs.git Fix bug with min and max and NaNs * src/data.c (minmax_driver): Fix bug with (min 0 NaN), which mistakenly yielded 0. Also, pacify GCC in a better way. * test/src/data-tests.el (data-tests-min): Test for the bug. --- diff --git a/src/data.c b/src/data.c index 95bf06e5102..e070be6c208 100644 --- a/src/data.c +++ b/src/data.c @@ -3010,16 +3010,16 @@ static Lisp_Object minmax_driver (ptrdiff_t nargs, Lisp_Object *args, enum Arith_Comparison comparison) { - eassume (0 < nargs); - Lisp_Object accum = args[0]; /* pacify GCC */ - for (ptrdiff_t argnum = 0; argnum < nargs; argnum++) + Lisp_Object accum = args[0]; + CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (accum); + for (ptrdiff_t argnum = 1; argnum < nargs; argnum++) { Lisp_Object val = args[argnum]; CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (val); - if (argnum == 0 || !NILP (arithcompare (val, accum, comparison))) + if (!NILP (arithcompare (val, accum, comparison))) accum = val; - else if (FLOATP (accum) && isnan (XFLOAT_DATA (accum))) - return accum; + else if (FLOATP (val) && isnan (XFLOAT_DATA (val))) + return val; } return accum; } diff --git a/test/src/data-tests.el b/test/src/data-tests.el index 5dc26348a6f..8de8c145d40 100644 --- a/test/src/data-tests.el +++ b/test/src/data-tests.el @@ -101,7 +101,11 @@ (should (= 3 (apply #'min '(3 8 3)))) (should-error (min 9 8 'foo)) (should-error (min (make-marker))) - (should (eql 1 (min (point-min-marker) 1)))) + (should (eql 1 (min (point-min-marker) 1))) + (should (isnan (min 0.0e+NaN))) + (should (isnan (min 0.0e+NaN 1 2))) + (should (isnan (min 1.0 0.0e+NaN))) + (should (isnan (min 1.0 0.0e+NaN 1.1)))) ;; Bool vector tests. Compactly represent bool vectors as hex ;; strings.