{"record":{"id":"08c9f32dd52196c5","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-float-08c9f3","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":933,"sourceCode":"        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        }\n    }\n\n    private static int xor(int a, int b) {\n        return a ^ b;\n    }\n\n    private static long xor(long a, long b) {\n        return a ^ b;\n    }\n\n    private static float xor(float a, float b) {\n        throw new ClassCastException(\"Cannot apply [^] operation to type [float]\");\n    }\n\n    private static double xor(double a, double b) {\n        throw new ClassCastException(\"Cannot apply [^] operation to type [float]\");\n    }\n\n    private static boolean xor(boolean a, boolean b) {\n        return a ^ b;\n    }\n\n    private static Object xor(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":915,"sourceCodeEnd":951,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L915-L951","documentation":"Thrown by DefMath.xor(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 XOR is only defined for integral and boolean types; the float overload exists to keep the method table complete but always fails.","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 xor(float, float) overload.","commonSituations":"XOR toggle logic on float-typed fields; float variables used in a bitwise XOR expression; refactoring integer XOR code to operate on float fields; float-mapped runtime field used in flag toggling.","solutions":["Cast both float operands to int before applying ^: int ia = (int) a; int ib = (int) b; int result = ia ^ ib;","Change the field mapping to integer if XOR logic is the intended use case","Use conditional logic (a != b) for boolean-like XOR semantics on float values"],"exampleFix":"// before\nfloat a = doc['x'].value;\nfloat b = doc['y'].value;\nfloat toggled = a ^ b;\n\n// after\nint ia = (int) doc['x'].value;\nint ib = (int) doc['y'].value;\nint toggled = ia ^ ib;","handlingStrategy":"type-guard","validationCode":"// Before bitwise XOR 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 integral before XOR\nboolean isIntegral(Object v) {\n  return v instanceof Integer || v instanceof Long || \n         v instanceof Short || v instanceof Byte;\n}","tryCatchPattern":null,"preventionTips":["Never apply XOR to float-typed variables — cast to int first","Change field mapping to integer if XOR toggle logic is needed","For boolean-like XOR semantics, use (a != 0) != (b != 0) instead","Audit scripts for ^ operator usage on float-mapped fields"],"tags":["painless","elasticsearch","float","type-error","bitwise","runtime","xor"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}