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;
source share