Python Glossary Русский

Python 3.14+Numbers & mathfunction

decimal.IEEEContext()

decimal

Creates a decimal context matching an IEEE 754 interchange format (decimal32, decimal64, decimal128) from a bit count that is a multiple of 32.

Syntax

decimal.IEEEContext(bits) -> Context

Added in Python 3.14. The bit count is not the precision: IEEEContext(64) gives prec=16 and IEEEContext(32) gives prec=7. Unlike the default context, its traps are off: division by zero does not raise DivisionByZero but silently yields Infinity and only sets a flag. An argument that is not a multiple of 32 or exceeds 512 raises ValueError.

Examples

import decimal
ctx = decimal.IEEEContext(32)
print(ctx.prec, ctx.divide(1, 3))  # → 7 0.3333333
import decimal
ctx = decimal.IEEEContext(64)
print(ctx.divide(1, 0), bool(ctx.flags[decimal.DivisionByZero]))  # → Infinity True
import decimal
try:
    decimal.IEEEContext(40)
except ValueError as e:
    print(e)  # → argument must be a multiple of 32, with a maximum of 512

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy