clojure/clojure · error · IllegalArgumentException

Value out of range for long:

Error message

Value out of range for long: 

What it means

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.

Solutions

  1. Keep the value as BigInt/BigInteger instead of casting to long.
  2. Check the magnitude first: (when (< Long/MIN_VALUE n Long/MAX_VALUE) (long n)).
  3. Use mod/remainder to reduce the value into long range when only a residue is needed.
  4. Fix accidental bigint promotion (e.g. from +' or big literals) if long was intended.

Example fix

// before
(long (factorial 25)) ; exceeds long, throws
// after
(let [n (factorial 25)]
  (if (< n 9223372036854775807)
    (long n)
    n)) ; keep as BigInt
Defensive patterns

Strategy: validation

Validate before calling

(defn fits-long? [^clojure.lang.BigInt bi]
  (nil? (.bipart bi)))

Type guard

(defn ->long [bi] (when (fits-long? bi) (long bi)))

Try / catch

(try
  (long big-val)
  (catch IllegalArgumentException _
    big-val)) ; keep arbitrary precision

Prevention

When it happens

Trigger: 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).

Common situations: 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.

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


AI-assisted analysis of clojure/clojure@f3b143341d (2026-09-09). Data as JSON: /api/errors/7c09716efd49d571. Report an issue: GitHub.

Appendix: source

Thrown at src/jvm/clojure/lang/RT.java:1259

	return Math.toIntExact(x);
}

static public int intCast(double x){
	if(x < Integer.MIN_VALUE || x > Integer.MAX_VALUE)
		throw new IllegalArgumentException("Value out of range for int: " + x);
	return (int) x;
}

static public long longCast(Object x){
	if(x instanceof Integer || x instanceof Long)
		return ((Number) x).longValue();
	else if (x instanceof BigInt)
		{
		BigInt bi = (BigInt) x;
		if(bi.bipart == null)
			return bi.lpart;
		else
			throw new IllegalArgumentException("Value out of range for long: " + x);
		}
	else if (x instanceof BigInteger)
		{
		BigInteger bi = (BigInteger) x;
		if(bi.bitLength() < 64)
			return bi.longValue();
		else
			throw new IllegalArgumentException("Value out of range for long: " + x);
		}
	else if (x instanceof Byte || x instanceof Short)
	    return ((Number) x).longValue();
	else if (x instanceof Ratio)
	    return longCast(((Ratio)x).bigIntegerValue());
	else if (x instanceof Character)
	    return longCast(((Character) x).charValue());
	else
	    return longCast(((Number)x).doubleValue());
}

View on GitHub (pinned to f3b143341d)