How to mark empty in one element of a floating-point array

I have a large floating point array (primitive), and not every element of the array is populated. How can I mark a specific item as EMPTY. I understand that this can be achieved with some special characters, but still I would like to know the standard way. Even if I use some special character, how will I handle the situation where the actual data element is the value of the special character. In short, my question is how to implement a NULL function in an array of primitive type in java.

PS - The reason I don't use the Float object is to achieve high memory performance and speed.

Thanks Winit

+4
source share
6 answers

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 not to risk using Sentianl Value as -1, which can be valid, and you can test with Float # isNaN (f)

+3
source

You can use Float.NaN , no real value will ever be that way.

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

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

For human readable code, you should use the Float.isNaN method.

+5
source

One option is to use a huge value like Float.MAX_VALUE .

Another would be to have a Float array, not a Float array, which will allow you to be null .

+1
source

I usually use Float.NaN (not-a-number) for this.

Unlike Float.MAX_VALUE et al, you do not need to specifically test them when you do the math on your arrays. NaNs will remain NaNs, while MAX_VALUE et al may need to be processed using conditional logic.

The only thing to keep in mind is that NaNs are not compared to anything, including themselves.

+1
source

There is a way to have normal floats, infinity, NaN and empty labels in float []. Float has a binary representation (see IEEE-754), you can see it with Float.floatToIntBits (float). NaN "canonical" (see Float API) binary representation - 0x7fc00000. Interestingly, NaN has many binary representations, they are all valid, for example, 0x7ff00000 is also NaN. try it

  int nan1 = 0x7ff00000; int nan2 = 0x7fc00000; float f1 = Float.intBitsToFloat(nan1); float f2 = Float.intBitsToFloat(nan2); System.out.println(Float.isNaN(f1)); System.out.println(Float.isNaN(f2)); 

you will see that both f1 and f2 are NaN.

Therefore, you can use any non-canonical NaN, for example 0x7ff00000, as an empty label. Make sure that when you write floats in your array, NaNs are converted to canonical form. For instance,

  f = (f != f) ? f = Float.NaN : f; 

Note that f! = F is the fastest way to check if float is NaN.

To check if float is an empty label

  isEmptyMark = f != f && Float.floatToRawIntBits(f) == 0x7ff00000; 
0
source

This is one of the main problems with primitives. That the range of values ​​is such that null cannot be meaningfully represented. I recommend switching to Float , otherwise you will need to define a specific range of values ​​and their value.
Example. If -1.0 not a significant number in your domain, you can use this to indicate null .
But I highly recommend switching to an object instead of primitives.

The reason I am not using the Float object is to achieve high memory and speed.

If you do not perform a really heavy amount of boxing / auto-boxing crunches, this will not cause a performance problem.
As always, take your measurements, and if you find that there really is a performance problem, you can opt out of Float to switch with preferences.
Do not optimize too soon

-1
source

All Articles