{"record":{"id":"7c09716efd49d571","repo":"clojure/clojure","slug":"value-out-of-range-for-long","errorCode":null,"errorMessage":"Value out of range for long: ","messagePattern":"Value out of range for long: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/jvm/clojure/lang/RT.java","lineNumber":1259,"sourceCode":"\treturn Math.toIntExact(x);\n}\n\nstatic public int intCast(double x){\n\tif(x < Integer.MIN_VALUE || x > Integer.MAX_VALUE)\n\t\tthrow new IllegalArgumentException(\"Value out of range for int: \" + x);\n\treturn (int) x;\n}\n\nstatic public long longCast(Object x){\n\tif(x instanceof Integer || x instanceof Long)\n\t\treturn ((Number) x).longValue();\n\telse if (x instanceof BigInt)\n\t\t{\n\t\tBigInt bi = (BigInt) x;\n\t\tif(bi.bipart == null)\n\t\t\treturn bi.lpart;\n\t\telse\n\t\t\tthrow new IllegalArgumentException(\"Value out of range for long: \" + x);\n\t\t}\n\telse if (x instanceof BigInteger)\n\t\t{\n\t\tBigInteger bi = (BigInteger) x;\n\t\tif(bi.bitLength() < 64)\n\t\t\treturn bi.longValue();\n\t\telse\n\t\t\tthrow new IllegalArgumentException(\"Value out of range for long: \" + x);\n\t\t}\n\telse if (x instanceof Byte || x instanceof Short)\n\t    return ((Number) x).longValue();\n\telse if (x instanceof Ratio)\n\t    return longCast(((Ratio)x).bigIntegerValue());\n\telse if (x instanceof Character)\n\t    return longCast(((Character) x).charValue());\n\telse\n\t    return longCast(((Number)x).doubleValue());\n}","sourceCodeStart":1241,"sourceCodeEnd":1277,"githubUrl":"https://github.com/clojure/clojure/blob/f3b143341d6efc6428b523b2eaa099a6cc99156e/src/jvm/clojure/lang/RT.java#L1241-L1277","documentation":"RT.longCast(Object) throws IllegalArgumentException when a clojure.lang.BigInt has a non-null bipart (value exceeds 64 bits), meaning it cannot be represented as a long. This occurs during (long x) coercion of arbitrary-precision numbers.","triggerScenarios":"RT.longCast(Object) on a BigInt whose bipart != null, i.e. a number produced by (bigint ...) or huge arithmetic that exceeds Long.MIN_VALUE..Long.MAX_VALUE, coerced via (long x).","commonSituations":"Factorials, exponents, cryptography big numbers narrowed to long; BigInt literals from parsing that exceed 2^63-1; promoting code from long math to bigint then accidentally casting back.","solutions":["Keep the value as BigInt/BigInteger instead of casting to long.","Check the magnitude first: (when (< Long/MIN_VALUE n Long/MAX_VALUE) (long n)).","Use mod/remainder to reduce the value into long range when only a residue is needed.","Fix accidental bigint promotion (e.g. from +' or big literals) if long was intended."],"exampleFix":"// before\n(long (factorial 25)) ; exceeds long, throws\n// after\n(let [n (factorial 25)]\n  (if (< n 9223372036854775807)\n    (long n)\n    n)) ; keep as BigInt","handlingStrategy":"validation","validationCode":"(defn fits-long? [^clojure.lang.BigInt bi]\n  (nil? (.bipart bi)))","typeGuard":"(defn ->long [bi] (when (fits-long? bi) (long bi)))","tryCatchPattern":"(try\n  (long big-val)\n  (catch IllegalArgumentException _\n    big-val)) ; keep arbitrary precision","preventionTips":["Avoid casting bigint results of factorial/exponent-style math to long","Model IDs/keys that can exceed 2^63 as BigInteger end-to-end","Use mod/remainder when only a residue in long range is needed"],"tags":["clojure","numeric-cast","bigint","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"f3b143341d6efc6428b523b2eaa099a6cc99156e","analyzedAt":"2026-09-09T12:04:58.961Z","contentChangedAt":"2026-09-09T12:04:58.961Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}