On this page
decimal.Context
Prev: decimal.Clamped | Up: decimal | Next: decimal.ConversionSyntax
Rel: decimal.Clamped, decimal.ConversionSyntax, decimal.Decimal, decimal.DecimalException
Ref: docs.python.org — decimal.Context
Tags: #public #python3 #stdlib #decimal
Import
import decimal
from decimal import Context
Signature & Overview
decimal.Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)
The context affects almost all operations and controls rounding, Over/Underflow, raising of exceptions and much more. A new context can be constructed as follows:
Methods & Attributes
Context.Etiny(self, /)— Return a value equal to Emin - prec + 1, which is the minimum exponent value for subnormal results. When underflow occurs, the exponent is set to Etiny.Context.Etop(self, /)— Return a value equal to Emax - prec + 1. This is the maximum exponent if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop() must not be negative.Context.abs(self, x, /)— Return the absolute value of x.Context.add(self, x, y, /)— Return the sum of x and y.Context.canonical(self, x, /)— Return a new instance of x.Context.clear_flags(self, /)— Reset all flags to False.Context.clear_traps(self, /)— Set all traps to False.Context.compare(self, x, y, /)— Compare x and y numerically.Context.compare_signal(self, x, y, /)— Compare x and y numerically. All NaNs signal.Context.compare_total(self, x, y, /)— Compare x and y using their abstract representation.Context.compare_total_mag(self, x, y, /)— Compare x and y using their abstract representation, ignoring sign.Context.copy(self, /)— Return a duplicate of the context.Context.copy_abs(self, x, /)— Return a copy of x with the sign set to 0.Context.copy_decimal(self, x, /)— Return a copy of Decimal x.Context.copy_negate(self, x, /)— Return a copy of x with the sign inverted.Context.copy_sign(self, x, y, /)— Copy the sign from y to x.Context.create_decimal(self, num='0', /)— Create a new Decimal instance from num, using self as the context. Unlike the Decimal constructor, this function observes the context limits.Context.create_decimal_from_float(self, f, /)— Create a new Decimal instance from float f. Unlike the Decimal.from_float() class method, this function observes the context limits.Context.divide(self, x, y, /)— Return x divided by y.Context.divide_int(self, x, y, /)— Return x divided by y, truncated to an integer.Context.divmod(self, x, y, /)— Return quotient and remainder of the division x / y.Context.exp(self, x, /)— Return e ** x.Context.fma(self, x, y, z, /)— Return x multiplied by y, plus z.Context.is_canonical(self, x, /)— Return True if x is canonical, False otherwise.Context.is_finite(self, x, /)— Return True if x is finite, False otherwise.Context.is_infinite(self, x, /)— Return True if x is infinite, False otherwise.Context.is_nan(self, x, /)— Return True if x is a qNaN or sNaN, False otherwise.Context.is_normal(self, x, /)— Return True if x is a normal number, False otherwise.Context.is_qnan(self, x, /)— Return True if x is a quiet NaN, False otherwise.Context.is_signed(self, x, /)— Return True if x is negative, False otherwise.Context.is_snan(self, x, /)— Return True if x is a signaling NaN, False otherwise.Context.is_subnormal(self, x, /)— Return True if x is subnormal, False otherwise.Context.is_zero(self, x, /)— Return True if x is a zero, False otherwise.Context.ln(self, x, /)— Return the natural (base e) logarithm of x.Context.log10(self, x, /)— Return the base 10 logarithm of x.Context.logb(self, x, /)— Return the exponent of the magnitude of the operand's MSD.Context.logical_and(self, x, y, /)— Applies the logical operation 'and' between each operand's digits.Context.logical_invert(self, x, /)— Invert all the digits in the operand.Context.logical_or(self, x, y, /)— Applies the logical operation 'or' between each operand's digits.Context.logical_xor(self, x, y, /)— Applies the logical operation 'xor' between each operand's digits.
Usage
from decimal import Context, Decimal
ctx = Context(prec=6)
assert str(ctx.divide(Decimal(1), Decimal(3))) == "0.333333"
Gotchas
- Use
localcontext()for temporary precision/rounding changes instead of mutating the thread-global context directly.
REPL Inspect
from decimal import Context
help(Context)
Linked from
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/decimal/Clamped
- tuts/python3/stdlib_module/decimal/ConversionSyntax
- tuts/python3/stdlib_module/decimal/Decimal
- tuts/python3/stdlib_module/decimal/DecimalException
- tuts/python3/stdlib_module/decimal/DecimalTuple
- tuts/python3/stdlib_module/decimal/DivisionByZero
- tuts/python3/stdlib_module/decimal/DivisionImpossible
- tuts/python3/stdlib_module/decimal/DivisionUndefined
- tuts/python3/stdlib_module/decimal/FloatOperation
- tuts/python3/stdlib_module/decimal/getcontext
- tuts/python3/stdlib_module/decimal/IEEEContext
- tuts/python3/stdlib_module/decimal/Inexact
- tuts/python3/stdlib_module/decimal/InvalidContext
- tuts/python3/stdlib_module/decimal/InvalidOperation
- tuts/python3/stdlib_module/decimal/localcontext
- tuts/python3/stdlib_module/decimal/Overflow
- tuts/python3/stdlib_module/decimal/Rounded
- tuts/python3/stdlib_module/decimal/setcontext
- tuts/python3/stdlib_module/decimal/Subnormal
- tuts/python3/stdlib_module/decimal/Underflow
Related notes
- tuts/python3/stdlib_module/decimal/Clamped Linked here; links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/ConversionSyntax Linked here; links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/Decimal Linked here; links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/DecimalException Linked here; links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/DecimalTuple links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/DivisionByZero links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/DivisionImpossible links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/DivisionUndefined links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/FloatOperation links to this page; shared tags: #decimal, #stdlib
- tuts/python3/stdlib_module/decimal/getcontext links to this page; shared tags: #decimal, #stdlib