{"record":{"id":"4469e8241f7f2d61","repo":"python/cpython","slug":"cannot-round-an-infinity","errorCode":null,"errorMessage":"cannot round an infinity","messagePattern":"cannot round an infinity","errorType":"exception","errorClass":"OverflowError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":1846,"sourceCode":"        ...     round(Decimal('-Infinity'), 37)\n        ...     round(Decimal('sNaN123'), 0)\n        Decimal('NaN')\n        Decimal('NaN123')\n\n        \"\"\"\n        if n is not None:\n            # two-argument form: use the equivalent quantize call\n            if not isinstance(n, int):\n                raise TypeError('Second argument to round should be integral')\n            exp = _dec_from_triple(0, '1', -n)\n            return self.quantize(exp)\n\n        # one-argument form\n        if self._is_special:\n            if self.is_nan():\n                raise ValueError(\"cannot round a NaN\")\n            else:\n                raise OverflowError(\"cannot round an infinity\")\n        return int(self._rescale(0, ROUND_HALF_EVEN))\n\n    def __floor__(self):\n        \"\"\"Return the floor of self, as an integer.\n\n        For a finite Decimal instance self, return the greatest\n        integer n such that n <= self.  If self is infinite or a NaN\n        then a Python exception is raised.\n\n        \"\"\"\n        if self._is_special:\n            if self.is_nan():\n                raise ValueError(\"cannot round a NaN\")\n            else:\n                raise OverflowError(\"cannot round an infinity\")\n        return int(self._rescale(0, ROUND_FLOOR))\n\n    def __ceil__(self):","sourceCodeStart":1828,"sourceCodeEnd":1864,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L1828-L1864","documentation":"round(Decimal('Infinity')) — one-argument form — raises OverflowError('cannot round an infinity'); there is no nearest integer to an unbounded value. Mirrors __int__/__trunc__ behavior on infinities.","triggerScenarios":"round(Decimal('Infinity')); round(Decimal('-inf')); values that overflowed to infinity earlier (Overflow trap disabled) then rounded for display.","commonSituations":"Formatting computed results for report output where an earlier operation overflowed silently; parsing 'Infinity' from JSON (json.loads accepts it) then rounding.","solutions":["Guard with is_infinite() and render a sentinel like '∞'/'inf' or reject","Fix the source: enable the Overflow trap or raise Context.prec so infinity never appears","Clamp to a domain maximum before rounding when a bounded display is acceptable"],"exampleFix":"// before\nshown = round(v)  # OverflowError when v is Infinity\n\n// after\nshown = 'inf' if v.is_infinite() else round(v)","handlingStrategy":"validation","validationCode":"def safe_round1(d):\n    if d.is_infinite():\n        raise OverflowError(f'cannot round {d}')\n    return round(d)","typeGuard":"def is_roundable(d) -> bool:\n    return not d._is_special","tryCatchPattern":"try:\n    n = round(d)\nexcept OverflowError:\n    n = display_cap  # clamp for display only\nexcept ValueError:\n    n = 0  # NaN policy","preventionTips":["Fix overflow at the source: trap Overflow or increase Context.prec","Guard display/format code against infinities from parsed JSON","Clamp computed aggregates to domain bounds before rounding"],"tags":["decimal","rounding","round","infinity","overflowerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}