From: Quentin S. <qsp...@ie...> - 2005-11-07 17:45:17
|
William Poetra Yoga H wrote: >--- Andy Adler <ad...@nc...> wrote: > > >>Are you happy with the way this function will handle large >>exponents where the interger part is lost? For example >> >>octave:6> isinteger(1e10/3) >>ans = 0 >>octave:7> isinteger(1e20/3) >>ans = 1 >> >>Andy >> >> >> > >Well... I'm not. But I can't think of a better way... >Btw, actually 1e20/3 should be treated as an integer anyway (or a floating >point with zero behind the decimal point) because Octave can't handle it. I >meant to use this function to check for integer count arguments passed to >functions, and I think most people would do the same thing, so... > >In the meantime, I've modified it a bit (thanks Paul): > >function retval = isinteger(A) >## -*- texinfo -*- >## @deftypefn {Function File} {} isinteger (@var{A}) >## Return 1 if @var{A} is an integer array. Otherwise, return 0. >## @end deftypefn >## >## @seealso{isa, isnumeric, isfloat} > >if (nargin ~= 1) > usage("isinteger(A)"); >end > >retval = ismatrix(A) && all((round(A) == A)(:)); > >And now I also have an isfloat function: > >function retval = isfloat(A) >## -*- texinfo -*- >## @deftypefn {Function File} {} isfloat (@var{A}) >## Return 1 if @var{A} is a floating point array. Otherwise, return 0. >## @end deftypefn >## >## @seealso{isa, isinteger, isnumeric} > >if (nargin ~= 1) > usage("isfloat(A)"); >end > >retval = ismatrix(A) && ~all((round(A) == A)(:)); > > > > If we're aiming for compatibility with the Matlab functions isinteger and isfloat, we should look at what they do: >> isinteger(1) ans = 0 >> isinteger(int8(1)) ans = 1 So, the outputs of these functions are based not on whether the contents of the array happen to be integers, which can happen with a double array, but on whether they are _stored_ as integers. In addition to the compatibility issue, checking all elements of an array to see if they are an integer could be much slower with a large array than just checking the array type. Given this, it would seem that the correct implementation of isinteger would involve parsing the output of the typeinfo function. -Quentin |