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

Class Decimal

object --+
         |
        Decimal

Floating point class for decimal arithmetic.

Instance Methods [hide private]
 
_isnan(self)
Returns whether the number is not actually one.
 
_isinfinity(self)
Returns whether the number is infinite
 
_check_nans(self, other=None, context=None)
Returns whether the number is not actually one.
 
__nonzero__(self)
Is the number non-zero?
 
__cmp__(self, other, context=None)
 
__eq__(self, other)
 
__ne__(self, other)
 
compare(self, other, context=None)
Compares one to another.
 
__hash__(x)
hash(x)
 
as_tuple(self)
Represents the number as a triple tuple.
 
__repr__(self)
Represents the number as an instance of Decimal.
 
__str__(self, eng=0, context=None)
Return string representation of the number in scientific notation.
 
to_eng_string(self, context=None)
Convert to engineering-type string.
 
__neg__(self, context=None)
Returns a copy with the sign switched.
 
__pos__(self, context=None)
Returns a copy, unless it is a sNaN.
 
__abs__(self, round=1, context=None)
Returns the absolute value of self.
 
__add__(self, other, context=None)
Returns self + other.
 
__radd__(self, other, context=None)
Returns self + other.
 
__sub__(self, other, context=None)
Return self + (-other)
 
__rsub__(self, other, context=None)
Return other + (-self)
 
_increment(self, round=1, context=None)
Special case of add, adding 1eExponent
 
__mul__(self, other, context=None)
Return self * other.
 
__rmul__(self, other, context=None)
Return self * other.
 
__div__(self, other, context=None)
Return self / other.
 
__truediv__(self, other, context=None)
Return self / other.
 
_divide(self, other, divmod=0, context=None)
Return a / b, to context.prec precision.
 
__rdiv__(self, other, context=None)
Swaps self/other and returns __div__.
 
__rtruediv__(self, other, context=None)
Swaps self/other and returns __div__.
 
