apache/cassandra · error · NumberFormatException

An infinite number cannot be converted into a decimal

Error message

An infinite number cannot be converted into a decimal

What it means

DecimalType.toBigDecimal also rejects infinite values: BigDecimal has no representation for +/-infinity, so a Number whose doubleValue() is infinite raises this NumberFormatException from any decimal arithmetic operation (add/substract/multiply/mod, etc.).

Solutions

  1. Check Double.isInfinite(d) before calling the arithmetic methods
  2. Clamp or reject out-of-range values at ingestion
  3. Fix the upstream computation (guard denominators, use BigDecimal throughout)

Example fix

// before
return decimalType.multiply(a, b);
// after
if (Double.isInfinite(a.doubleValue()) || Double.isInfinite(b.doubleValue()))
    throw new IllegalArgumentException("operands must be finite");
return decimalType.multiply(a, b);
Defensive patterns

Strategy: type-guard

Validate before calling

if (Double.isInfinite(x.doubleValue()) || Double.isInfinite(y.doubleValue())) throw new IllegalArgumentException("infinite operand not allowed in decimal arithmetic");

Type guard

boolean isFiniteNumber(Number n) { double d = n.doubleValue(); return !Double.isNaN(d) && !Double.isInfinite(d); }

Try / catch

try { return DecimalType.instance.multiply(l, r); } catch (NumberFormatException e) { if (e.getMessage().contains("infinite")) { /* handle infinite operand */ } throw e; }

Prevention

When it happens

Trigger: Passing Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY (e.g. from 1.0/0.0 or overflow of a double computation) into DecimalType.add/substract/multiply/mod/leftOperand/rightOperand.

Common situations: Division by zero upstream producing infinity; overflow of double-valued intermediates then combined with decimals; importing JSON with Infinity tokens.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/02af88aba79983b0. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/db/marshal/DecimalType.java:355

     *
     * @param number the value to convert
     * @return the converted value
     */
    protected BigDecimal toBigDecimal(Number number)
    {
        if (number instanceof BigDecimal)
            return (BigDecimal) number;

        if (number instanceof BigInteger)
            return new BigDecimal((BigInteger) number);

        double d = number.doubleValue();

        if (Double.isNaN(d))
            throw new NumberFormatException("A NaN cannot be converted into a decimal");

        if (Double.isInfinite(d))
            throw new NumberFormatException("An infinite number cannot be converted into a decimal");

        return BigDecimal.valueOf(d);
    }

    @Override
    public ByteBuffer add(Number left, Number right)
    {
        return decompose(toBigDecimal(left).add(toBigDecimal(right), MAX_PRECISION));
    }

    @Override
    public ByteBuffer substract(Number left, Number right)
    {
        return decompose(toBigDecimal(left).subtract(toBigDecimal(right), MAX_PRECISION));
    }

    @Override
    public ByteBuffer multiply(Number left, Number right)

View on GitHub (pinned to 88fd0f6a0e)