{"record":{"id":"30535d38e0e40337","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-float-30535d","errorCode":null,"errorMessage":"Cannot apply [<<] operation to type [float]","messagePattern":"Cannot apply \\[<<\\] operation to type \\[float\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":996,"sourceCode":"            return longIntegralValue(left) | longIntegralValue(right);\n        } else {\n            return intIntegralValue(left) | intIntegralValue(right);\n        }\n    }\n\n    // shift operators, valid for any integral types, but does not promote.\n    // we implement all shifts as long shifts, because the extra bits are ignored anyway.\n\n    private static int lsh(int a, long b) {\n        return a << b;\n    }\n\n    private static long lsh(long a, long b) {\n        return a << b;\n    }\n\n    private static float lsh(float a, long b) {\n        throw new ClassCastException(\"Cannot apply [<<] operation to type [float]\");\n    }\n\n    private static double lsh(double a, long b) {\n        throw new ClassCastException(\"Cannot apply [<<] operation to type [double]\");\n    }\n\n    private static boolean lsh(boolean a, long b) {\n        throw new ClassCastException(\"Cannot apply [<<] operation to type [boolean]\");\n    }\n\n    public static Object lsh(Object left, long right) {\n        if (left instanceof Long) {\n            return (long) (left) << right;\n        } else {\n            return intIntegralValue(left) << right;\n        }\n    }\n","sourceCodeStart":978,"sourceCodeEnd":1014,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L978-L1014","documentation":"Thrown by DefMath.lsh(float, long) which unconditionally throws. When the left operand is statically typed float, the compiler's promote() method yields float.class for the left side, selecting the lsh(float, long) overload. Left-shift is only defined for integral types; the float overload exists for method-table completeness but always fails.","triggerScenarios":"A Painless script applies the << operator where the left operand is statically typed as float. The shift amount (right operand) is promoted to long. The compiler selects lsh(float, long) which unconditionally throws.","commonSituations":"Bit-shift on a float-typed field; float variable used in a shift expression for packing/encoding logic; refactoring integer shift code to operate on float fields; float-mapped runtime field used in bit-manipulation logic.","solutions":["Cast the float left operand to int or long before shifting: int iv = (int) v; int result = iv << shift;","Change the field mapping to an integer type if bit-shifting is the intended use case","Use multiplication (v * Math.pow(2, shift)) if the goal is scaling rather than bit manipulation"],"exampleFix":"// before\nfloat val = doc['code'].value;\nfloat shifted = val << 2;\n\n// after\nint val = (int) doc['code'].value;\nint shifted = val << 2;","handlingStrategy":"type-guard","validationCode":"// Before left-shift on float, cast to int\nfloat val = doc['code'].value;\nint iv = (int) val;\nreturn iv << 2;","typeGuard":"// Check that the left operand is integral before shift\nboolean isShiftable(Object v) {\n  return v instanceof Integer || v instanceof Long || \n         v instanceof Short || v instanceof Byte;\n}","tryCatchPattern":null,"preventionTips":["Never apply << to float-typed variables — cast to int or long first","Change field mapping to integer if bit-shifting is needed","Consider multiplication (v * Math.pow(2, n)) if scaling rather than bit manipulation is the goal","Audit scripts that use << on fields mapped as float"],"tags":["painless","elasticsearch","float","type-error","shift","runtime","left-shift"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}