elastic/elasticsearch · error · ClassCastException
Cannot apply [^] operation to type [float]
Error message
Cannot apply [^] operation to type [float]
What it means
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.
Source
Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java:933
if (left instanceof Boolean && right instanceof Boolean) {
return (boolean) left & (boolean) right;
} else if (left instanceof Long || right instanceof Long) {
return longIntegralValue(left) & longIntegralValue(right);
} else {
return intIntegralValue(left) & intIntegralValue(right);
}
}
private static int xor(int a, int b) {
return a ^ b;
}
private static long xor(long a, long b) {
return a ^ b;
}
private static float xor(float a, float b) {
throw new ClassCastException("Cannot apply [^] operation to type [float]");
}
private static double xor(double a, double b) {
throw new ClassCastException("Cannot apply [^] operation to type [float]");
}
private static boolean xor(boolean a, boolean b) {
return a ^ b;
}
private static Object xor(Object left, Object right) {
if (left instanceof Boolean && right instanceof Boolean) {
return (boolean) left ^ (boolean) right;
} else if (left instanceof Long || right instanceof Long) {
return longIntegralValue(left) ^ longIntegralValue(right);
} else {
return intIntegralValue(left) ^ intIntegralValue(right);
}View on GitHub (pinned to db6a809a66)
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
Example fix
// before float a = doc['x'].value; float b = doc['y'].value; float toggled = a ^ b; // after int ia = (int) doc['x'].value; int ib = (int) doc['y'].value; int toggled = ia ^ ib;
Defensive patterns
Strategy: type-guard
Validate before calling
// Before bitwise XOR on float, cast to int float a = doc['x'].value; float b = doc['y'].value; int ia = (int) a; int ib = (int) b; return ia ^ ib;
Type guard
// Check that the value is integral before XOR
boolean isIntegral(Object v) {
return v instanceof Integer || v instanceof Long ||
v instanceof Short || v instanceof Byte;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot apply [&] operation to type [float]
- Cannot apply [|] operation to type [float]
- Cannot convert [{}] to an integral value.
- Cannot apply [<<] operation to type [float]
- Cannot apply [>>] operation to type [float]
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/08c9f32dd52196c5.
Report an issue: GitHub.