Class NumericUtils

java.lang.Object
org.elasticsearch.xpack.esql.core.util.NumericUtils

public abstract class NumericUtils extends Object
  • Field Details

    • UNSIGNED_LONG_MAX

      public static final BigInteger UNSIGNED_LONG_MAX
    • UNSIGNED_LONG_MAX_AS_DOUBLE

      public static final double UNSIGNED_LONG_MAX_AS_DOUBLE
    • TWOS_COMPLEMENT_BITMASK

      public static final long TWOS_COMPLEMENT_BITMASK
      See Also:
    • LONG_MAX_PLUS_ONE_AS_BIGINTEGER

      public static final BigInteger LONG_MAX_PLUS_ONE_AS_BIGINTEGER
    • LONG_MAX_PLUS_ONE_AS_DOUBLE

      public static final double LONG_MAX_PLUS_ONE_AS_DOUBLE
    • ONE_AS_UNSIGNED_LONG

      public static final long ONE_AS_UNSIGNED_LONG
    • ZERO_AS_UNSIGNED_LONG

      public static final long ZERO_AS_UNSIGNED_LONG
  • Constructor Details

    • NumericUtils

      public NumericUtils()
  • Method Details

    • isUnsignedLong

      public static boolean isUnsignedLong(BigInteger bi)
    • inUnsignedLongRange

      public static boolean inUnsignedLongRange(double d)
    • asUnsignedLong

      public static BigInteger asUnsignedLong(BigInteger bi)
    • asLongUnsigned

      public static long asLongUnsigned(BigInteger ul)
      Converts a BigInteger holding an unsigned_long to its (signed) long representation. There's no checking on the input value, if this is negative or exceeds unsigned_long range -- call isUnsignedLong(BigInteger) if needed.
      Parameters:
      ul - The unsigned_long value to convert.
      Returns:
      The long representation of the unsigned_long.
    • asLongUnsigned

      public static long asLongUnsigned(long ul)
      Converts a long value to an unsigned long stored as a (signed) long.
      Parameters:
      ul - Long value to convert to unsigned long
      Returns:
      The long representation of the converted unsigned long.
    • unsignedLongAsNumber

      public static Number unsignedLongAsNumber(long l)
      Converts an unsigned long value "encoded" into a (signed) long to a Number, holding the "expanded" value. This can be either a Long (if original value fits), or a BigInteger, otherwise.

      An unsigned long is converted to a (signed) long by adding Long.MIN_VALUE (or subtracting "abs"(Long.MIN_VALUE), so that [0, "abs"(MIN_VALUE) + MAX_VALUE] becomes [MIN_VALUE, MAX_VALUE]) before storing the result. When recovering the original value: - if the result is negative, the unsigned long value has been less than Long.MAX_VALUE, so recovering it requires adding the Long.MIN_VALUE back; this is equivalent to 2-complementing it; the function returns a Long; - if the result remained positive, the value was greater than Long.MAX_VALUE, so we need to add that back; the function returns a BigInteger.

      Parameters:
      l - "Encoded" unsigned long.
      Returns:
      Number, holding the "decoded" value.
    • unsignedLongAsLongExact

      public static long unsignedLongAsLongExact(long l)
      Converts an unsigned long value "encoded" into a (signed) long. In case of overflow, an ArithmeticException is thrown.
    • unsignedLongAsBigInteger

      public static BigInteger unsignedLongAsBigInteger(long l)
    • unsignedLongToDouble

      public static double unsignedLongToDouble(long l)
    • unsignedLongAddExact

      public static long unsignedLongAddExact(long x, long y)
    • unsignedLongSubtractExact

      public static long unsignedLongSubtractExact(long x, long y)
    • unsignedLongMultiplyExact

      public static long unsignedLongMultiplyExact(long x, long y)
    • unsignedLongMultiplyHigh

      public static long unsignedLongMultiplyHigh(long x, long y)
    • asFiniteNumber

      public static double asFiniteNumber(double dbl)
      Check if the provided double is both finite and a number (i.e. not Double.NaN).
      Parameters:
      dbl - The double to verify.
      Returns:
      The input value.
      Throws:
      ArithmeticException - if the provided double is either infinite or not a number.
    • saturatingIntValue

      public static int saturatingIntValue(Number n)
      Converts a number to an integer, saturating that integer if the number doesn't fit naturally. That is to say, values greater than Integer.MAX_VALUE yield Integer.MAX_VALUE and values less than Integer.MIN_VALUE yield Integer.MIN_VALUE This function exists because Long::intValue() yields -1 and 0 for Long.MAX_VALUE and Long.MIN_VALUE, respectively.
      Parameters:
      n - the nubmer to convert
      Returns:
      a valid integer