From bug-request at octave dot org Tue Jan 18 20:09:34 2005 Subject: ANY(NaN) should return 0 instead of 1 From: "John W. Eaton" To: Schloegl Alois Cc: octave bug mailing list Date: Tue, 18 Jan 2005 21:13:23 -0500 On 15-Jan-2005, Schloegl Alois wrote: | Octave 2.1.64 returns 1 for ANY(NaN) and ANY([0,NaN]). | I suggest to change this; ANY(NaN) and ANY([0,NaN]) should return 0. | | Rationale: | ANY(NaN) should be handled like ANY([]) which yields 0. | This would be also consistent with the behavior of ALL(NaN) which returns 1. Please try the following patch. Thanks, jwe liboctave/ChangeLog: 2005-01-18 John W. Eaton * dNDArray.cc (NDArray::any): NaN does not count as a nonzero value. * CNDArray.cc (ComplexNDArray::any): Likewise. src/ChangeLog: 2005-01-18 John W. Eaton * mx-inlines.cc (MX_ND_REDUCTION): Delete RET_ELT_TYPE arg. Change all uses. Use VAL instead of RET_ELT_TYPE when resizing. * ov-complex.h (octave_complex::any): New function. * ov-scalar.h (octave_scalar::any): New function. Index: liboctave/CNDArray.cc =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/CNDArray.cc,v retrieving revision 1.23 diff -u -r1.23 CNDArray.cc --- liboctave/CNDArray.cc 9 Nov 2004 18:31:25 -0000 1.23 +++ liboctave/CNDArray.cc 19 Jan 2005 02:05:29 -0000 at @ -617,7 +617,10 @@ ComplexNDArray::any (int dim) const { MX_ND_ANY_ALL_REDUCTION - (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0)), false); + (MX_ND_ANY_EVAL (elem (iter_idx) != Complex (0, 0) + && ! (lo_ieee_isnan (::real (elem (iter_idx))) + || lo_ieee_isnan (::imag (elem (iter_idx))))), + false); } ComplexNDArray Index: liboctave/dNDArray.cc =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/dNDArray.cc,v retrieving revision 1.23 diff -u -r1.23 dNDArray.cc --- liboctave/dNDArray.cc 9 Nov 2004 18:31:25 -0000 1.23 +++ liboctave/dNDArray.cc 19 Jan 2005 02:05:31 -0000 at @ -609,7 +609,9 @@ boolNDArray NDArray::any (int dim) const { - MX_ND_ANY_ALL_REDUCTION (MX_ND_ANY_EVAL (MX_ND_ANY_EXPR), false); + MX_ND_ANY_ALL_REDUCTION + (MX_ND_ANY_EVAL (elem (iter_idx) != 0 + && ! lo_ieee_isnan (elem (iter_idx))), false); } NDArray Index: liboctave/mx-inlines.cc =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/mx-inlines.cc,v retrieving revision 1.36 diff -u -r1.36 mx-inlines.cc --- liboctave/mx-inlines.cc 5 Aug 2004 13:26:15 -0000 1.36 +++ liboctave/mx-inlines.cc 19 Jan 2005 02:05:31 -0000 at @ -379,7 +379,7 @@ } #define MX_ND_REDUCTION(EVAL_EXPR, END_EXPR, VAL, ACC_DECL, \ - RET_TYPE, RET_ELT_TYPE) \ + RET_TYPE) \ \ RET_TYPE retval; \ \ at @ -457,7 +457,7 @@ int num_iter = (this->numel () / dim_length); \ \ /* Make sure retval has correct dimensions */ \ - retval.resize (dv, RET_ELT_TYPE ()); \ + retval.resize (dv, VAL); \ \ Array iter_idx (dv.length (), 0); \ \ at @ -489,14 +489,14 @@ #define MX_ND_REAL_OP_REDUCTION(ASN_EXPR, INIT_VAL) \ MX_ND_REDUCTION (acc ASN_EXPR, retval.elem (iter_idx) = acc, \ - INIT_VAL, double acc = INIT_VAL, NDArray, double) + INIT_VAL, double acc = INIT_VAL, NDArray) #define MX_ND_COMPLEX_OP_REDUCTION(ASN_EXPR, INIT_VAL) \ MX_ND_REDUCTION (acc ASN_EXPR, retval.elem (iter_idx) = acc, \ - INIT_VAL, Complex acc = INIT_VAL, ComplexNDArray, Complex) + INIT_VAL, Complex acc = INIT_VAL, ComplexNDArray) #define MX_ND_ANY_ALL_REDUCTION(EVAL_EXPR, VAL) \ - MX_ND_REDUCTION (EVAL_EXPR, , VAL, , boolNDArray, bool) + MX_ND_REDUCTION (EVAL_EXPR, , VAL, , boolNDArray) #define MX_ND_CUMULATIVE_OP(RET_TYPE, ACC_TYPE, VAL, OP) \ RET_TYPE retval; \ Index: src/ov-complex.h =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov-complex.h,v retrieving revision 1.27 diff -u -r1.27 ov-complex.h --- src/ov-complex.h 1 Sep 2004 21:10:28 -0000 1.27 +++ src/ov-complex.h 19 Jan 2005 02:05:36 -0000 at @ -32,6 +32,7 @@ #include #include +#include "lo-ieee.h" #include "mx-base.h" #include "oct-alloc.h" #include "str-vec.h" at @ -78,6 +79,13 @@ octave_value do_index_op (const octave_value_list& idx, int resize_ok); + octave_value any (int = 0) const + { + return (scalar != Complex (0, 0) + && ! (lo_ieee_isnan (::real (scalar)) + || lo_ieee_isnan (::imag (scalar)))); + } + bool is_complex_scalar (void) const { return true; } bool is_complex_type (void) const { return true; } Index: src/ov-scalar.h =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov-scalar.h,v retrieving revision 1.35 diff -u -r1.35 ov-scalar.h --- src/ov-scalar.h 1 Oct 2004 19:59:40 -0000 1.35 +++ src/ov-scalar.h 19 Jan 2005 02:05:36 -0000 at @ -32,6 +32,7 @@ #include #include +#include "lo-ieee.h" #include "lo-mappers.h" #include "lo-utils.h" #include "mx-base.h" at @ -77,6 +78,9 @@ idx_vector index_vector (void) const { return idx_vector (scalar); } + octave_value any (int = 0) const + { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } + bool is_real_scalar (void) const { return true; } bool is_real_type (void) const { return true; } ------------------------------------------------------------- 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 -------------------------------------------------------------