{"record":{"id":"031fe139fd501524","repo":"python/cpython","slug":"argument-must-be-a-multiple-of-32-with-a-maximum","errorCode":null,"errorMessage":"argument must be a multiple of 32, with a maximum of {IEEE_CONTEXT_MAX_BITS}","messagePattern":"argument must be a multiple of 32, with a maximum of (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":434,"sourceCode":"    \"\"\"\n    if ctx is None:\n        ctx = getcontext()\n    ctx_manager = _ContextManager(ctx)\n    for key, value in kwargs.items():\n        if key not in _context_attributes:\n            raise TypeError(f\"'{key}' is an invalid keyword argument for this function\")\n        setattr(ctx_manager.new_context, key, value)\n    return ctx_manager\n\n\ndef IEEEContext(bits, /):\n    \"\"\"\n    Return a context object initialized to the proper values for one of the\n    IEEE interchange formats.  The argument must be a multiple of 32 and less\n    than IEEE_CONTEXT_MAX_BITS.\n    \"\"\"\n    if bits <= 0 or bits > IEEE_CONTEXT_MAX_BITS or bits % 32:\n        raise ValueError(\"argument must be a multiple of 32, \"\n                         f\"with a maximum of {IEEE_CONTEXT_MAX_BITS}\")\n\n    ctx = Context()\n    ctx.prec = 9 * (bits//32) - 2\n    ctx.Emax = 3 * (1 << (bits//16 + 3))\n    ctx.Emin = 1 - ctx.Emax\n    ctx.rounding = ROUND_HALF_EVEN\n    ctx.clamp = 1\n    ctx.traps = dict.fromkeys(_signals, False)\n\n    return ctx\n\n\n##### Decimal class #######################################################\n\n# Do not subclass Decimal from numbers.Real and do not register it as such\n# (because Decimals are not interoperable with floats).  See the notes in\n# numbers.py for more detail.","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L416-L452","documentation":"Raised by decimal.IEEEContext(bits) when the requested bit width is not a positive multiple of 32 or exceeds IEEE_CONTEXT_MAX_BITS (999999999 on 64-bit builds). IEEEContext builds a Context matching an IEEE 754 interchange format (32, 64, 128, ... bit), and precision/Emax/Emin are derived from bits//32 and bits//16, so arbitrary widths would produce nonsensical contexts. It is a ValueError thrown before any context is constructed.","triggerScenarios":"IEEEContext(100) (not a multiple of 32); IEEEContext(0) or IEEEContext(-32) (bits <= 0); IEEEContext(10**9) (exceeds IEEE_CONTEXT_MAX_BITS). Valid calls are IEEEContext(32), IEEEContext(64), IEEEContext(128), IEEEContext(256), ...","commonSituations":"Dynamically computing a bit width from user input or a precision setting (e.g. IEEEContext(16 * precision) yielding 48 or 80), or confusing decimal digit precision with IEEE format width (passing 7 for 'single precision' instead of 32).","solutions":["Pass one of the standard interchange widths: 32, 64, 128, 256, ... (all positive multiples of 32)","If the width is computed, round/validate it first: bits = max(32, ((bits + 31) // 32) * 32)","Check against decimal.IEEE_CONTEXT_MAX_BITS before calling if the value can be huge"],"exampleFix":"// before\nctx = IEEEContext(16 * precision)  # precision=4 -> 64 ok, precision=3 -> 48 fails? no: 48 is multiple of 32? no -> ValueError\n\n// after\nbits = max(32, ((16 * precision + 31) // 32) * 32)\nctx = IEEEContext(bits)","handlingStrategy":"validation","validationCode":"import decimal\n\ndef valid_ieee_bits(bits):\n    return (isinstance(bits, int)\n            and bits > 0\n            and bits % 32 == 0\n            and bits <= decimal.IEEE_CONTEXT_MAX_BITS)\n\n# before calling:\n# assert valid_ieee_bits(bits); ctx = decimal.IEEEContext(bits)","typeGuard":"def is_ieee_width(bits) -> bool:\n    return isinstance(bits, int) and 0 < bits <= __import__('decimal').IEEE_CONTEXT_MAX_BITS and bits % 32 == 0","tryCatchPattern":"try:\n    ctx = decimal.IEEEContext(bits)\nexcept ValueError as e:\n    raise ValueError(f'bad IEEE width {bits!r}: use 32/64/128/...') from e","preventionTips":["Hardcode the standard widths (32, 64, 128, 256) as named constants instead of computing them","Treat bit width and decimal-digit precision as different units; never pass prec into IEEEContext","Validate externally supplied widths at the config boundary, not at the call site"],"tags":["decimal","ieee754","context","validation","valueerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}