Package io.github.jbellis.jvector.util
Class NumericUtils
java.lang.Object
io.github.jbellis.jvector.util.NumericUtils
Helper APIs to encode numeric values as sortable bytes and vice-versa.
To also index floating point numbers, this class supplies a method to convert them to
integer values by changing their bit layout: floatToSortableInt(float). You will have no precision loss by converting floating point numbers to
integers and back (only that the integer form is not usable). Other data types like dates can
easily converted to longs or ints (e.g. date to long: Date.getTime()).
-
Method Summary
Modifier and TypeMethodDescriptionstatic intfloatToSortableInt(float value) Converts afloatvalue to a sortable signedint.static intsortableFloatBits(int bits) Converts IEEE 754 representation of a float to sortable order (or back to the original)static floatsortableIntToFloat(int encoded) Converts a sortableintback to afloat.
-
Method Details
-
floatToSortableInt
public static int floatToSortableInt(float value) Converts afloatvalue to a sortable signedint. The value is converted by getting their IEEE 754 floating-point "float format" bit layout and then some bits are swapped, to be able to compare the result as int. By this the precision is not reduced, but the value can easily used as an int. The sort order (includingFloat.NaN) is defined byFloat.compareTo(java.lang.Float);NaNis greater than positive infinity.- See Also:
-
sortableIntToFloat
public static float sortableIntToFloat(int encoded) Converts a sortableintback to afloat.- See Also:
-
sortableFloatBits
public static int sortableFloatBits(int bits) Converts IEEE 754 representation of a float to sortable order (or back to the original)
-