decimal :: Decimal :: Class Decimal
[hide private]
[frames] | no frames]

Class Decimal

object --+
         |
        Decimal

Floating point class for decimal arithmetic.

Instance Methods [hide private]
 
__abs__(self, round=True, context=None)
Returns the absolute value of self.
 
__add__(self, other, context=None)
Returns self + other.
 
__complex__(self)
 
__copy__(self)
 
__deepcopy__(self, memo)
 
__div__(self, other, context=None)
Return self / other.
 
__divmod__(self, other, context=None)
Return (self // other, self % other)
 
__eq__(self, other, context=None)
 
__float__(self)
Float representation.
 
__floordiv__(self, other, context=None)
self // other
 
__format__(self, specifier, context=None, _localeconv=None)
Format a Decimal instance according to the given specifier.
 
__ge__(self, other, context=None)
 
__gt__(self, other, context=None)
 
__hash__(x)
hash(x)
 
__int__(self)
Converts self to an int, truncating if necessary.
 
__le__(self, other, context=None)
 
__long__(self)
Converts to a long.
 
__lt__(self, other, context=None)
 
__mod__(self, other, context=None)
self % other
 
__mul__(self, other, context=None)
Return self * other.
 
__ne__(self, other, context=None)
 
__neg__(self, context=None)
Returns a copy with the sign switched.
 
__nonzero__(self)
Return True if self is nonzero; otherwise return False.
 
__pos__(self, context=None)
Returns a copy, unless it is a sNaN.
 
__pow__(self, other, modulo=None, context=None)
Return self ** other [ % modulo].
 
__radd__(self, other, context=None)
Returns self + other.
 
__rdiv__(self, other, context=None)
Swaps self/other and returns __truediv__.
 
__rdivmod__(self, other, context=None)
Swaps self/other and returns __divmod__.
 
__reduce__(self)
helper for pickle
 
__repr__(self)
Represents the number as an instance of Decimal.
 
__rfloordiv__(self, other, context=None)
Swaps self/other and returns __floordiv__.
 
__rmod__(self, other, context=None)
Swaps self/other and returns __mod__.
 
__rmul__(self, other, context=None)
Return self * other.
 
__rpow__(self, other, context=None)
Swaps self/other and returns __pow__.
 
__rsub__(self, other, context=None)
Return other - self
 
__rtruediv__(self, other, context=None)
Swaps self/other and returns __truediv__.
 
__str__(self, eng=False, context=None)
Return string representation of the number in scientific notation.
 
__sub__(self, other, context=None)
Return self - other
 
__truediv__(self, other, context=None)
Return self / other.
 
__trunc__(self)
Converts self to an int, truncating if necessary.
 
_check_nans(self, other=None, context=None)
Returns whether the number is not actually one.
 
_cmp(self, other)
Compare the two non-NaN decimal instances self and other.
 
_compare_check_nans(self, other, context)
Version of _check_nans used for the signaling comparisons compare_signal, __le__, __lt__, __ge__, __gt__.
 
_divide(self, other, context)
Return (self // other, self % other), to context.prec precision.
 
_fill_logical(self, context, opa, opb)
 
_fix(self, context)
Round if it is necessary to keep self within prec precision.
 
_fix_nan(self, context)
Decapitate the payload of a NaN to fit the context
 
_iseven(self)
Returns True if self is even.
 
_isinfinity(self)
Returns whether the number is infinite
 
_isinteger(self)
Returns whether self is an integer
 
_islogical(self)
Return True if self is a logical operand.
 
_isnan(self)
Returns whether the number is not actually one.
 
_ln_exp_bound(self)
Compute a lower bound for the adjusted exponent of self.ln().
 
_log10_exp_bound(self)
Compute a lower bound for the adjusted exponent of self.log10().
 
_power_exact(self, other, p)
Attempt to compute self**other exactly.
 
_power_modulo(self, other, modulo, context=None)
Three argument version of __pow__
 
_rescale(self, exp, rounding)
Rescale self so that the exponent is exp, either by padding with zeros or by truncating digits, using the given rounding mode.
 
_round(self, places, rounding)
Round a nonzero, nonspecial Decimal to a fixed number of significant figures, using the given rounding mode.
 
_round_05up(self, prec)
Round down unless digit prec-1 is 0 or 5.
 
_round_ceiling(self, prec)
Rounds up (not away from 0 if negative.)
 
_round_down(self, prec)
Also known as round-towards-0, truncate.
 
_round_floor(self, prec)
Rounds down (not towards 0 if negative)
 
_round_half_down(self, prec)
Round 5 down
 
_round_half_even(self, prec)
Round 5 to even, rest to nearest.
 
_round_half_up(self, prec)
Rounds 5 up (away from 0)
 
_round_up(self, prec)
Rounds away from 0.
 
adjusted(self)
Return the adjusted exponent of self
 
as_tuple(self)
Represents the number as a triple tuple.
 
canonical(self, context=None)
Returns the same Decimal object.
 
compare(self, other, context=None)
Compares one to another.
 
compare_signal(self, other, context=None)
Compares self to the other operand numerically.
 
compare_total(self, other)
Compares self to other using the abstract representations.
 
compare_total_mag(self, other)
Compares self to other using abstract repr., ignoring sign.
 
conjugate(self)
 
copy_abs(self)
Returns a copy with the sign set to 0.
 
copy_negate(self)
Returns a copy with the sign inverted.
 
copy_sign(self, other)
Returns self with the sign of other.
 
exp(self, context=None)
Returns e ** self.
 
fma(self, other, third, context=None)
Fused multiply-add.
 
is_canonical(self)
Return True if self is canonical; otherwise return False.
 
is_finite(self)
Return True if self is finite; otherwise return False.
 
is_infinite(self)
Return True if self is infinite; otherwise return False.
 
is_nan(self)
Return True if self is a qNaN or sNaN; otherwise return False.
 
is_normal(self, context=None)
Return True if self is a normal number; otherwise return False.
 
is_qnan(self)
Return True if self is a quiet NaN; otherwise return False.
 
is_signed(self)
Return True if self is negative; otherwise return False.
 
is_snan(self)
Return True if self is a signaling NaN; otherwise return False.
 
is_subnormal(self, context=None)
Return True if self is subnormal; otherwise return False.
 
is_zero(self)
Return True if self is a zero; otherwise return False.
 
ln(self, context=None)
Returns the natural (base e) logarithm of self.
 
log10(self, context=None)
Returns the base 10 logarithm of self.
 
logb(self, context=None)
Returns the exponent of the magnitude of self's MSD.
 
logical_and(self, other, context=None)
Applies an 'and' operation between self and other's digits.
 
logical_invert(self, context=None)
Invert all its digits.
 
logical_or(self, other, context=None)
Applies an 'or' operation between self and other's digits.
 
logical_xor(self, other, context=None)
Applies an 'xor' operation between self and other's digits.
 
max(self, other, context=None)
Returns the larger value.
 
max_mag(self, other, context=None)
Compares the values numerically with their sign ignored.
 
min(self, other, context=None)
Returns the smaller value.
 
min_mag(self, other, context=None)
Compares the values numerically with their sign ignored.
 
next_minus(self, context=None)
Returns the largest representable number smaller than itself.
 
next_plus(self, context=None)
Returns the smallest representable number larger than itself.
 
next_toward(self, other, context=None)
Returns the number closest to self, in the direction towards other.
 
normalize(self, context=None)
Normalize- strip trailing 0s, change anything equal to 0 to 0e0
 
number_class(self, context=None)
Returns an indication of the class of self.
 
quantize(self, exp, rounding=None, context=None, watchexp=True)
Quantize self so its exponent is the same as that of exp.
 
radix(self)
Just returns 10, as this is Decimal, :)
 
remainder_near(self, other, context=None)
Remainder nearest to 0- abs(remainder-near) <= other/2
 
rotate(self, other, context=None)
Returns a rotated copy of self, value-of-other times.
 
same_quantum(self, other)
Return True if self and other have the same exponent; otherwise return False.
 
scaleb(self, other, context=None)
Returns self operand after adding the second value to its exp.
 
shift(self, other, context=None)
Returns a shifted copy of self, value-of-other times.
 
sqrt(self, context=None)
Return the square root of self.
 
to_eng_string(self, context=None)
Convert to engineering-type string.
 
to_integral(self, rounding=None, context=None)
Rounds to the nearest integer, without raising inexact, rounded.
 
to_integral_exact(self, rounding=None, context=None)
Rounds to a nearby integer.
 
to_integral_value(self, rounding=None, context=None)
Rounds to the nearest integer, without raising inexact, rounded.

Inherited from object: __delattr__, __getattribute__, __init__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Class Methods [hide private]
 
from_float(cls, f)
Converts a float to a decimal number, exactly.
Static Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, value='0', context=None)
Create a decimal point instance.
Class Variables [hide private]
  _pick_rounding_function = {'ROUND_05UP': '_round_05up', 'ROUND...
Properties [hide private]
  _exp
  _int
  _is_special
  _sign
  imag
  real

Inherited from object: __class__

Method Details [hide private]

__abs__(self, round=True, context=None)

 

Returns the absolute value of self.

If the keyword argument 'round' is false, do not round. The expression self.__abs__(round=False) is equivalent to self.copy_abs().

__add__(self, other, context=None)
(Addition operator)

 

Returns self + other.

-INF + INF (or the reverse) cause InvalidOperation errors.

__format__(self, specifier, context=None, _localeconv=None)

 

Format a Decimal instance according to the given specifier.

The specifier should be a standard format specifier, with the form described in PEP 3101. Formatting types 'e', 'E', 'f', 'F', 'g', 'G', 'n' and '%' are supported. If the formatting type is omitted it defaults to 'g' or 'G', depending on the value of context.capitals.

Overrides: object.__format__

__hash__(x)
(Hashing function)

 

hash(x)

Overrides: object.__hash__

__long__(self)

 

Converts to a long.

Equivalent to long(int(self))

__mul__(self, other, context=None)

 

Return self * other.

(+-) INF * 0 (or its reverse) raise InvalidOperation.

__neg__(self, context=None)

 

Returns a copy with the sign switched.

Rounds, if it has reason.

__new__(cls, value='0', context=None)
Static Method

 

Create a decimal point instance.

>>> Decimal('3.14')              # string input
Decimal('3.14')
>>> Decimal((0, (3, 1, 4), -2))  # tuple (sign, digit_tuple, exponent)
Decimal('3.14')
>>> Decimal(314)                 # int or long
Decimal('314')
>>> Decimal(Decimal(314))        # another decimal instance
Decimal('314')
>>> Decimal('  3.14  \n')        # leading and trailing whitespace okay
Decimal('3.14')
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

__nonzero__(self)
(Boolean test operator)

 

Return True if self is nonzero; otherwise return False.

NaNs and infinities are considered nonzero.

__pos__(self, context=None)

 

Returns a copy, unless it is a sNaN.

Rounds the number (if more then precision digits)

__pow__(self, other, modulo=None, context=None)

 

Return self ** other [ % modulo].

With two arguments, compute self**other.

With three arguments, compute (self**other) % modulo. For the three argument form, the following restrictions on the arguments hold:

  • all three arguments must be integral
  • other must be nonnegative
  • either self or other (or both) must be nonzero
  • modulo must be nonzero and must have at most p digits, where p is the context precision.

If any of these restrictions is violated the InvalidOperation flag is raised.

The result of pow(self, other, modulo) is identical to the result that would be obtained by computing (self**other) % modulo with unbounded precision, but is computed more efficiently. It is always exact.

__radd__(self, other, context=None)
(Right-side addition operator)

 

Returns self + other.

-INF + INF (or the reverse) cause InvalidOperation errors.

__reduce__(self)

 

helper for pickle

Overrides: object.__reduce__
(inherited documentation)

__repr__(self)
(Representation operator)

 

Represents the number as an instance of Decimal.

Overrides: object.__repr__

__rmul__(self, other, context=None)

 

Return self * other.

(+-) INF * 0 (or its reverse) raise InvalidOperation.

__str__(self, eng=False, context=None)
(Informal representation operator)

 

Return string representation of the number in scientific notation.

Captures all of the information in the underlying representation.

Overrides: object.__str__

_check_nans(self, other=None, context=None)

 

Returns whether the number is not actually one.

if self, other are sNaN, signal if self, other are NaN return nan return 0

Done before operations.

_cmp(self, other)

 

Compare the two non-NaN decimal instances self and other.

Returns -1 if self < other, 0 if self == other and 1 if self > other. This routine is for internal use only.

_compare_check_nans(self, other, context)

 

Version of _check_nans used for the signaling comparisons compare_signal, __le__, __lt__, __ge__, __gt__.

Signal InvalidOperation if either self or other is a (quiet or signaling) NaN. Signaling NaNs take precedence over quiet NaNs.

Return 0 if neither operand is a NaN.

_divide(self, other, context)

 

Return (self // other, self % other), to context.prec precision.

Assumes that neither self nor other is a NaN, that self is not infinite and that other is nonzero.

_fix(self, context)

 

Round if it is necessary to keep self within prec precision.

Rounds and fixes the exponent. Does not raise on a sNaN.

Arguments: self - Decimal instance context - context used.

_iseven(self)

 

Returns True if self is even. Assumes self is an integer.

_isinfinity(self)

 

Returns whether the number is infinite

0 if finite or not a number 1 if +INF -1 if -INF

_islogical(self)

 

Return True if self is a logical operand.

For being logical, it must be a finite number with a sign of 0, an exponent of 0, and a coefficient whose digits must all be either 0 or 1.

_isnan(self)

 

Returns whether the number is not actually one.

0 if a number 1 if NaN 2 if sNaN

_ln_exp_bound(self)

 

Compute a lower bound for the adjusted exponent of self.ln(). In other words, compute r such that self.ln() >= 10**r. Assumes that self is finite and positive and that self != 1.

_log10_exp_bound(self)

 

Compute a lower bound for the adjusted exponent of self.log10(). In other words, find r such that self.log10() >= 10**r. Assumes that self is finite and positive and that self != 1.

_power_exact(self, other, p)

 

Attempt to compute self**other exactly.

Given Decimals self and other and an integer p, attempt to compute an exact result for the power self**other, with p digits of precision. Return None if self**other is not exactly representable in p digits.

Assumes that elimination of special cases has already been performed: self and other must both be nonspecial; self must be positive and not numerically equal to 1; other must be nonzero. For efficiency, other._exp should not be too large, so that 10**abs(other._exp) is a feasible calculation.

_rescale(self, exp, rounding)

 

Rescale self so that the exponent is exp, either by padding with zeros or by truncating digits, using the given rounding mode.

Specials are returned without change. This operation is quiet: it raises no flags, and uses no information from the context.

exp = exp to scale to (an integer) rounding = rounding mode

_round(self, places, rounding)

 

Round a nonzero, nonspecial Decimal to a fixed number of significant figures, using the given rounding mode.

Infinities, NaNs and zeros are returned unaltered.

This operation is quiet: it raises no flags, and uses no information from the context.

as_tuple(self)

 

Represents the number as a triple tuple.

To show the internals exactly as they are.

canonical(self, context=None)

 

Returns the same Decimal object.

As we do not have different encodings for the same number, the received object already is in its canonical form.

compare(self, other, context=None)

 

Compares one to another.

-1 => a < b 0 => a = b 1 => a > b NaN => one is NaN Like __cmp__, but returns Decimal instances.

compare_signal(self, other, context=None)

 

Compares self to the other operand numerically.

It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs.

compare_total(self, other)

 

Compares self to other using the abstract representations.

This is not like the standard compare, which use their numerical value. Note that a total ordering is defined for all possible abstract representations.

compare_total_mag(self, other)

 

Compares self to other using abstract repr., ignoring sign.

Like compare_total, but with operand's sign ignored and assumed to be 0.

fma(self, other, third, context=None)

 

Fused multiply-add.

Returns self*other+third with no rounding of the intermediate product self*other.

self and other are multiplied together, with no rounding of the result. The third operand is then added to the result, and a single final rounding is performed.

from_float(cls, f)
Class Method

 

Converts a float to a decimal number, exactly.

Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). Since 0.1 is not exactly representable in binary floating point, the value is stored as the nearest representable value which is 0x1.999999999999ap-4. The exact equivalent of the value in decimal is 0.1000000000000000055511151231257827021181583404541015625.

>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> Decimal.from_float(float('nan'))
Decimal('NaN')
>>> Decimal.from_float(float('inf'))
Decimal('Infinity')
>>> Decimal.from_float(-float('inf'))
Decimal('-Infinity')
>>> Decimal.from_float(-0.0)
Decimal('-0')

is_canonical(self)

 

Return True if self is canonical; otherwise return False.

Currently, the encoding of a Decimal instance is always canonical, so this method returns True for any Decimal.

is_finite(self)

 

Return True if self is finite; otherwise return False.

A Decimal instance is considered finite if it is neither infinite nor a NaN.

logb(self, context=None)

 

Returns the exponent of the magnitude of self's MSD.

The result is the integer which is the exponent of the magnitude of the most significant digit of self (as though it were truncated to a single digit while maintaining the value of that digit and without limiting the resulting exponent).

max(self, other, context=None)

 

Returns the larger value.

Like max(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds.

min(self, other, context=None)

 

Returns the smaller value.

Like min(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds.

next_toward(self, other, context=None)

 

Returns the number closest to self, in the direction towards other.

The result is the closest representable number to self (excluding self) that is in the direction towards other, unless both have the same value. If the two operands are numerically equal, then the result is a copy of self with the sign set to be the same as the sign of other.

number_class(self, context=None)

 
Returns an indication of the class of self.

The class is one of the following strings:
  sNaN
  NaN
  -Infinity
  -Normal
  -Subnormal
  -Zero
  +Zero
  +Subnormal
  +Normal
  +Infinity

quantize(self, exp, rounding=None, context=None, watchexp=True)

 

Quantize self so its exponent is the same as that of exp.

Similar to self._rescale(exp._exp) but with error checking.

same_quantum(self, other)

 
Return True if self and other have the same exponent; otherwise
return False.

If either operand is a special value, the following rules are used:
   * return True if both operands are infinities
   * return True if both operands are NaNs
   * otherwise, return False.

to_eng_string(self, context=None)

 

Convert to engineering-type string.

Engineering notation has an exponent which is a multiple of 3, so there are up to 3 digits left of the decimal place.

Same rules for when in exponential and when as a value as in __str__.

to_integral_exact(self, rounding=None, context=None)

 

Rounds to a nearby integer.

If no rounding mode is specified, take the rounding mode from the context. This method raises the Rounded and Inexact flags when appropriate.

See also: to_integral_value, which does exactly the same as this method except that it doesn't raise Inexact or Rounded.


Class Variable Details [hide private]

_pick_rounding_function

Value:
{'ROUND_05UP': '_round_05up',
 'ROUND_CEILING': '_round_ceiling',
 'ROUND_DOWN': '_round_down',
 'ROUND_FLOOR': '_round_floor',
 'ROUND_HALF_DOWN': '_round_half_down',
 'ROUND_HALF_EVEN': '_round_half_even',
 'ROUND_HALF_UP': '_round_half_up',
 'ROUND_UP': '_round_up'}

Property Details [hide private]

imag

Get Method:
unreachable.imag(self)

real

Get Method:
unreachable.real(self)