{"record":{"id":"e06842b459261d4d","repo":"TheAlgorithms/Python","slug":"math-range-error","errorCode":null,"errorMessage":"math range error","messagePattern":"math range error","errorType":"exception","errorClass":"OverflowError","httpStatus":null,"severity":"error","filePath":"maths/gamma.py","lineNumber":98,"sourceCode":"        ...\n    ValueError: math domain error\n    >>> gamma_recursive(-4)\n    Traceback (most recent call last):\n        ...\n    ValueError: math domain error\n    >>> gamma_recursive(172)\n    Traceback (most recent call last):\n        ...\n    OverflowError: math range error\n    >>> gamma_recursive(1.1)\n    Traceback (most recent call last):\n        ...\n    NotImplementedError: num must be an integer or a half-integer\n    \"\"\"\n    if num <= 0:\n        raise ValueError(\"math domain error\")\n    if num > 171.5:\n        raise OverflowError(\"math range error\")\n    elif num - int(num) not in (0, 0.5):\n        raise NotImplementedError(\"num must be an integer or a half-integer\")\n    elif num == 0.5:\n        return math.sqrt(math.pi)\n    else:\n        return 1.0 if num == 1 else (num - 1) * gamma_recursive(num - 1)\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n    num = 1.0\n    while num:\n        num = float(input(\"Gamma of: \"))\n        print(f\"gamma_iterative({num}) = {gamma_iterative(num)}\")\n        print(f\"gamma_recursive({num}) = {gamma_recursive(num)}\")\n        print(\"\\nEnter 0 to exit...\")","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/gamma.py#L80-L116","documentation":"Raised by gamma_recursive in maths/gamma.py when num > 171.5. Gamma(171.625...) exceeds the maximum double-precision float (~1.8e308), so computing it would overflow; the code pre-empts this with OverflowError('math range error'), matching the behavior of math.gamma at the same threshold. The check runs before recursion begins.","triggerScenarios":"Calling gamma_recursive(172) or any num > 171.5, including large half-integers like 172.5. The 'if num > 171.5' branch raises immediately.","commonSituations":"Computing factorials of large numbers via Gamma (n! = Gamma(n+1), so factorials above ~170! overflow); combinatorics or statistics code that evaluates Gamma at unchecked large arguments; migrating from math.gamma and hitting the identical limit.","solutions":["Keep arguments at num <= 171.5; note that for factorial semantics n! = Gamma(n+1), so n must be <= 170.","If you need Gamma of large arguments, work in log space: use math.lgamma(num) instead of gamma_recursive(num).","For ratios of Gammas (common in statistics), rewrite using lgamma differences to avoid overflow entirely."],"exampleFix":"// before\nfrom maths.gamma import gamma_recursive\nlog_gamma = gamma_recursive(200)  # OverflowError\n\n// after\nimport math\nlog_gamma = math.lgamma(200)  # works, returns ln(Gamma(200))","handlingStrategy":"fallback","validationCode":"GAMMA_MAX = 171.5\nif num > GAMMA_MAX:\n    log_val = math.lgamma(num)  # work in log space instead","typeGuard":null,"tryCatchPattern":"try:\n    val = gamma_recursive(num)\nexcept OverflowError:\n    val = math.lgamma(num)  # or handle as 'too large for float'","preventionTips":["For factorials remember n! overflows a double past n=170","Prefer log-Gamma (math.lgamma) in statistics code to avoid overflow entirely"],"tags":["math","gamma","overflow","floating-point"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}