{"record":{"id":"6e273344942b73c4","repo":"TheAlgorithms/Python","slug":"exponent-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"Exponent must be a non-negative integer","messagePattern":"Exponent must be a non-negative integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/binary_exponentiation.py","lineNumber":41,"sourceCode":"    >>> binary_exp_recursive(11, 13)\n    34522712143931\n    >>> binary_exp_recursive(-1, 3)\n    -1\n    >>> binary_exp_recursive(0, 5)\n    0\n    >>> binary_exp_recursive(3, 1)\n    3\n    >>> binary_exp_recursive(3, 0)\n    1\n    >>> binary_exp_recursive(1.5, 4)\n    5.0625\n    >>> binary_exp_recursive(3, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Exponent must be a non-negative integer\n    \"\"\"\n    if exponent < 0:\n        raise ValueError(\"Exponent must be a non-negative integer\")\n\n    if exponent == 0:\n        return 1\n\n    if exponent % 2 == 1:\n        return binary_exp_recursive(base, exponent - 1) * base\n\n    b = binary_exp_recursive(base, exponent // 2)\n    return b * b\n\n\ndef binary_exp_iterative(base: float, exponent: int) -> float:\n    \"\"\"\n    Computes a^b iteratively, where a is the base and b is the exponent\n\n    >>> binary_exp_iterative(3, 5)\n    243\n    >>> binary_exp_iterative(11, 13)","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/binary_exponentiation.py#L23-L59","documentation":"binary_exp_recursive(base, exponent) computes base**exponent by repeated squaring and rejects negative exponents with ValueError('Exponent must be a non-negative integer'), because the algorithm (and its int-based halving) has no reciprocal branch.","triggerScenarios":"binary_exp_recursive(3, -1); any negative exponent from modular-inverse style code or from 0-defaulted counters decremented below zero.","commonSituations":"Porting math.pow habits (negative exponents allowed there); computing reciprocals; exponent derived as (a - b) where b can exceed a.","solutions":["Use a nonnegative exponent; for negative powers compute 1 / binary_exp_recursive(base, -exponent) yourself.","Clamp or validate the exponent at the call site.","Use Python's ** operator if you need full negative-exponent support."],"exampleFix":"# before\nresult = binary_exp_recursive(2, -3)\n\n# after\nresult = 1 / binary_exp_recursive(2, 3)  # 0.125","handlingStrategy":"validation","validationCode":"if exponent < 0:\n    result = 1 / binary_exp_recursive(base, -exponent)\nelse:\n    result = binary_exp_recursive(base, exponent)","typeGuard":"def is_nonneg_int(e: object) -> bool:\n    return isinstance(e, int) and e >= 0","tryCatchPattern":null,"preventionTips":["Handle negative exponents by reciprocating before calling","Use the ** operator when full negative-exponent semantics are required"],"tags":["math","exponentiation","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}