{"record":{"id":"c961ae2eec96f3e8","repo":"python/cpython","slug":"division-by-zero","errorCode":null,"errorMessage":"division by zero","messagePattern":"division by zero","errorType":"exception","errorClass":"ZeroDivisionError","httpStatus":null,"severity":"error","filePath":"Lib/_pylong.py","lineNumber":533,"sourceCode":"    n = b.bit_length()\n    a_digits = _int2digits(a, n)\n\n    r = 0\n    q_digits = []\n    for a_digit in reversed(a_digits):\n        q_digit, r = _div2n1n((r << n) + a_digit, b, n)\n        q_digits.append(q_digit)\n    q_digits.reverse()\n    q = _digits2int(q_digits, n)\n    return q, r\n\n\ndef int_divmod(a, b):\n    \"\"\"Asymptotically fast replacement for divmod, for 'int'.\n    Its time complexity is O(n**1.58), where n = #bits(a) + #bits(b).\n    \"\"\"\n    if b == 0:\n        raise ZeroDivisionError('division by zero')\n    elif b < 0:\n        q, r = int_divmod(-a, -b)\n        return q, -r\n    elif a < 0:\n        q, r = int_divmod(~a, b)\n        return ~q, b + ~r\n    else:\n        return _divmod_pos(a, b)\n\n\n# Notes on _dec_str_to_int_inner:\n#\n# Stefan Pochmann worked up a str->int function that used the decimal\n# module to, in effect, convert from base 10 to base 256. This is\n# \"unnatural\", in that it requires multiplying and dividing by large\n# powers of 2, which `decimal` isn't naturally suited to. But\n# `decimal`'s `*` and `/` are asymptotically superior to CPython's, so\n# at _some_ point it could be expected to win.","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pylong.py#L515-L551","documentation":"ZeroDivisionError raised by int_divmod in Lib/_pylong.py, the asymptotically fast Python implementation of divmod() for very large ints. CPython delegates divmod on huge integers to this module, and it explicitly guards the b == 0 case with this message. It is the same error you get from the built-in divmod, just thrown from the fast-path module.","triggerScenarios":"divmod(a, b) (or operations like //, % that route through divmod) where b == 0 and the operands are large enough that CPython dispatches to _pylong.int_divmod instead of the C long/long path. Also reachable by directly calling _pylong.int_divmod(a, 0).","commonSituations":"Computing quotients with a divisor computed at runtime (counts, differences, normalization factors) that can be zero; batch/financial/bignum scripts where a denominator is an aggregation that came back empty; fuzz tests hitting _pylong directly.","solutions":["Check the divisor before dividing: if b == 0: handle the degenerate case explicitly instead of calling divmod.","If zero is a legitimate value, decide on semantics (return None, skip the item, or raise your own domain error with context).","If you called _pylong.int_divmod directly, use the built-in divmod which has the same behavior but clearer provenance."],"exampleFix":"// before\nq, r = divmod(total, count)\n\n// after\nif count == 0:\n    raise ValueError(f\"cannot normalize: count is 0 for total={total}\")\nq, r = divmod(total, count)","handlingStrategy":"validation","validationCode":"def safe_divmod(a, b):\n    if b == 0:\n        raise ValueError('divisor must be nonzero')\n    return divmod(a, b)","typeGuard":null,"tryCatchPattern":"try:\n    q, r = divmod(a, b)\nexcept ZeroDivisionError:\n    q, r = 0, a  # only if zero-divisor is semantically OK in your domain","preventionTips":["Treat divisors as validated inputs: assert or check nonzero before any //, %, divmod.","For aggregates (len, sum, count) used as denominators, handle the empty/zero case explicitly.","Unit-test the zero-divisor edge of every function that divides."],"tags":["python","arithmetic","zerodivision","stdlib","bignum"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}