clojure/clojure · error · IllegalArgumentException
Value out of range for float:
Error message
Value out of range for float:
What it means
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.
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.
Example fix
// before
(def f (float x)) ;; throws when x = 1e50
// after
(def f (if (<= (- Float/MAX_VALUE) x Float/MAX_VALUE)
(float x)
(throw (ex-info "cannot represent as float" {:x x})))) Defensive patterns
Strategy: validation
Validate before calling
(defn safe-float-cast [x]
(when (and (number? x) (<= (- Float/MAX_VALUE) x Float/MAX_VALUE))
(float x))) Type guard
(defn float-representable? [x] (and (number? x) (<= (Math/abs (double x)) Float/MAX_VALUE)))
Try / catch
(try (float x)
(catch IllegalArgumentException _
(throw (ex-info "value not representable as float" {:value x})))) Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Value out of range for int:
- Value out of range for long:
- Value out of range for short:
- AbortException
- Accessor/struct mismatch
AI-assisted analysis of clojure/clojure@f3b143341d (2026-09-09).
Data as JSON: /api/errors/a0053d69f12ff71c.
Report an issue: GitHub.
Appendix: source
Thrown at src/jvm/clojure/lang/RT.java:1313
}
static public long longCast(long x){
return x;
}
static public long longCast(double x){
if(x < Long.MIN_VALUE || x > Long.MAX_VALUE)
throw new IllegalArgumentException("Value out of range for long: " + x);
return (long) x;
}
static public float floatCast(Object x){
if(x instanceof Float)
return ((Float) x).floatValue();
double n = ((Number) x).doubleValue();
if(n < -Float.MAX_VALUE || n > Float.MAX_VALUE)
throw new IllegalArgumentException("Value out of range for float: " + x);
return (float) n;
}
static public float floatCast(byte x){
return x;
}
static public float floatCast(short x){
return x;
}
static public float floatCast(int x){
return x;
}
static public float floatCast(float x){View on GitHub (pinned to f3b143341d)