You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is often useful to convert a floating point values to integers in a way that preserves ordering. For example, when converting to a sortable binary key for LevelDB, storing in a SQL database that doesn't handle NaN or infinity, etc.
This trick doesn't seem to be well-publicized, but is used by Lucene, etc.
/** * Converts a double value to a sortable long. The value is converted by swapping * some bits in the IEEE 754 layout to be able to compare the result as a long. * * @see #sortableLongToDouble(long) */publicstaticlongdoubleToSortableLong(doublevalue)
{
longbits = doubleToLongBits(value);
returnbits ^ ((bits >> 63) & Long.MAX_VALUE);
}
/** * Converts a sortable long to double. * * @see #doubleToSortableLong(double) */publicstaticdoublesortableLongToDouble(longvalue)
{
value ^= (value >> 63) & Long.MAX_VALUE;
returnlongBitsToDouble(value);
}
/** * Converts a float value to a sortable int. The value is converted by swapping * some bits in the IEEE 754 layout to be able to compare the result as a int. * * @see #sortableIntToFloat(int) */publicstaticintfloatToSortableInt(floatvalue)
{
intbits = floatToIntBits(value);
returnbits ^ ((bits >> 31) & Integer.MAX_VALUE);
}
/** * Converts a sortable int to float. * * @see #floatToSortableInt(float) */publicstaticfloatsortableIntToFloat(intvalue)
{
value ^= (value >> 31) & Integer.MAX_VALUE;
returnintBitsToFloat(value);
}
The text was updated successfully, but these errors were encountered:
It is often useful to convert a floating point values to integers in a way that preserves ordering. For example, when converting to a sortable binary key for LevelDB, storing in a SQL database that doesn't handle NaN or infinity, etc.
This trick doesn't seem to be well-publicized, but is used by Lucene, etc.
The text was updated successfully, but these errors were encountered: