{"record":{"id":"a0053d69f12ff71c","repo":"clojure/clojure","slug":"value-out-of-range-for-float","errorCode":null,"errorMessage":"Value out of range for float: ","messagePattern":"Value out of range for float: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/jvm/clojure/lang/RT.java","lineNumber":1313,"sourceCode":"}\n\nstatic public long longCast(long x){\n\treturn x;\n}\n\nstatic public long longCast(double x){\n\tif(x < Long.MIN_VALUE || x > Long.MAX_VALUE)\n\t\tthrow new IllegalArgumentException(\"Value out of range for long: \" + x);\n\treturn (long) x;\n}\n\nstatic public float floatCast(Object x){\n\tif(x instanceof Float)\n\t\treturn ((Float) x).floatValue();\n\n\tdouble n = ((Number) x).doubleValue();\n\tif(n < -Float.MAX_VALUE || n > Float.MAX_VALUE)\n\t\tthrow new IllegalArgumentException(\"Value out of range for float: \" + x);\n\n\treturn (float) n;\n\n}\n\nstatic public float floatCast(byte x){\n    return x;\n}\n\nstatic public float floatCast(short x){\n    return x;\n}\n\nstatic public float floatCast(int x){\n\treturn x;\n}\n\nstatic public float floatCast(float x){","sourceCodeStart":1295,"sourceCodeEnd":1331,"githubUrl":"https://github.com/clojure/clojure/blob/f3b143341d6efc6428b523b2eaa099a6cc99156e/src/jvm/clojure/lang/RT.java#L1295-L1331","documentation":"RT.floatCast(Object) narrows any Number to a Java float. If the number's double value exceeds Float.MAX_VALUE in magnitude, the narrowing would be lossy/infinite, so the runtime throws this IllegalArgumentException rather than producing Float/POSITIVE_INFINITY.","triggerScenarios":"Calling (float x) / RT.floatCast on a Number (Double, BigDecimal, BigInt, Long) whose doubleValue() is outside [-Float.MAX_VALUE, Float.MAX_VALUE], e.g. (float 1e50).","commonSituations":"Converting large doubles or BigInts to float for interop with float-typed Java APIs, math on large values then cast to float, reading oversized numeric fields from data files.","solutions":["Check range before casting: only cast when (<= (- Float/MAX_VALUE) n Float/MAX_VALUE).","Use doubles instead of floats if the magnitude legitimately exceeds float range.","Clamp the value: (float (max (- Float/MAX_VALUE) (min Float/MAX_VALUE n))) if saturation is acceptable.","Catch IllegalArgumentException if out-of-range floats are expected and handle them explicitly."],"exampleFix":"// before\n(def f (float x)) ;; throws when x = 1e50\n// after\n(def f (if (<= (- Float/MAX_VALUE) x Float/MAX_VALUE)\n         (float x)\n         (throw (ex-info \"cannot represent as float\" {:x x}))))","handlingStrategy":"validation","validationCode":"(defn safe-float-cast [x]\n  (when (and (number? x) (<= (- Float/MAX_VALUE) x Float/MAX_VALUE))\n    (float x)))","typeGuard":"(defn float-representable? [x]\n  (and (number? x) (<= (Math/abs (double x)) Float/MAX_VALUE)))","tryCatchPattern":"(try (float x)\n  (catch IllegalArgumentException _\n    (throw (ex-info \"value not representable as float\" {:value x}))))","preventionTips":["Check magnitude against Float/MAX_VALUE before casting","Prefer doubles when values may be large","Clamp intentionally with min/max when saturation is acceptable","Beware products of large doubles overflowing float range"],"tags":["clojure","numeric-cast","overflow","float"],"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"}