Search code examples
matlabfloating-pointintegerfloating-point-precision

How do I filter out floats with integers [logic]


I have an array of floats. Some of these floats are VERY close to integers, but they're still floats. I need to filter these values.

For example:

array_n = [n]; //an array of floats k = 160.

In the if loop, n is an element in array_n. I need to pick n out of the array using k. I've tried doing n%k <= k, but it just didn't work. I tried just filtering out numbers that are close to integers by doing n%1 <= .000000001, but certain values came out to 1 when I did n%1 (I'm really not sure why or how).

Any ideas?

I'm using MATLAB, but I think the problem applies to any language.


Solution

  • let x be the rounded number to the closest integer.

    if abs(x - number) < threshold, then this number is an integer.