{"record":{"id":"a48c882b294629f3","repo":"apache/flink","slug":"logarithm-of-zero-is-undefined","errorCode":null,"errorMessage":"Logarithm of zero is undefined.","messagePattern":"Logarithm of zero is undefined\\.","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/MathUtils.java","lineNumber":36,"sourceCode":"\npackage org.apache.flink.util;\n\n/** Collection of simple mathematical routines. */\npublic final class MathUtils {\n\n    /**\n     * Computes the logarithm of the given value to the base of 2, rounded down. It corresponds to\n     * the position of the highest non-zero bit. The position is counted, starting with 0 from the\n     * least significant bit to the most significant bit. For example, <code>log2floor(16) = 4\n     * </code>, and <code>log2floor(10) = 3</code>.\n     *\n     * @param value The value to compute the logarithm for.\n     * @return The logarithm (rounded down) to the base of 2.\n     * @throws ArithmeticException Thrown, if the given value is zero.\n     */\n    public static int log2floor(int value) throws ArithmeticException {\n        if (value == 0) {\n            throw new ArithmeticException(\"Logarithm of zero is undefined.\");\n        }\n\n        return 31 - Integer.numberOfLeadingZeros(value);\n    }\n\n    /**\n     * Computes the logarithm of the given value to the base of 2. This method throws an error, if\n     * the given argument is not a power of 2.\n     *\n     * @param value The value to compute the logarithm for.\n     * @return The logarithm to the base of 2.\n     * @throws ArithmeticException Thrown, if the given value is zero.\n     * @throws IllegalArgumentException Thrown, if the given value is not a power of two.\n     */\n    public static int log2strict(int value) throws ArithmeticException, IllegalArgumentException {\n        if (value == 0) {\n            throw new ArithmeticException(\"Logarithm of zero is undefined.\");\n        }","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/MathUtils.java#L18-L54","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the caller: log the value before the call and find why it is 0 (usually a config-derived size).","Clamp inputs to at least 1 when zero is possible: Math.max(1, value).","Fix the underlying memory/size configuration (e.g. taskmanager.memory.* segment or network fractions) so computed sizes are non-zero.","For genuinely empty inputs, short-circuit before the log2 computation instead of calling it."],"exampleFix":"// before\nint shift = MathUtils.log2floor(bufferCount); // bufferCount == 0 -> ArithmeticException\n\n// after\nif (bufferCount <= 0) {\n    throw new IllegalArgumentException(\"bufferCount must be positive: \" + bufferCount);\n}\nint shift = MathUtils.log2floor(bufferCount);","handlingStrategy":"validation","validationCode":"if (value <= 0) {\n    throw new IllegalArgumentException(\"value must be positive, got \" + value);\n}\nint log = MathUtils.log2floor(value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp sizes/counts to >= 1 before logarithm math","Validate memory/size configs produce non-zero values","Short-circuit empty inputs before sizing computations"],"tags":["math","logarithm","validation","sizing"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}