Skip to content

Commit f4156f6

Browse files
committed
Fix nanflag support for min/max in mx-inlines.cc (bug #67714)
This changeset fixes a wrong result for min/max operations on dense matrices with NaN values, which can be reproduced with: [m,i] = max ([1,2,3;4,3,NaN;4,5,6], [], 2, "includenan") * mx-inlines.cc: Rework the logic of nan-handling as described above. * max.cc: Add BISTs
1 parent 07d42c8 commit f4156f6

File tree

2 files changed

+54
-188
lines changed

2 files changed

+54
-188
lines changed

libinterp/corefcn/max.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,19 @@ is real or complex. For elements with equal magnitude, a second comparison by
11081108
%!assert (min ([1i 2 -3 4]), 1i)
11091109
%!assert (min ([-2+i, 2-i]), 2-1i)
11101110
1111+
## Test nanflag with dense arrays
1112+
%!test
1113+
%! [m,i] = min ([1,2,3;4,3,NaN;4,5,6], [], 2, "includenan");
1114+
%! assert (m, [1; NaN; 4]);
1115+
%! assert (i, [1; 3; 1]);
1116+
%! [m,i] = min ([1,2,3;4,NaN,NaN;4,5,6], [], 2, "includenan");
1117+
%! assert (m, [1; NaN; 4]);
1118+
%! assert (i, [1; 2; 1]);
1119+
%!test
1120+
%! x = magic (3);
1121+
%! x(2, 3) = NaN;
1122+
%! assert (min (x, [], 2, "includenan"), [1; NaN; 2]);
1123+
11111124
## Test input validation
11121125
%!error min ()
11131126
%!error min (1, 2, 3, 4)
@@ -1403,6 +1416,19 @@ is real or complex. For elements with equal magnitude, a second comparison by
14031416
%!assert (min ([1i 2 -3 4]), 1i)
14041417
%!assert (min ([-2+i, 2-i]), 2-1i)
14051418
1419+
## Test nanflag with dense arrays
1420+
%!test
1421+
%! [m,i] = max ([1,2,3;4,3,NaN;4,5,6], [], 2, "includenan");
1422+
%! assert (m, [3; NaN; 6]);
1423+
%! assert (i, [3; 3; 3]);
1424+
%! [m,i] = max ([1,2,3;4,NaN,NaN;4,5,6], [], 2, "includenan");
1425+
%! assert (m, [3; NaN; 6]);
1426+
%! assert (i, [3; 2; 3]);
1427+
%!test
1428+
%! x = magic (3);
1429+
%! x(2, 3) = NaN;
1430+
%! assert (max (x, [], 2, "includenan"), [8; NaN; 9]);
1431+
14061432
## Test input validation
14071433
%!error max ()
14081434
%!error max (1, 2, 3, 4)

0 commit comments

Comments
 (0)