Package gnue :: Package common :: Package external :: Module decimal
[show private | hide private]

Module gnue.common.external.decimal

This is a Py2.3 implementation of decimal floating point arithmetic based on
the General Decimal Arithmetic Specification:

    www2.hursley.ibm.com/decimal/decarith.html

and IEEE standard 854-1987:

    www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html

Decimal floating point has finite precision with arbitrarily large bounds.

The purpose of the module is to support arithmetic using familiar
"schoolhouse" rules and to avoid the some of tricky representation
issues associated with binary floating point.  The package is especially
useful for financial applications or for contexts where users have
expectations that are at odds with binary floating point (for instance,
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
of the expected Decimal("0.00") returned by decimal floating point).

Here are some examples of using the decimal module:

>>> from decimal import *
>>> setcontext(ExtendedContext)
>>> Decimal(0)
Decimal("0")
>>> Decimal("1")
Decimal("1")
>>> Decimal("-.0123")
Decimal("-0.0123")
>>> Decimal(123456)
Decimal("123456")
>>> Decimal("123.45e12345678901234567890")
Decimal("1.2345E+12345678901234567892")
>>> Decimal("1.33") + Decimal("1.27")
Decimal("2.60")
>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
Decimal("-2.20")
>>> dig = Decimal(1)
>>> print dig / Decimal(3)
0.333333333
>>> getcontext().prec = 18
>>> print dig / Decimal(3)
0.333333333333333333
>>> print dig.sqrt()
1
>>> print Decimal(3).sqrt()
1.73205080756887729
>>> print Decimal(3) ** 123
4.85192780976896427E+58
>>> inf = Decimal(1) / Decimal(0)
>>> print inf
Infinity
>>> neginf = Decimal(-1) / Decimal(0)
>>> print neginf
-Infinity
>>> print neginf + inf
NaN
>>> print neginf * inf
-Infinity
>>> print dig / 0
Infinity
>>> getcontext().traps[DivisionByZero] = 1
>>> print dig / 0
Traceback (most recent call last):
  ...
  ...
  ...
DivisionByZero: x / 0
>>> c = Context()
>>> c.traps[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal("NaN")
>>> c.traps[InvalidOperation] = 1
>>> print c.flags[InvalidOperation]
1
>>> c.flags[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> print c.divide(Decimal(0), Decimal(0))
Traceback (most recent call last):
  ...
  ...
  ...
InvalidOperation: 0 / 0
>>> print c.flags[InvalidOperation]
1
>>> c.flags[InvalidOperation] = 0
>>> c.traps[InvalidOperation] = 0
>>> print c.divide(Decimal(0), Decimal(0))
NaN
>>> print c.flags[InvalidOperation]
1
>>>

Classes
Context Contains the context for a Decimal instance.
Decimal Floating point class for decimal arithmetic.

Exceptions
Clamped Exponent of a 0 changed to fit bounds.
DecimalException Base exception class.
DivisionByZero Division by 0.
Inexact Had to round, losing information.
InvalidOperation An invalid operation was performed.
Overflow Numerical overflow.
Rounded Number got rounded (not necessarily changed during rounding).
Subnormal Exponent < Emin before rounding.
Underflow Numerical underflow with result rounded to 0.

Function Summary
  getcontext(_local)
Returns this thread's context.
  setcontext(context, _local)
Set this thread's context to context.

Variable Summary
Context BasicContext = Context(prec=9, rounding=ROUND_HALF_UP, E...
Context DefaultContext = Context(prec=28, rounding=ROUND_HALF_EV...
Context ExtendedContext = Context(prec=9, rounding=ROUND_HALF_EV...
str ROUND_CEILING = 'ROUND_CEILING'
str ROUND_DOWN = 'ROUND_DOWN'
str ROUND_FLOOR = 'ROUND_FLOOR'
str ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
str ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
str ROUND_HALF_UP = 'ROUND_HALF_UP'
str ROUND_UP = 'ROUND_UP'

Function Details

getcontext(_local=<thread._local object at 0xb77501e8>)

Returns this thread's context.

If this thread does not yet have a context, returns a new context and sets this thread's context. New contexts are copies of DefaultContext.

setcontext(context, _local=<thread._local object at 0xb77501e8>)

Set this thread's context to context.

Variable Details

BasicContext

Type:
Context
Value:
Context(prec=9, rounding=ROUND_HALF_UP, Emin=-999999999, Emax=99999999\
9, capitals=1, flags=[], traps=[Overflow, InvalidOperation, Underflow,\
 DivisionByZero, Clamped])                                             

DefaultContext

Type:
Context
Value:
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=99999\
9999, capitals=1, flags=[], traps=[Overflow, InvalidOperation, Divisio\
nByZero])                                                              

ExtendedContext

Type:
Context
Value:
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999\
999, capitals=1, flags=[], traps=[])                                   

ROUND_CEILING

Type:
str
Value:
'ROUND_CEILING'                                                        

ROUND_DOWN

Type:
str
Value:
'ROUND_DOWN'                                                           

ROUND_FLOOR

Type:
str
Value:
'ROUND_FLOOR'                                                          

ROUND_HALF_DOWN

Type:
str
Value:
'ROUND_HALF_DOWN'                                                      

ROUND_HALF_EVEN

Type:
str
Value:
'ROUND_HALF_EVEN'                                                      

ROUND_HALF_UP

Type:
str
Value:
'ROUND_HALF_UP'                                                        

ROUND_UP

Type:
str
Value:
'ROUND_UP'                                                             


GNUe Home

Public API

Developer's Corner