public final class NumericUtils extends Object
To also index floating point numbers, this class supplies two methods to convert them
to integer values by changing their bit layout: doubleToSortableLong(double)
,
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()
).
Modifier and Type | Method and Description |
---|---|
static void |
add(int bytesPerDim,
int dim,
byte[] a,
byte[] b,
byte[] result)
Result = a + b, where a and b are unsigned.
|
static void |
bigIntToSortableBytes(BigInteger bigInt,
int bigIntSize,
byte[] result,
int offset)
Encodes a BigInteger
value such that unsigned byte order comparison
is consistent with BigInteger.compareTo(BigInteger) . |
static long |
doubleToSortableLong(double value)
Converts a
double value to a sortable signed long . |
static int |
floatToSortableInt(float value)
Converts a
float value to a sortable signed int . |
static void |
intToSortableBytes(int value,
byte[] result,
int offset)
Encodes an integer
value such that unsigned byte order comparison
is consistent with Integer.compare(int, int) |
static void |
longToSortableBytes(long value,
byte[] result,
int offset)
Encodes an long
value such that unsigned byte order comparison
is consistent with Long.compare(long, long) |
static BigInteger |
sortableBytesToBigInt(byte[] encoded,
int offset,
int length)
Decodes a BigInteger value previously written with
bigIntToSortableBytes(java.math.BigInteger, int, byte[], int) |
static int |
sortableBytesToInt(byte[] encoded,
int offset)
Decodes an integer value previously written with
intToSortableBytes(int, byte[], int) |
static long |
sortableBytesToLong(byte[] encoded,
int offset)
Decodes a long value previously written with
longToSortableBytes(long, byte[], int) |
static long |
sortableDoubleBits(long bits)
Converts IEEE 754 representation of a double to sortable order (or back to the original)
|
static int |
sortableFloatBits(int bits)
Converts IEEE 754 representation of a float to sortable order (or back to the original)
|
static float |
sortableIntToFloat(int encoded)
Converts a sortable
int back to a float . |
static double |
sortableLongToDouble(long encoded)
Converts a sortable
long back to a double . |
static void |
subtract(int bytesPerDim,
int dim,
byte[] a,
byte[] b,
byte[] result)
Result = a - b, where a >= b, else
IllegalArgumentException is thrown. |
public static long doubleToSortableLong(double value)
double
value to a sortable signed long
.
The value is converted by getting their IEEE 754 floating-point "double format"
bit layout and then some bits are swapped, to be able to compare the result as long.
By this the precision is not reduced, but the value can easily used as a long.
The sort order (including Double.NaN
) is defined by
Double.compareTo(java.lang.Double)
; NaN
is greater than positive infinity.sortableLongToDouble(long)
public static double sortableLongToDouble(long encoded)
long
back to a double
.doubleToSortableLong(double)
public static int floatToSortableInt(float value)
float
value to a sortable signed int
.
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 (including Float.NaN
) is defined by
Float.compareTo(java.lang.Float)
; NaN
is greater than positive infinity.sortableIntToFloat(int)
public static float sortableIntToFloat(int encoded)
int
back to a float
.floatToSortableInt(float)
public static long sortableDoubleBits(long bits)
public static int sortableFloatBits(int bits)
public static void subtract(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)
IllegalArgumentException
is thrown.public static void add(int bytesPerDim, int dim, byte[] a, byte[] b, byte[] result)
IllegalArgumentException
is thrown.public static void intToSortableBytes(int value, byte[] result, int offset)
value
such that unsigned byte order comparison
is consistent with Integer.compare(int, int)
sortableBytesToInt(byte[], int)
public static int sortableBytesToInt(byte[] encoded, int offset)
intToSortableBytes(int, byte[], int)
intToSortableBytes(int, byte[], int)
public static void longToSortableBytes(long value, byte[] result, int offset)
value
such that unsigned byte order comparison
is consistent with Long.compare(long, long)
sortableBytesToLong(byte[], int)
public static long sortableBytesToLong(byte[] encoded, int offset)
longToSortableBytes(long, byte[], int)
longToSortableBytes(long, byte[], int)
public static void bigIntToSortableBytes(BigInteger bigInt, int bigIntSize, byte[] result, int offset)
value
such that unsigned byte order comparison
is consistent with BigInteger.compareTo(BigInteger)
. This also sign-extends
the value to bigIntSize
bytes if necessary: useful to create a fixed-width size.sortableBytesToBigInt(byte[], int, int)
public static BigInteger sortableBytesToBigInt(byte[] encoded, int offset, int length)
bigIntToSortableBytes(java.math.BigInteger, int, byte[], int)
Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.