PyDocs

Nav

Numeric and Mathematical Modules

Context

decimal

class
  • Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)
Create context.

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:

    >>> c = Context(prec=28, Emin=-425000000, Emax=425000000,
    ...             rounding=ROUND_HALF_EVEN, capitals=1, clamp=1,
    ...             traps=[InvalidOperation, DivisionByZero, Overflow],
    ...             flags=[])
    >>>
Members of Context

a

abs
Return the absolute value of x.
add
Return the sum of x and y.

c

canonical
Return a new instance of x.
capitals
Either 0 or 1 (the default).
clamp
Either 0 (the default) or 1.
clear_flags
Reset all flags to False.
clear_traps
Set all traps to False.
compare
Compare x and y numerically.
compare_signal
Compare x and y numerically.
compare_total
Compare x and y using their abstract representation.
compare_total_mag
Compare x and y using their abstract representation, ignoring sign.
copy
Return a duplicate of the context.
copy_abs
Return a copy of x with the sign set to 0.
copy_decimal
Return a copy of Decimal x.
copy_negate
Return a copy of x with the sign inverted.
copy_sign
Copy the sign from y to x.
create_decimal
Create a new Decimal instance from num, using self as the context.
create_decimal_from_float
Create a new Decimal instance from float f.

d

divide
Return x divided by y.
divide_int
Return x divided by y, truncated to an integer.
divmod
Return quotient and remainder of the division x / y.

e

Emax
Integers specifying the outer limits allowable for exponents.
Emin
Integers specifying the outer limits allowable for exponents.
Etiny
Return a value equal to Emin - prec + 1.
Etop
Return a value equal to Emax - prec + 1.
exp
Return e ** x.

f

flags
Lists of any signals to be set.
fma
Return x multiplied by y, plus z.

i

is_canonical
Return True if x is canonical, False otherwise.
is_finite
Return True if x is finite, False otherwise.
is_infinite
Return True if x is infinite, False otherwise.
is_nan
Return True if x is a qNaN or sNaN, False otherwise.
is_normal
Return True if x is a normal number, False otherwise.
is_qnan
Return True if x is a quiet NaN, False otherwise.
is_signed
Return True if x is negative, False otherwise.
is_snan
Return True if x is a signaling NaN, False otherwise.
is_subnormal
Return True if x is subnormal, False otherwise.
is_zero
Return True if x is a zero, False otherwise.

l

ln
Return the natural (base e) logarithm of x.
log10
Return the base 10 logarithm of x.
logb
Return the exponent of the magnitude of the operand's MSD.
logical_and
Applies the logical operation 'and' between each operand's digits.
logical_invert
Invert all the digits in the operand.
logical_or
Applies the logical operation 'or' between each operand's digits.
logical_xor
Applies the logical operation 'xor' between each operand's digits.

m

max
Compare the values numerically and return the maximum.
max_mag
Compare the values numerically with their sign ignored.
min
Compare the values numerically and return the minimum.
min_mag
Compare the values numerically with their sign ignored.
minus
Minus corresponds to unary prefix minus in Python.
multiply
Return the product of x and y.

n

next_minus
Return the largest representable number smaller than x.
next_plus
Return the smallest representable number larger than x.
next_toward
Return the number closest to x, in the direction towards y.
normalize
Reduce x to its simplest form.
number_class
Return an indication of the class of x.

p

plus
Plus corresponds to the unary prefix plus operator in Python.
power
Compute a**b.
prec
An integer in the range [1, MAX_PREC] that sets the precision for arithmetic operations in the context.

q

quantize
Return a value equal to x (rounded), having the exponent of y.

r

radix
Return 10.
remainder
Return the remainder from integer division.
remainder_near
Return x - y * n.
rotate
Return a copy of x, rotated by y places.
rounding
One of the constants listed in the section Rounding Modes.

s

same_quantum
Return True if the two operands have the same exponent.
scaleb
Return the first operand after adding the second value to its exp.
shift
Return a copy of x, shifted by y places.
sqrt
Square root of a non-negative number to context precision.
subtract
Return the difference between x and y.

t

to_eng_string
Convert a number to a string, using engineering notation.
to_integral_exact
Round to an integer.
to_sci_string
Convert a number to a string using scientific notation.
traps
Lists of any signals to be set.
1 Example
>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')
Decimal('1.2345')
from the Python docs
See Also
No see-alsos for decimal/Context
0 Notes
No notes for Context