{"record":{"id":"cb8baf2c9b349498","repo":"elastic/elasticsearch","slug":"cannot-apply-not-to-type-float","errorCode":null,"errorMessage":"Cannot apply not [~] to type [float]","messagePattern":"Cannot apply not \\[~\\] to type \\[float\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":44,"sourceCode":" * {@code int,long,float,double,boolean,Object}. Operators can throw exceptions if\n * the type is illegal. The {@code Object} type must be a \"generic\" handler that\n * handles all legal types: it must be convertible to every possible legal signature.\n */\n@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) {","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L26-L62","documentation":"The bitwise NOT operator (~) is only defined for integral types (int, long, short, char, byte). DefMath routes def-typed operands to type-specific overloads; when the runtime value is a float, the float overload unconditionally throws a ClassCastException because there is no meaningful bitwise complement for floating-point values.","triggerScenarios":"A Painless script applies the ~ operator to a def variable whose runtime value is a Float. Example: `def x = 1.5f; def y = ~x;`","commonSituations":"A script receives a floating-point value from a document field or computation but applies a bitwise operation intended for integers. Mixing def-typed numeric values where the type is inferred from runtime data.","solutions":["Ensure the operand is an integral type before applying ~.","Cast the def value to int or long explicitly: `def y = ~(int) x;`","Use an explicit type declaration (int, long) instead of def for the variable."],"exampleFix":"// before\ndef x = doc['score'].value;\ndef y = ~x;\n\n// after\ndef x = (int) doc['score'].value;\ndef y = ~x;","handlingStrategy":"type-guard","validationCode":"// Painless: ensure the value is integral before applying ~\ndef x = doc['value'].value;\nif (x instanceof Integer || x instanceof Long) {\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":["Use the logical NOT (!) for boolean values; reserve ~ strictly for integer types.","Declare variables intended for bitwise operations as int or long, not def or float/double.","Cast to int or long explicitly before ~ if the source value may be floating-point."],"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"}