elastic/elasticsearch · error · ClassCastException

Cannot apply [>>>] operation to type [float]

Error message

Cannot apply [>>>] operation to type [float]

What it means

Painless throws this ClassCastException at runtime when the unsigned right-shift (>>>) operator is applied to a float value through the def/dynamic path. DefMath.ush(float, long) is a throwing stub: >>> is a bitwise operation and has no defined result on IEEE-754 floats. Only the int and long overloads of ush compute the shift; the float/double/boolean stubs exist to give the dynamic dispatcher a readable failure for every promoted type.

Source

Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java:1052

    public static Object rsh(Object left, long right) {
        if (left instanceof Long) {
            return (long) left >> right;
        } else {
            return intIntegralValue(left) >> right;
        }
    }

    private static int ush(int a, long b) {
        return a >>> b;
    }

    private static long ush(long a, long b) {
        return a >>> b;
    }

    private static float ush(float a, long b) {
        throw new ClassCastException("Cannot apply [>>>] operation to type [float]");
    }

    private static double ush(double a, long b) {
        throw new ClassCastException("Cannot apply [>>>] operation to type [double]");
    }

    private static boolean ush(boolean a, long b) {
        throw new ClassCastException("Cannot apply [>>>] operation to type [boolean]");
    }

    public static Object ush(Object left, long right) {
        if (left instanceof Long) {
            return (long) (left) >>> right;
        } else {
            return intIntegralValue(left) >>> right;
        }
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Cast to int or long before the shift: '((long) x) >>> 2'.
  2. Replace def with an explicit integral type so the compiler catches the mismatch.
  3. If the value is genuinely floating-point, replace the bitwise shift with the appropriate arithmetic equivalent.

Example fix

// before
def x = doc['ratio'].value;  // float field
def y = x >>> 4;
// after
int x = (int) doc['ratio'].value;
int y = x >>> 4;
Defensive patterns

Strategy: type-guard

Validate before calling

// Cast to long before unsigned right shift:
// def x = doc['ratio'].value;
// long y = ((long) x) >>> 4;

Type guard

// boolean isIntegral = x instanceof Integer || x instanceof Long || x instanceof Short || x instanceof Byte;

Prevention

When it happens

Trigger: A def variable holding a float is used with >>>: 'def x = 2.5f; def y = x >>> 2;'. The Object-dispatch ush(Object, long) eventually selects the float stub and throws.

Common situations: Scripts doing bitmask/flag extraction on def-typed values sourced from float-mapped doc fields. Porting bit-twiddling code that assumed integer storage. Runtime fields that produce float results fed into shift logic.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/8cf89a2e818f1f56. Report an issue: GitHub.