🔴 The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
java.lang.ArithmeticException: BigInteger divide by zero
at java.base/java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1271)
at java.base/java.math.BigInteger.divideKnuth(BigInteger.java:2491)
at java.base/java.math.BigInteger.divide(BigInteger.java:2472)⚡ Quick Fix Works 80% of the time
Test the divisor signum before dividing.
if (divisor.signum() != 0) {
quotient = value.divide(divisor);
}🧠 Why this Happens
Tap to expand the deep technical explanation
BigInteger implements division with Knuth Algorithm D over magnitude arrays; a divisor with signum()==0 short-circuits to a dedicated ArithmeticException whose wording differs from the primitive case. mod() additionally requires a POSITIVE modulus ("BigInteger: modulus not positive" for zero/negative), and modInverse() demands gcd(value, modulus)==1, otherwise "BigInteger not invertible.".
The HITEC City Parking Spot Analogy:
Long division by nothing: you cannot place even the first digit of the quotient, so the algorithm refuses to begin.
🔁 How to Reproduce Confirm this is your error
BigInteger ten = BigInteger.TEN; ten.divide(BigInteger.ZERO); // ArithmeticException: BigInteger divide by zero
🛠️ Solutions (5 Ways to Fix)
Check signum() on every divisor
👉 Use this if divisor values derive from user input or parsed data.
signum() returns -1/0/1 and is the canonical emptiness test for BigInteger; guarding once covers divide, remainder and mod alike.
if (m.signum() == 0) throw new IllegalArgumentException("divisor is zero");
BigInteger q = x.divide(m);Precheck invertibility before modInverse
👉 Use this if implementing modular crypto operations.
x.gcd(modulus).equals(BigInteger.ONE) proves an inverse exists; failing that path with a domain message beats leaking ArithmeticException to users.
if (!a.gcd(p).equals(BigInteger.ONE)) {
throw new NotInvertibleException(a + " has no inverse mod " + p);
}
return a.modInverse(p);Validate modulus positivity for mod/modPow
👉 Use this if exponents/moduli come from configuration.
mod() requires strictly positive modulus; checking m.signum() == 1 up front replaces the generic exception with config-level errors.
if (modulus.signum() != 1) throw new ConfigError("modulus must be > 0");Route through gcd for shared-factor problems
👉 Use this if simplifying fractions or normalizing ratios.
x.gcd(y) is always safe (gcd(x,0)=x) and often removes the need to divide by risky values at all.
BigInteger g = a.gcd(b);
return g.signum() == 0 ? ZERO : a.divide(g).multiply(...);Wrap bignum math behind a domain service
👉 Use this if several modules perform arbitrary-precision arithmetic.
One service validates all divisors/moduli and throws typed exceptions with context, keeping ArithmeticException internal.
MathOps.safeDivide(numerator, denominator) // central validation📋 Version Notes
Same messages: "BigInteger divide by zero", "BigInteger: modulus not positive", "BigInteger not invertible."
Added sqrt() which also throws ArithmeticException on negative arguments - same family of exact-math refusals.
🛡️ How to Prevent This Next Time
Never trust parsed strings/config as divisors: signum-check at parse time and keep bignum operations behind validated service methods.