__divmod__(self, other, context=None)
(self // other, self % other)
 
__rdivmod__(self, other, context=None)
Swaps self/other and returns __divmod__.
 
__mod__(self, other, context=None)
self % other
 
__rmod__(self, other, context=None)
Swaps self/other and returns __mod__.
 
remainder_near(self, other, context=None)
Remainder nearest to 0- abs(remainder-near) <= other/2
 
__floordiv__(self, other, context=None)
self // other
 
__rfloordiv__(self, other, context=None)
Swaps self/other and returns __floordiv__.
 
__float__(self)
Float representation.
 
__int__(self)
Converts self to an int, truncating if necessary.
 
__long__(self)
Converts to a long.
 
_fix(self, context)
Round if it is necessary to keep self within prec precision.
 
_fixexponents(self, context)
Fix the exponents and return a copy with the exponent in bounds.
 
_round(self, prec=None, rounding=None, context=None)
Returns a rounded version of self.
 
_round_down(self, prec, expdiff, context)
Also known as round-towards-0, truncate.
 
_round_half_up(self, prec, expdiff, context, tmp=None)
Rounds 5 up (away from 0)
 
_round_half_even(self, prec, expdiff, context)
Round 5 to even, rest to nearest.
 
_round_half_down(self, prec, expdiff, context)
Round 5 down
 
_round_up(self, prec, expdiff, context)
Rounds away from 0.
 
_round_ceiling(self, prec, expdiff, context)
Rounds up (not away from 0 if negative.)
 
_round_floor(self, prec, expdiff, context)
Rounds down (not towards 0 if negative)
 
__pow__(self, n, modulo=None, context=None)
Return self ** n (mod modulo)
 
__rpow__(self, other, context=None)
Swaps self/other and returns __pow__.
 
normalize(self, context=None)
Normalize- strip trailing 0s, change anything equal to 0 to 0e0
 
quantize(self, exp, rounding=None, context=None, watchexp=1)
Quantize self so its exponent is the same as that of exp.
 
same_quantum(self, other)
Test whether self and other have the same exponent.
 
_rescale(self, exp, rounding=None, context=None, watchexp=1)
Rescales so that the exponent is exp.
 
to_integral(self, rounding=None, context=None)
Rounds to the nearest integer, without raising inexact, rounded.
 
sqrt(self, context=None)
Return the square root of self.
 
max(self, other, context=None)
Returns the larger value.
 
min(self, other, context=None)
Returns the smaller value.
 
_isinteger(self)
Returns whether self is an integer
 
_iseven(self)
Returns 1 if self is even.
 
adjusted(self)
Return the adjusted exponent of self
 
__reduce__(self)
helper for pickle
 
__copy__(self)
 
__deepcopy__(self, memo)

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

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_CEILING': '_round_ceiling', ...
Properties [hide private]
  _exp
  _int
  _is_special
  _sign

Inherited from object: __class__

Method Details [hide private]

__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 input (sign, digit_tuple, exponent)
Decimal("3.14")
>>> Decimal(314)                 # int or long
Decimal("314")
>>> Decimal(Decimal(314))        # another decimal instance
Decimal("314")
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

_isnan(self)

 

Returns whether the number is not actually one.

0 if a number 1 if NaN 2 if sNaN

_isinfinity(self)

 

Returns whether the number is infinite

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

_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.

__nonzero__(self)
(Boolean test operator)

 

Is the number non-zero?

0 if self == 0 1 if self != 0

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.

__hash__(x)
(Hashing function)

 

hash(x)

Overrides: object.__hash__

as_tuple(self)

 

Represents the number as a triple tuple.

To show the internals exactly as they are.

__repr__(self)
(Representation operator)

 

Represents the number as an instance of Decimal.

Overrides: object.__repr__

__str__(self, eng=0, 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__

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__.

__neg__(self, context=None)

 

Returns a copy with the sign switched.

Rounds, if it has reason.

__pos__(self, context=None)

 

Returns a copy, unless it is a sNaN.

Rounds the number (if more then precision digits)

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

 

Returns the absolute value of self.

If the second argument is 0, do not round.

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

 

Returns self + other.

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

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

 

Returns self + other.

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

_increment(self, round=1, context=None)

 

Special case of add, adding 1eExponent

Since it is common, (rounding, for example) this adds (sign)*one E self._exp to the number more efficiently than add.

For example: Decimal('5.624e10')._increment() == Decimal('5.625e10')

__mul__(self, other, context=None)

 

Return self * other.

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

__rmul__(self, other, context=None)

 

Return self * other.

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

_divide(self, other, divmod=0, context=None)

 

Return a / b, to context.prec precision.

divmod: 0 => true division 1 => (a //b, a%b) 2 => a //b 3 => a%b

Actually, if divmod is 2 or 3 a tuple is returned, but errors for computing the other value are not raised.

__long__(self)

 

Converts to a long.

Equivalent to long(int(self))

_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.

_fixexponents(self, context)

 

Fix the exponents and return a copy with the exponent in bounds. Only call if known to not be a special value.

_round(self, prec=None, rounding=None, context=None)

 

Returns a rounded version of self.

You can specify the precision or rounding method. Otherwise, the context determines it.

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

 

Return self ** n (mod modulo)

If modulo is None (default), don't take it mod modulo.

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

 

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)

 

Test whether self and other have the same exponent.

same as self._exp == other._exp, except NaN == sNaN

_rescale(self, exp, rounding=None, context=None, watchexp=1)

 

Rescales so that the exponent is exp.

exp = exp to scale to (an integer) rounding = rounding version watchexp: if set (default) an error is returned if exp is greater than Emax or less than Etiny.

sqrt(self, context=None)

 

Return the square root of self.

Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn)) Should quadratically approach the right answer.

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.

_iseven(self)

 

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

__reduce__(self)

 

helper for pickle

Overrides: object.__reduce__
(inherited documentation)

Class Variable Details [hide private]

_pick_rounding_function

Value:
{'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'}