From bug-octave-request at bevo dot che dot wisc dot edu Fri Apr 27 13:23:31 2001 Subject: NaN-related bug in SORT From: "John W. Eaton" To: a dot schloegl at ieee dot org Cc: bug-octave at bevo dot che dot wisc dot edu Date: Fri, 27 Apr 2001 13:23:24 -0500 On 27-Apr-2001, Alois =?iso-8859-1?Q?Schl=F6gl?= wrote: | octave:6> sort([3,4,nan,3,4,nan]) | ans = | | 3 4 NaN 3 4 NaN | | Sorry, I've no patch. But I'd suggest that NaN's should be sorted at one | end of the array. IEEE754/854 would suggest to but NaN above +Inf. Please try the following patch. FWIW, I think this is also compatible with the way Matlab works. Thanks, jwe 2001-04-27 John W. Eaton * DLD-FUNCTIONS/sort.cc (mx_sort): Check for NaNs in comparisons. Index: sort.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/DLD-FUNCTIONS/sort.cc,v retrieving revision 1.4 diff -c -r1.4 sort.cc *** sort.cc 2000/01/12 03:07:52 1.4 --- sort.cc 2001/04/27 18:21:43 *************** *** 24,29 **** --- 24,31 ---- #include #endif + #include "lo-mappers.h" + #include "defun-dld.h" #include "error.h" #include "gripes.h" *************** *** 175,181 **** { Array l = create_index_array (nr); ! DO_SORT (nr, (m (p-1, j) > m (q-1, j))); MATRIX_CREATE_RETURN_VALUES (ms, m); } --- 177,183 ---- { Array l = create_index_array (nr); ! DO_SORT (nr, (xisnan (m (p-1, j)) || m (p-1, j) > m (q-1, j))); MATRIX_CREATE_RETURN_VALUES (ms, m); } *************** *** 208,214 **** { Array l = create_index_array (n); ! DO_SORT (n, (v (p-1) > v (q-1))); VECTOR_CREATE_RETURN_VALUES (vs, v); } --- 210,216 ---- { Array l = create_index_array (n); ! DO_SORT (n, (xisnan (v (p-1)) || v (p-1) > v (q-1))); VECTOR_CREATE_RETURN_VALUES (vs, v); } *************** *** 243,258 **** { Array l = create_index_array (nr); ! int all_elts_real = 1; for (int i = 0; i < nr; i++) if (imag (cm (i, j)) != 0.0) { ! all_elts_real = 0; break; } DO_SORT (nr, ((all_elts_real ! && real (cm (p-1, j)) > real (cm (q-1, j))) || abs (cm (p-1, j)) > abs (cm (q-1, j)))); MATRIX_CREATE_RETURN_VALUES (cms, cm); --- 245,262 ---- { Array l = create_index_array (nr); ! bool all_elts_real = true; for (int i = 0; i < nr; i++) if (imag (cm (i, j)) != 0.0) { ! all_elts_real = false; break; } DO_SORT (nr, ((all_elts_real ! && (xisnan (real (cm (p-1, j))) ! || real (cm (p-1, j)) > real (cm (q-1, j)))) ! || xisnan (cm (p-1, j)) || abs (cm (p-1, j)) > abs (cm (q-1, j)))); MATRIX_CREATE_RETURN_VALUES (cms, cm); *************** *** 286,301 **** { Array l = create_index_array (n); ! int all_elts_real = 1; for (int i = 0; i < n; i++) if (imag (cv (i)) != 0.0) { ! all_elts_real = 0; break; } DO_SORT (n, ((all_elts_real ! && real (cv (p-1)) > real (cv (q-1))) || abs (cv (p-1)) > abs (cv (q-1)))); VECTOR_CREATE_RETURN_VALUES (cvs, cv); --- 290,307 ---- { Array l = create_index_array (n); ! bool all_elts_real = true; for (int i = 0; i < n; i++) if (imag (cv (i)) != 0.0) { ! all_elts_real = false; break; } DO_SORT (n, ((all_elts_real ! && (xisnan (real (cv (p-1))) ! || real (cv (p-1)) > real (cv (q-1)))) ! || xisnan (cv (p-1)) || abs (cv (p-1)) > abs (cv (q-1)))); VECTOR_CREATE_RETURN_VALUES (cvs, cv); ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------