{"record":{"id":"54da15bd7aeb589c","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-float","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":903,"sourceCode":"        } else if (o instanceof Character) {\n            return (char) o;\n        } else {\n            throw new ClassCastException(\"Cannot convert [\" + o.getClass().getCanonicalName() + \"] to an integral value.\");\n        }\n    }\n\n    // bitwise operators: valid only for integral types\n\n    private static int and(int a, int b) {\n        return a & b;\n    }\n\n    private static long and(long a, long b) {\n        return a & b;\n    }\n\n    private static float and(float a, float b) {\n        throw new ClassCastException(\"Cannot apply [&] operation to type [float]\");\n    }\n\n    private static double and(double a, double b) {\n        throw new ClassCastException(\"Cannot apply [&] operation to type [float]\");\n    }\n\n    private static boolean and(boolean a, boolean b) {\n        return a & b;\n    }\n\n    private static Object and(Object left, Object right) {\n        if (left instanceof Boolean && right instanceof Boolean) {\n            return (boolean) left & (boolean) right;\n        } else if (left instanceof Long || right instanceof Long) {\n            return longIntegralValue(left) & longIntegralValue(right);\n        } else {\n            return intIntegralValue(left) & intIntegralValue(right);\n        }","sourceCodeStart":885,"sourceCodeEnd":921,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L885-L921","documentation":"Thrown by DefMath.and(float, float) which unconditionally throws. When both operands are statically typed float, binary promotion yields float.class and the compiler dispatches to this overload. Bitwise AND is only defined for integral types and booleans in Java; Painless mirrors this restriction by providing a float overload that always fails, keeping the method table complete.","triggerScenarios":"A Painless script applies the & operator to two operands both statically typed as float. The promote() method returns float.class for (float, float), selecting the and(float, float) overload which always throws.","commonSituations":"Bitwise mask logic on a field explicitly mapped as float; two float variables used in a bitmask expression; refactoring integer bitmask code to operate on float fields without changing the operator; float-typed runtime field used in a flag/mask computation.","solutions":["Cast both float operands to int before applying &: int ia = (int) a; int ib = (int) b; int result = ia & ib;","Change the field type from float to integer or long if bitmask logic is the intended use case","Use conditional logic instead of bitwise masks for float-valued conditions"],"exampleFix":"// before\nfloat a = doc['x'].value;\nfloat b = doc['y'].value;\nfloat result = a & b;\n\n// after\nint ia = (int) doc['x'].value;\nint ib = (int) doc['y'].value;\nint result = ia & ib;","handlingStrategy":"type-guard","validationCode":"// Before bitwise AND on float, cast to int\nfloat a = doc['x'].value;\nfloat b = doc['y'].value;\nint ia = (int) a;\nint ib = (int) b;\nreturn ia & ib;","typeGuard":"// Check that the value is not float before bitwise AND\nboolean isNotFloatingPoint(Object v) {\n  return !(v instanceof Float) && !(v instanceof Double);\n}","tryCatchPattern":null,"preventionTips":["Never apply bitwise operators to float-typed variables — cast to int first","If bitmask logic is needed, change the field mapping from float to integer","Audit scripts that use bitwise operators on fields mapped as float","Use explicit int declarations instead of float for bitmask variables"],"tags":["painless","elasticsearch","float","type-error","bitwise","runtime","and"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}