apache/flink · error · ArithmeticException

Logarithm of zero is undefined.

Error message

Logarithm of zero is undefined.

What it means

MathUtils.log2floor computes floor(log2(value)) as 31 - Integer.numberOfLeadingZeros(value). That formula is meaningless for 0 (which has no highest set bit), so the method throws ArithmeticException when value == 0 rather than returning garbage. Negative values are not checked and rely on the bit formula's behavior.

Source

Thrown at flink-core/src/main/java/org/apache/flink/util/MathUtils.java:36

package org.apache.flink.util;

/** Collection of simple mathematical routines. */
public final class MathUtils {

    /**
     * Computes the logarithm of the given value to the base of 2, rounded down. It corresponds to
     * the position of the highest non-zero bit. The position is counted, starting with 0 from the
     * least significant bit to the most significant bit. For example, <code>log2floor(16) = 4
     * </code>, and <code>log2floor(10) = 3</code>.
     *
     * @param value The value to compute the logarithm for.
     * @return The logarithm (rounded down) to the base of 2.
     * @throws ArithmeticException Thrown, if the given value is zero.
     */
    public static int log2floor(int value) throws ArithmeticException {
        if (value == 0) {
            throw new ArithmeticException("Logarithm of zero is undefined.");
        }

        return 31 - Integer.numberOfLeadingZeros(value);
    }

    /**
     * Computes the logarithm of the given value to the base of 2. This method throws an error, if
     * the given argument is not a power of 2.
     *
     * @param value The value to compute the logarithm for.
     * @return The logarithm to the base of 2.
     * @throws ArithmeticException Thrown, if the given value is zero.
     * @throws IllegalArgumentException Thrown, if the given value is not a power of two.
     */
    public static int log2strict(int value) throws ArithmeticException, IllegalArgumentException {
        if (value == 0) {
            throw new ArithmeticException("Logarithm of zero is undefined.");
        }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Check the caller: log the value before the call and find why it is 0 (usually a config-derived size).
  2. Clamp inputs to at least 1 when zero is possible: Math.max(1, value).
  3. Fix the underlying memory/size configuration (e.g. taskmanager.memory.* segment or network fractions) so computed sizes are non-zero.
  4. For genuinely empty inputs, short-circuit before the log2 computation instead of calling it.

Example fix

// before
int shift = MathUtils.log2floor(bufferCount); // bufferCount == 0 -> ArithmeticException

// after
if (bufferCount <= 0) {
    throw new IllegalArgumentException("bufferCount must be positive: " + bufferCount);
}
int shift = MathUtils.log2floor(bufferCount);
Defensive patterns

Strategy: validation

Validate before calling

if (value <= 0) {
    throw new IllegalArgumentException("value must be positive, got " + value);
}
int log = MathUtils.log2floor(value);

Prevention

When it happens

Trigger: Calling log2floor(0) — usually because a size, buffer count, or capacity value of 0 was passed into sizing math that assumes at least 1 (e.g. computing segment shifts or table sizes).

Common situations: Memory segment sizing or hash table growth code where a configured buffer/memory size evaluates to 0 bytes; a collection size of 0 feeding capacity math; misconfigured memory options starving a component until its computed size hits zero.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/a48c882b294629f3. Report an issue: GitHub.