Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark empty in a single element in a float array

I have a large float (primitive) array and not every element in the array is filled. How can i mark a particular element as EMPTY. I understand this can be achieved by some special symbols but still i would like to know the standard way. Even if i am using some special symbol , how will i handle a situation where the actual data item is the value of special symbol. In short my question is how to implement the NULL feature in a primitive type array in java.

PS - The reason why i am not using Float object is to achieve a high memory and speed performance.

Thanks Vineeth

like image 365
Vineeth Mohan Avatar asked Jan 14 '23 21:01

Vineeth Mohan


2 Answers

You could use Float.NaN, no valid value will ever be that.

Note that one NaN is never equal to another NaN, so you can check if something is NaN by:

float a = Float.NaN;
if( a != a ) {
    System.out.println("wat?");
}

In code intended for people to read, you should use Float.isNaN method though.

like image 103
Esailija Avatar answered Jan 17 '23 09:01

Esailija


Can you use Float.NaN?

public class FloatArray {
    public static void main(String[] args) {
        float[] data = new float[10];
        data[5] = Float.NaN;
        for (float f : data){
            if (Float.isNaN(f)){
                System.out.println("No Valve");
            } else {
                System.out.println(f);
            }
        }
    }
}

Then where is not risk of using a Sentianl Value like -1 that may be valid and you can test using Float#isNaN(f)

like image 26
GrahamA Avatar answered Jan 17 '23 09:01

GrahamA