{"record":{"id":"9bb4705a772605f2","repo":"elastic/elasticsearch","slug":"cannot-apply-not-to-type-double","errorCode":null,"errorMessage":"Cannot apply not [~] to type [double]","messagePattern":"Cannot apply not \\[~\\] to type \\[double\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":48,"sourceCode":"@SuppressWarnings(\"unused\")\npublic class DefMath {\n\n    // Unary not: only applicable to integral types\n\n    private static int not(int v) {\n        return ~v;\n    }\n\n    private static long not(long v) {\n        return ~v;\n    }\n\n    private static float not(float v) {\n        throw new ClassCastException(\"Cannot apply not [~] to type [float]\");\n    }\n\n    private static double not(double v) {\n        throw new ClassCastException(\"Cannot apply not [~] to type [double]\");\n    }\n\n    private static boolean not(boolean v) {\n        throw new ClassCastException(\"Cannot apply not [~] to type [boolean]\");\n    }\n\n    private static Object not(Object unary) {\n        if (unary instanceof Long) {\n            return ~(Long) unary;\n        } else if (unary instanceof Integer) {\n            return ~(Integer) unary;\n        } else if (unary instanceof Short) {\n            return ~(Short) unary;\n        } else if (unary instanceof Character) {\n            return ~(Character) unary;\n        } else if (unary instanceof Byte) {\n            return ~(Byte) unary;\n        }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L30-L66","documentation":"The bitwise NOT operator (~) is only defined for integral types. When DefMath receives a def operand whose runtime type is Double, the double overload throws a ClassCastException immediately because bitwise complement has no defined result on IEEE 754 double-precision values.","triggerScenarios":"A Painless script applies ~ to a def variable holding a Double at runtime. Example: `def x = 3.14; def y = ~x;`","commonSituations":"Applying bitwise NOT to a computed or document-derived floating-point value. Default numeric literals in Painless def mode resolving to Double when mixed with fractional values.","solutions":["Cast the operand to an integral type: `def y = ~(long) x;`","Declare the variable as int or long instead of def.","Guard with instanceof to branch on integral vs floating types."],"exampleFix":"// before\ndef x = doc['ratio'].value;\ndef y = ~x;\n\n// after\ndef x = (long) doc['ratio'].value;\ndef y = ~x;","handlingStrategy":"type-guard","validationCode":"// Painless: cast to long before applying ~ on a double-derived value\ndef x = doc['value'].value;\nif (x instanceof Double) {\n    def y = ~(long) x;\n} else if (x instanceof Long || x instanceof Integer) {\n    def y = ~x;\n}","typeGuard":"// Painless type guard for integral types\ndef isIntegral(def value) {\n    return value instanceof Integer || value instanceof Long || value instanceof Short || value instanceof Byte || value instanceof Character;\n}","tryCatchPattern":null,"preventionTips":["Declare variables intended for bitwise operations as int or long, not def.","Cast floating-point values to long or int before applying the ~ operator.","Use the explicit type system so the compiler rejects ~ on double-typed variables."],"tags":["painless","def-type","bitwise","type-mismatch","arithmetic"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}