New function to detect floating point integers?

I’m wondering if it would be useful to add a function to Octave that detects when a floating point data type is an integer value.

This is distinct from checking the data type. For checking the data type we have isinteger or the other way around isfloat.

I find many motivating examples in m-files where the code does a check like this

if (x == fix (x))
  ## x is an integer
endif

Although the code is as trivial as

function retval = is_floating_point_integer (x)
  retval = (x == fix (x));
endfunction

I would probably implement it in C++. We did this for some of the very simple functions like isscalar because they are called so frequently that even a trivial m-file is problematic for performance.

In terms of naming, we already have the function flintmax for FLoating point INTeger MAXimum. Hence, I would suggest isflint or isflinteger as a name.

Any comments on whether this is worth doing?

EDIT: if I go ahead and create this function, should it return true for actual integer data types like uint8? My initial thinking is that it should because what the programmer is looking for is the input to be an integer, and that might be because it is of integer data type or because it is a floating point number that happens to be an integer. If I implement this then maybe isflint isn’t the right name anymore. Maybe something like isint would be better.

1 Like

I agree this is a good thing to do.

In C++ code we also have things like

octave::math::x_nint (val) == val

so it might be good to also replace those with a similar function.

1 Like