python-2.5.2/win32/Lib/decimal.py
author jjkang
Fri, 11 Jun 2010 15:22:40 +0800
changeset 2 9da1e5517a66
parent 0 ae805ac0140d
permissions -rw-r--r--
Change SFL to EPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     1
# Copyright (c) 2004 Python Software Foundation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     2
# All rights reserved.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     4
# Written by Eric Price <eprice at tjhsst.edu>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     5
#    and Facundo Batista <facundo at taniquetil.com.ar>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     6
#    and Raymond Hettinger <python at rcn.com>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     7
#    and Aahz <aahz at pobox.com>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     8
#    and Tim Peters
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
     9
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    10
# This module is currently Py2.3 compatible and should be kept that way
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    11
# unless a major compelling advantage arises.  IOW, 2.3 compatibility is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    12
# strongly preferred, but not guaranteed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    13
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    14
# Also, this module should be kept in sync with the latest updates of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    15
# the IBM specification as it evolves.  Those updates will be treated
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    16
# as bug fixes (deviation from the spec is a compatibility, usability
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    17
# bug) and will be backported.  At this point the spec is stabilizing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    18
# and the updates are becoming fewer, smaller, and less significant.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    19
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    20
"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    21
This is a Py2.3 implementation of decimal floating point arithmetic based on
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    22
the General Decimal Arithmetic Specification:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    23
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    24
    www2.hursley.ibm.com/decimal/decarith.html
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    25
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    26
and IEEE standard 854-1987:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    27
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    28
    www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    29
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    30
Decimal floating point has finite precision with arbitrarily large bounds.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    31
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    32
The purpose of this module is to support arithmetic using familiar
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    33
"schoolhouse" rules and to avoid some of the tricky representation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    34
issues associated with binary floating point.  The package is especially
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    35
useful for financial applications or for contexts where users have
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    36
expectations that are at odds with binary floating point (for instance,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    37
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    38
of the expected Decimal("0.00") returned by decimal floating point).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    39
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    40
Here are some examples of using the decimal module:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    41
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    42
>>> from decimal import *
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    43
>>> setcontext(ExtendedContext)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    44
>>> Decimal(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    45
Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    46
>>> Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    47
Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    48
>>> Decimal("-.0123")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    49
Decimal("-0.0123")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    50
>>> Decimal(123456)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    51
Decimal("123456")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    52
>>> Decimal("123.45e12345678901234567890")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    53
Decimal("1.2345E+12345678901234567892")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    54
>>> Decimal("1.33") + Decimal("1.27")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    55
Decimal("2.60")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    56
>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    57
Decimal("-2.20")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    58
>>> dig = Decimal(1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    59
>>> print dig / Decimal(3)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    60
0.333333333
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    61
>>> getcontext().prec = 18
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    62
>>> print dig / Decimal(3)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    63
0.333333333333333333
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    64
>>> print dig.sqrt()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    65
1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    66
>>> print Decimal(3).sqrt()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    67
1.73205080756887729
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    68
>>> print Decimal(3) ** 123
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    69
4.85192780976896427E+58
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    70
>>> inf = Decimal(1) / Decimal(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    71
>>> print inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    72
Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    73
>>> neginf = Decimal(-1) / Decimal(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    74
>>> print neginf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    75
-Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    76
>>> print neginf + inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    77
NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    78
>>> print neginf * inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    79
-Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    80
>>> print dig / 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    81
Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    82
>>> getcontext().traps[DivisionByZero] = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    83
>>> print dig / 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    84
Traceback (most recent call last):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    85
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    86
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    87
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    88
DivisionByZero: x / 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    89
>>> c = Context()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    90
>>> c.traps[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    91
>>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    92
0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    93
>>> c.divide(Decimal(0), Decimal(0))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    94
Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    95
>>> c.traps[InvalidOperation] = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    96
>>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    97
1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    98
>>> c.flags[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
    99
>>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   100
0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   101
>>> print c.divide(Decimal(0), Decimal(0))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   102
Traceback (most recent call last):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   103
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   104
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   105
  ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   106
InvalidOperation: 0 / 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   107
>>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   108
1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   109
>>> c.flags[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   110
>>> c.traps[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   111
>>> print c.divide(Decimal(0), Decimal(0))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   112
NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   113
>>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   114
1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   115
>>>
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   116
"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   117
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   118
__all__ = [
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   119
    # Two major classes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   120
    'Decimal', 'Context',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   121
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   122
    # Contexts
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   123
    'DefaultContext', 'BasicContext', 'ExtendedContext',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   124
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   125
    # Exceptions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   126
    'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   127
    'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   128
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   129
    # Constants for use in setting up contexts
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   130
    'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   131
    'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   132
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   133
    # Functions for manipulating contexts
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   134
    'setcontext', 'getcontext', 'localcontext'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   135
]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   136
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   137
import copy as _copy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   138
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   139
# Rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   140
ROUND_DOWN = 'ROUND_DOWN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   141
ROUND_HALF_UP = 'ROUND_HALF_UP'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   142
ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   143
ROUND_CEILING = 'ROUND_CEILING'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   144
ROUND_FLOOR = 'ROUND_FLOOR'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   145
ROUND_UP = 'ROUND_UP'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   146
ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   147
ROUND_05UP = 'ROUND_05UP'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   148
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   149
# Errors
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   150
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   151
class DecimalException(ArithmeticError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   152
    """Base exception class.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   153
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   154
    Used exceptions derive from this.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   155
    If an exception derives from another exception besides this (such as
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   156
    Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   157
    called if the others are present.  This isn't actually used for
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   158
    anything, though.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   159
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   160
    handle  -- Called when context._raise_error is called and the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   161
               trap_enabler is set.  First argument is self, second is the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   162
               context.  More arguments can be given, those being after
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   163
               the explanation in _raise_error (For example,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   164
               context._raise_error(NewError, '(-x)!', self._sign) would
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   165
               call NewError().handle(context, self._sign).)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   166
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   167
    To define a new exception, it should be sufficient to have it derive
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   168
    from DecimalException.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   169
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   170
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   171
        pass
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   172
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   173
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   174
class Clamped(DecimalException):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   175
    """Exponent of a 0 changed to fit bounds.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   176
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   177
    This occurs and signals clamped if the exponent of a result has been
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   178
    altered in order to fit the constraints of a specific concrete
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   179
    representation.  This may occur when the exponent of a zero result would
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   180
    be outside the bounds of a representation, or when a large normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   181
    number would have an encoded exponent that cannot be represented.  In
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   182
    this latter case, the exponent is reduced to fit and the corresponding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   183
    number of zero digits are appended to the coefficient ("fold-down").
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   184
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   185
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   186
class InvalidOperation(DecimalException):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   187
    """An invalid operation was performed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   188
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   189
    Various bad things cause this:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   190
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   191
    Something creates a signaling NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   192
    -INF + INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   193
    0 * (+-)INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   194
    (+-)INF / (+-)INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   195
    x % 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   196
    (+-)INF % x
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   197
    x._rescale( non-integer )
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   198
    sqrt(-x) , x > 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   199
    0 ** 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   200
    x ** (non-integer)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   201
    x ** (+-)INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   202
    An operand is invalid
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   203
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   204
    The result of the operation after these is a quiet positive NaN,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   205
    except when the cause is a signaling NaN, in which case the result is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   206
    also a quiet NaN, but with the original sign, and an optional
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   207
    diagnostic information.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   208
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   209
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   210
        if args:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   211
            ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   212
            return ans._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   213
        return NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   214
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   215
class ConversionSyntax(InvalidOperation):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   216
    """Trying to convert badly formed string.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   217
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   218
    This occurs and signals invalid-operation if an string is being
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   219
    converted to a number and it does not conform to the numeric string
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   220
    syntax.  The result is [0,qNaN].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   221
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   222
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   223
        return NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   224
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   225
class DivisionByZero(DecimalException, ZeroDivisionError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   226
    """Division by 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   227
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   228
    This occurs and signals division-by-zero if division of a finite number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   229
    by zero was attempted (during a divide-integer or divide operation, or a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   230
    power operation with negative right-hand operand), and the dividend was
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   231
    not zero.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   232
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   233
    The result of the operation is [sign,inf], where sign is the exclusive
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   234
    or of the signs of the operands for divide, or is 1 for an odd power of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   235
    -0, for power.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   236
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   237
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   238
    def handle(self, context, sign, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   239
        return Infsign[sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   240
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   241
class DivisionImpossible(InvalidOperation):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   242
    """Cannot perform the division adequately.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   243
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   244
    This occurs and signals invalid-operation if the integer result of a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   245
    divide-integer or remainder operation had too many digits (would be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   246
    longer than precision).  The result is [0,qNaN].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   247
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   248
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   249
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   250
        return NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   251
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   252
class DivisionUndefined(InvalidOperation, ZeroDivisionError):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   253
    """Undefined result of division.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   254
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   255
    This occurs and signals invalid-operation if division by zero was
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   256
    attempted (during a divide-integer, divide, or remainder operation), and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   257
    the dividend is also zero.  The result is [0,qNaN].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   258
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   259
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   260
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   261
        return NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   262
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   263
class Inexact(DecimalException):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   264
    """Had to round, losing information.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   265
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   266
    This occurs and signals inexact whenever the result of an operation is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   267
    not exact (that is, it needed to be rounded and any discarded digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   268
    were non-zero), or if an overflow or underflow condition occurs.  The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   269
    result in all cases is unchanged.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   270
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   271
    The inexact signal may be tested (or trapped) to determine if a given
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   272
    operation (or sequence of operations) was inexact.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   273
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   274
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   275
class InvalidContext(InvalidOperation):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   276
    """Invalid context.  Unknown rounding, for example.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   277
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   278
    This occurs and signals invalid-operation if an invalid context was
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   279
    detected during an operation.  This can occur if contexts are not checked
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   280
    on creation and either the precision exceeds the capability of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   281
    underlying concrete representation or an unknown or unsupported rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   282
    was specified.  These aspects of the context need only be checked when
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   283
    the values are required to be used.  The result is [0,qNaN].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   284
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   285
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   286
    def handle(self, context, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   287
        return NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   288
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   289
class Rounded(DecimalException):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   290
    """Number got rounded (not  necessarily changed during rounding).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   291
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   292
    This occurs and signals rounded whenever the result of an operation is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   293
    rounded (that is, some zero or non-zero digits were discarded from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   294
    coefficient), or if an overflow or underflow condition occurs.  The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   295
    result in all cases is unchanged.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   296
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   297
    The rounded signal may be tested (or trapped) to determine if a given
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   298
    operation (or sequence of operations) caused a loss of precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   299
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   300
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   301
class Subnormal(DecimalException):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   302
    """Exponent < Emin before rounding.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   303
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   304
    This occurs and signals subnormal whenever the result of a conversion or
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   305
    operation is subnormal (that is, its adjusted exponent is less than
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   306
    Emin, before any rounding).  The result in all cases is unchanged.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   307
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   308
    The subnormal signal may be tested (or trapped) to determine if a given
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   309
    or operation (or sequence of operations) yielded a subnormal result.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   310
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   311
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   312
class Overflow(Inexact, Rounded):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   313
    """Numerical overflow.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   314
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   315
    This occurs and signals overflow if the adjusted exponent of a result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   316
    (from a conversion or from an operation that is not an attempt to divide
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   317
    by zero), after rounding, would be greater than the largest value that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   318
    can be handled by the implementation (the value Emax).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   319
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   320
    The result depends on the rounding mode:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   321
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   322
    For round-half-up and round-half-even (and for round-half-down and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   323
    round-up, if implemented), the result of the operation is [sign,inf],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   324
    where sign is the sign of the intermediate result.  For round-down, the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   325
    result is the largest finite number that can be represented in the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   326
    current precision, with the sign of the intermediate result.  For
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   327
    round-ceiling, the result is the same as for round-down if the sign of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   328
    the intermediate result is 1, or is [0,inf] otherwise.  For round-floor,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   329
    the result is the same as for round-down if the sign of the intermediate
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   330
    result is 0, or is [1,inf] otherwise.  In all cases, Inexact and Rounded
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   331
    will also be raised.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   332
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   333
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   334
    def handle(self, context, sign, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   335
        if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   336
                                ROUND_HALF_DOWN, ROUND_UP):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   337
            return Infsign[sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   338
        if sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   339
            if context.rounding == ROUND_CEILING:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   340
                return Infsign[sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   341
            return _dec_from_triple(sign, '9'*context.prec,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   342
                            context.Emax-context.prec+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   343
        if sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   344
            if context.rounding == ROUND_FLOOR:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   345
                return Infsign[sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   346
            return _dec_from_triple(sign, '9'*context.prec,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   347
                             context.Emax-context.prec+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   348
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   349
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   350
class Underflow(Inexact, Rounded, Subnormal):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   351
    """Numerical underflow with result rounded to 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   352
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   353
    This occurs and signals underflow if a result is inexact and the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   354
    adjusted exponent of the result would be smaller (more negative) than
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   355
    the smallest value that can be handled by the implementation (the value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   356
    Emin).  That is, the result is both inexact and subnormal.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   357
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   358
    The result after an underflow will be a subnormal number rounded, if
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   359
    necessary, so that its exponent is not less than Etiny.  This may result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   360
    in 0 with the sign of the intermediate result and an exponent of Etiny.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   361
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   362
    In all cases, Inexact, Rounded, and Subnormal will also be raised.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   363
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   364
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   365
# List of public traps and flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   366
_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   367
           Underflow, InvalidOperation, Subnormal]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   368
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   369
# Map conditions (per the spec) to signals
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   370
_condition_map = {ConversionSyntax:InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   371
                  DivisionImpossible:InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   372
                  DivisionUndefined:InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   373
                  InvalidContext:InvalidOperation}
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   374
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   375
##### Context Functions ##################################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   376
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   377
# The getcontext() and setcontext() function manage access to a thread-local
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   378
# current context.  Py2.4 offers direct support for thread locals.  If that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   379
# is not available, use threading.currentThread() which is slower but will
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   380
# work for older Pythons.  If threads are not part of the build, create a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   381
# mock threading object with threading.local() returning the module namespace.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   382
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   383
try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   384
    import threading
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   385
except ImportError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   386
    # Python was compiled without threads; create a mock object instead
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   387
    import sys
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   388
    class MockThreading(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   389
        def local(self, sys=sys):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   390
            return sys.modules[__name__]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   391
    threading = MockThreading()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   392
    del sys, MockThreading
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   393
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   394
try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   395
    threading.local
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   396
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   397
except AttributeError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   398
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   399
    # To fix reloading, force it to create a new context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   400
    # Old contexts have different exceptions in their dicts, making problems.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   401
    if hasattr(threading.currentThread(), '__decimal_context__'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   402
        del threading.currentThread().__decimal_context__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   403
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   404
    def setcontext(context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   405
        """Set this thread's context to context."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   406
        if context in (DefaultContext, BasicContext, ExtendedContext):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   407
            context = context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   408
            context.clear_flags()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   409
        threading.currentThread().__decimal_context__ = context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   410
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   411
    def getcontext():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   412
        """Returns this thread's context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   413
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   414
        If this thread does not yet have a context, returns
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   415
        a new context and sets this thread's context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   416
        New contexts are copies of DefaultContext.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   417
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   418
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   419
            return threading.currentThread().__decimal_context__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   420
        except AttributeError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   421
            context = Context()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   422
            threading.currentThread().__decimal_context__ = context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   423
            return context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   424
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   425
else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   426
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   427
    local = threading.local()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   428
    if hasattr(local, '__decimal_context__'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   429
        del local.__decimal_context__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   430
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   431
    def getcontext(_local=local):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   432
        """Returns this thread's context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   433
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   434
        If this thread does not yet have a context, returns
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   435
        a new context and sets this thread's context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   436
        New contexts are copies of DefaultContext.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   437
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   438
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   439
            return _local.__decimal_context__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   440
        except AttributeError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   441
            context = Context()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   442
            _local.__decimal_context__ = context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   443
            return context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   444
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   445
    def setcontext(context, _local=local):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   446
        """Set this thread's context to context."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   447
        if context in (DefaultContext, BasicContext, ExtendedContext):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   448
            context = context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   449
            context.clear_flags()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   450
        _local.__decimal_context__ = context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   451
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   452
    del threading, local        # Don't contaminate the namespace
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   453
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   454
def localcontext(ctx=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   455
    """Return a context manager for a copy of the supplied context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   456
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   457
    Uses a copy of the current context if no context is specified
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   458
    The returned context manager creates a local decimal context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   459
    in a with statement:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   460
        def sin(x):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   461
             with localcontext() as ctx:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   462
                 ctx.prec += 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   463
                 # Rest of sin calculation algorithm
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   464
                 # uses a precision 2 greater than normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   465
             return +s  # Convert result to normal precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   466
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   467
         def sin(x):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   468
             with localcontext(ExtendedContext):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   469
                 # Rest of sin calculation algorithm
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   470
                 # uses the Extended Context from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   471
                 # General Decimal Arithmetic Specification
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   472
             return +s  # Convert result to normal context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   473
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   474
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   475
    # The string below can't be included in the docstring until Python 2.6
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   476
    # as the doctest module doesn't understand __future__ statements
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   477
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   478
    >>> from __future__ import with_statement
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   479
    >>> print getcontext().prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   480
    28
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   481
    >>> with localcontext():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   482
    ...     ctx = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   483
    ...     ctx.prec += 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   484
    ...     print ctx.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   485
    ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   486
    30
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   487
    >>> with localcontext(ExtendedContext):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   488
    ...     print getcontext().prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   489
    ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   490
    9
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   491
    >>> print getcontext().prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   492
    28
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   493
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   494
    if ctx is None: ctx = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   495
    return _ContextManager(ctx)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   496
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   497
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   498
##### Decimal class #######################################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   499
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   500
class Decimal(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   501
    """Floating point class for decimal arithmetic."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   502
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   503
    __slots__ = ('_exp','_int','_sign', '_is_special')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   504
    # Generally, the value of the Decimal instance is given by
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   505
    #  (-1)**_sign * _int * 10**_exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   506
    # Special values are signified by _is_special == True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   507
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   508
    # We're immutable, so use __new__ not __init__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   509
    def __new__(cls, value="0", context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   510
        """Create a decimal point instance.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   511
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   512
        >>> Decimal('3.14')              # string input
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   513
        Decimal("3.14")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   514
        >>> Decimal((0, (3, 1, 4), -2))  # tuple (sign, digit_tuple, exponent)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   515
        Decimal("3.14")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   516
        >>> Decimal(314)                 # int or long
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   517
        Decimal("314")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   518
        >>> Decimal(Decimal(314))        # another decimal instance
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   519
        Decimal("314")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   520
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   521
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   522
        # Note that the coefficient, self._int, is actually stored as
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   523
        # a string rather than as a tuple of digits.  This speeds up
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   524
        # the "digits to integer" and "integer to digits" conversions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   525
        # that are used in almost every arithmetic operation on
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   526
        # Decimals.  This is an internal detail: the as_tuple function
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   527
        # and the Decimal constructor still deal with tuples of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   528
        # digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   529
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   530
        self = object.__new__(cls)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   531
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   532
        # From a string
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   533
        # REs insist on real strings, so we can too.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   534
        if isinstance(value, basestring):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   535
            m = _parser(value)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   536
            if m is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   537
                if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   538
                    context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   539
                return context._raise_error(ConversionSyntax,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   540
                                "Invalid literal for Decimal: %r" % value)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   541
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   542
            if m.group('sign') == "-":
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   543
                self._sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   544
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   545
                self._sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   546
            intpart = m.group('int')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   547
            if intpart is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   548
                # finite number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   549
                fracpart = m.group('frac')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   550
                exp = int(m.group('exp') or '0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   551
                if fracpart is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   552
                    self._int = (intpart+fracpart).lstrip('0') or '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   553
                    self._exp = exp - len(fracpart)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   554
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   555
                    self._int = intpart.lstrip('0') or '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   556
                    self._exp = exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   557
                self._is_special = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   558
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   559
                diag = m.group('diag')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   560
                if diag is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   561
                    # NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   562
                    self._int = diag.lstrip('0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   563
                    if m.group('signal'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   564
                        self._exp = 'N'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   565
                    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   566
                        self._exp = 'n'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   567
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   568
                    # infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   569
                    self._int = '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   570
                    self._exp = 'F'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   571
                self._is_special = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   572
            return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   573
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   574
        # From an integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   575
        if isinstance(value, (int,long)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   576
            if value >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   577
                self._sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   578
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   579
                self._sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   580
            self._exp = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   581
            self._int = str(abs(value))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   582
            self._is_special = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   583
            return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   584
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   585
        # From another decimal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   586
        if isinstance(value, Decimal):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   587
            self._exp  = value._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   588
            self._sign = value._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   589
            self._int  = value._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   590
            self._is_special  = value._is_special
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   591
            return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   592
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   593
        # From an internal working value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   594
        if isinstance(value, _WorkRep):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   595
            self._sign = value.sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   596
            self._int = str(value.int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   597
            self._exp = int(value.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   598
            self._is_special = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   599
            return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   600
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   601
        # tuple/list conversion (possibly from as_tuple())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   602
        if isinstance(value, (list,tuple)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   603
            if len(value) != 3:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   604
                raise ValueError('Invalid tuple size in creation of Decimal '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   605
                                 'from list or tuple.  The list or tuple '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   606
                                 'should have exactly three elements.')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   607
            # process sign.  The isinstance test rejects floats
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   608
            if not (isinstance(value[0], (int, long)) and value[0] in (0,1)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   609
                raise ValueError("Invalid sign.  The first value in the tuple "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   610
                                 "should be an integer; either 0 for a "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   611
                                 "positive number or 1 for a negative number.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   612
            self._sign = value[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   613
            if value[2] == 'F':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   614
                # infinity: value[1] is ignored
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   615
                self._int = '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   616
                self._exp = value[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   617
                self._is_special = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   618
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   619
                # process and validate the digits in value[1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   620
                digits = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   621
                for digit in value[1]:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   622
                    if isinstance(digit, (int, long)) and 0 <= digit <= 9:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   623
                        # skip leading zeros
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   624
                        if digits or digit != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   625
                            digits.append(digit)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   626
                    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   627
                        raise ValueError("The second value in the tuple must "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   628
                                         "be composed of integers in the range "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   629
                                         "0 through 9.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   630
                if value[2] in ('n', 'N'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   631
                    # NaN: digits form the diagnostic
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   632
                    self._int = ''.join(map(str, digits))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   633
                    self._exp = value[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   634
                    self._is_special = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   635
                elif isinstance(value[2], (int, long)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   636
                    # finite number: digits give the coefficient
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   637
                    self._int = ''.join(map(str, digits or [0]))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   638
                    self._exp = value[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   639
                    self._is_special = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   640
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   641
                    raise ValueError("The third value in the tuple must "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   642
                                     "be an integer, or one of the "
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   643
                                     "strings 'F', 'n', 'N'.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   644
            return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   645
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   646
        if isinstance(value, float):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   647
            raise TypeError("Cannot convert float to Decimal.  " +
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   648
                            "First convert the float to a string")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   649
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   650
        raise TypeError("Cannot convert %r to Decimal" % value)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   651
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   652
    def _isnan(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   653
        """Returns whether the number is not actually one.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   654
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   655
        0 if a number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   656
        1 if NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   657
        2 if sNaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   658
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   659
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   660
            exp = self._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   661
            if exp == 'n':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   662
                return 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   663
            elif exp == 'N':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   664
                return 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   665
        return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   666
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   667
    def _isinfinity(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   668
        """Returns whether the number is infinite
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   669
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   670
        0 if finite or not a number
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   671
        1 if +INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   672
        -1 if -INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   673
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   674
        if self._exp == 'F':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   675
            if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   676
                return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   677
            return 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   678
        return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   679
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   680
    def _check_nans(self, other=None, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   681
        """Returns whether the number is not actually one.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   682
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   683
        if self, other are sNaN, signal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   684
        if self, other are NaN return nan
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   685
        return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   686
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   687
        Done before operations.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   688
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   689
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   690
        self_is_nan = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   691
        if other is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   692
            other_is_nan = False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   693
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   694
            other_is_nan = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   695
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   696
        if self_is_nan or other_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   697
            if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   698
                context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   699
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   700
            if self_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   701
                return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   702
                                        self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   703
            if other_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   704
                return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   705
                                        other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   706
            if self_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   707
                return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   708
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   709
            return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   710
        return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   711
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   712
    def __nonzero__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   713
        """Return True if self is nonzero; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   714
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   715
        NaNs and infinities are considered nonzero.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   716
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   717
        return self._is_special or self._int != '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   718
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   719
    def __cmp__(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   720
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   721
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   722
            # Never return NotImplemented
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   723
            return 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   724
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   725
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   726
            # check for nans, without raising on a signaling nan
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   727
            if self._isnan() or other._isnan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   728
                return 1  # Comparison involving NaN's always reports self > other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   729
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   730
            # INF = INF
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   731
            return cmp(self._isinfinity(), other._isinfinity())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   732
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   733
        # check for zeros;  note that cmp(0, -0) should return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   734
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   735
            if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   736
                return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   737
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   738
                return -((-1)**other._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   739
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   740
            return (-1)**self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   741
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   742
        # If different signs, neg one is less
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   743
        if other._sign < self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   744
            return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   745
        if self._sign < other._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   746
            return 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   747
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   748
        self_adjusted = self.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   749
        other_adjusted = other.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   750
        if self_adjusted == other_adjusted:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   751
            self_padded = self._int + '0'*(self._exp - other._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   752
            other_padded = other._int + '0'*(other._exp - self._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   753
            return cmp(self_padded, other_padded) * (-1)**self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   754
        elif self_adjusted > other_adjusted:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   755
            return (-1)**self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   756
        else: # self_adjusted < other_adjusted
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   757
            return -((-1)**self._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   758
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   759
    def __eq__(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   760
        if not isinstance(other, (Decimal, int, long)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   761
            return NotImplemented
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   762
        return self.__cmp__(other) == 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   763
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   764
    def __ne__(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   765
        if not isinstance(other, (Decimal, int, long)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   766
            return NotImplemented
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   767
        return self.__cmp__(other) != 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   768
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   769
    def compare(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   770
        """Compares one to another.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   771
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   772
        -1 => a < b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   773
        0  => a = b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   774
        1  => a > b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   775
        NaN => one is NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   776
        Like __cmp__, but returns Decimal instances.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   777
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   778
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   779
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   780
        # Compare(NaN, NaN) = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   781
        if (self._is_special or other and other._is_special):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   782
            ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   783
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   784
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   785
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   786
        return Decimal(self.__cmp__(other))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   787
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   788
    def __hash__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   789
        """x.__hash__() <==> hash(x)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   790
        # Decimal integers must hash the same as the ints
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   791
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   792
        # The hash of a nonspecial noninteger Decimal must depend only
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   793
        # on the value of that Decimal, and not on its representation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   794
        # For example: hash(Decimal("100E-1")) == hash(Decimal("10")).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   795
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   796
            if self._isnan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   797
                raise TypeError('Cannot hash a NaN value.')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   798
            return hash(str(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   799
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   800
            return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   801
        if self._isinteger():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   802
            op = _WorkRep(self.to_integral_value())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   803
            return hash((-1)**op.sign*op.int*10**op.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   804
        # The value of a nonzero nonspecial Decimal instance is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   805
        # faithfully represented by the triple consisting of its sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   806
        # its adjusted exponent, and its coefficient with trailing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   807
        # zeros removed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   808
        return hash((self._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   809
                     self._exp+len(self._int),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   810
                     self._int.rstrip('0')))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   811
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   812
    def as_tuple(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   813
        """Represents the number as a triple tuple.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   814
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   815
        To show the internals exactly as they are.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   816
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   817
        return (self._sign, tuple(map(int, self._int)), self._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   818
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   819
    def __repr__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   820
        """Represents the number as an instance of Decimal."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   821
        # Invariant:  eval(repr(d)) == d
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   822
        return 'Decimal("%s")' % str(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   823
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   824
    def __str__(self, eng=False, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   825
        """Return string representation of the number in scientific notation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   826
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   827
        Captures all of the information in the underlying representation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   828
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   829
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   830
        sign = ['', '-'][self._sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   831
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   832
            if self._exp == 'F':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   833
                return sign + 'Infinity'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   834
            elif self._exp == 'n':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   835
                return sign + 'NaN' + self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   836
            else: # self._exp == 'N'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   837
                return sign + 'sNaN' + self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   838
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   839
        # number of digits of self._int to left of decimal point
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   840
        leftdigits = self._exp + len(self._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   841
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   842
        # dotplace is number of digits of self._int to the left of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   843
        # decimal point in the mantissa of the output string (that is,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   844
        # after adjusting the exponent)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   845
        if self._exp <= 0 and leftdigits > -6:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   846
            # no exponent required
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   847
            dotplace = leftdigits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   848
        elif not eng:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   849
            # usual scientific notation: 1 digit on left of the point
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   850
            dotplace = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   851
        elif self._int == '0':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   852
            # engineering notation, zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   853
            dotplace = (leftdigits + 1) % 3 - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   854
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   855
            # engineering notation, nonzero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   856
            dotplace = (leftdigits - 1) % 3 + 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   857
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   858
        if dotplace <= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   859
            intpart = '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   860
            fracpart = '.' + '0'*(-dotplace) + self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   861
        elif dotplace >= len(self._int):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   862
            intpart = self._int+'0'*(dotplace-len(self._int))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   863
            fracpart = ''
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   864
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   865
            intpart = self._int[:dotplace]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   866
            fracpart = '.' + self._int[dotplace:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   867
        if leftdigits == dotplace:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   868
            exp = ''
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   869
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   870
            if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   871
                context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   872
            exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   873
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   874
        return sign + intpart + fracpart + exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   875
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   876
    def to_eng_string(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   877
        """Convert to engineering-type string.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   878
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   879
        Engineering notation has an exponent which is a multiple of 3, so there
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   880
        are up to 3 digits left of the decimal place.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   881
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   882
        Same rules for when in exponential and when as a value as in __str__.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   883
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   884
        return self.__str__(eng=True, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   885
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   886
    def __neg__(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   887
        """Returns a copy with the sign switched.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   888
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   889
        Rounds, if it has reason.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   890
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   891
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   892
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   893
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   894
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   895
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   896
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   897
            # -Decimal('0') is Decimal('0'), not Decimal('-0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   898
            ans = self.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   899
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   900
            ans = self.copy_negate()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   901
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   902
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   903
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   904
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   905
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   906
    def __pos__(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   907
        """Returns a copy, unless it is a sNaN.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   908
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   909
        Rounds the number (if more then precision digits)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   910
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   911
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   912
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   913
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   914
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   915
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   916
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   917
            # + (-0) = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   918
            ans = self.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   919
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   920
            ans = Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   921
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   922
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   923
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   924
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   925
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   926
    def __abs__(self, round=True, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   927
        """Returns the absolute value of self.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   928
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   929
        If the keyword argument 'round' is false, do not round.  The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   930
        expression self.__abs__(round=False) is equivalent to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   931
        self.copy_abs().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   932
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   933
        if not round:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   934
            return self.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   935
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   936
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   937
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   938
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   939
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   940
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   941
        if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   942
            ans = self.__neg__(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   943
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   944
            ans = self.__pos__(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   945
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   946
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   947
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   948
    def __add__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   949
        """Returns self + other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   950
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   951
        -INF + INF (or the reverse) cause InvalidOperation errors.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   952
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   953
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   954
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   955
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   956
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   957
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   958
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   959
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   960
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   961
            ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   962
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   963
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   964
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   965
            if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   966
                # If both INF, same sign => same as both, opposite => error.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   967
                if self._sign != other._sign and other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   968
                    return context._raise_error(InvalidOperation, '-INF + INF')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   969
                return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   970
            if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   971
                return Decimal(other)  # Can't both be infinity here
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   972
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   973
        exp = min(self._exp, other._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   974
        negativezero = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   975
        if context.rounding == ROUND_FLOOR and self._sign != other._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   976
            # If the answer is 0, the sign should be negative, in this case.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   977
            negativezero = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   978
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   979
        if not self and not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   980
            sign = min(self._sign, other._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   981
            if negativezero:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   982
                sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   983
            ans = _dec_from_triple(sign, '0', exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   984
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   985
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   986
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   987
            exp = max(exp, other._exp - context.prec-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   988
            ans = other._rescale(exp, context.rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   989
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   990
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   991
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   992
            exp = max(exp, self._exp - context.prec-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   993
            ans = self._rescale(exp, context.rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   994
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   995
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   996
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   997
        op1 = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   998
        op2 = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
   999
        op1, op2 = _normalize(op1, op2, context.prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1000
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1001
        result = _WorkRep()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1002
        if op1.sign != op2.sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1003
            # Equal and opposite
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1004
            if op1.int == op2.int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1005
                ans = _dec_from_triple(negativezero, '0', exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1006
                ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1007
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1008
            if op1.int < op2.int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1009
                op1, op2 = op2, op1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1010
                # OK, now abs(op1) > abs(op2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1011
            if op1.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1012
                result.sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1013
                op1.sign, op2.sign = op2.sign, op1.sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1014
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1015
                result.sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1016
                # So we know the sign, and op1 > 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1017
        elif op1.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1018
            result.sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1019
            op1.sign, op2.sign = (0, 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1020
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1021
            result.sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1022
        # Now, op1 > abs(op2) > 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1023
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1024
        if op2.sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1025
            result.int = op1.int + op2.int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1026
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1027
            result.int = op1.int - op2.int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1028
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1029
        result.exp = op1.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1030
        ans = Decimal(result)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1031
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1032
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1033
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1034
    __radd__ = __add__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1035
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1036
    def __sub__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1037
        """Return self - other"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1038
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1039
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1040
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1041
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1042
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1043
            ans = self._check_nans(other, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1044
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1045
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1046
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1047
        # self - other is computed as self + other.copy_negate()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1048
        return self.__add__(other.copy_negate(), context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1049
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1050
    def __rsub__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1051
        """Return other - self"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1052
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1053
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1054
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1055
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1056
        return other.__sub__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1057
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1058
    def __mul__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1059
        """Return self * other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1060
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1061
        (+-) INF * 0 (or its reverse) raise InvalidOperation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1062
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1063
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1064
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1065
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1066
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1067
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1068
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1069
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1070
        resultsign = self._sign ^ other._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1071
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1072
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1073
            ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1074
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1075
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1076
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1077
            if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1078
                if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1079
                    return context._raise_error(InvalidOperation, '(+-)INF * 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1080
                return Infsign[resultsign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1081
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1082
            if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1083
                if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1084
                    return context._raise_error(InvalidOperation, '0 * (+-)INF')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1085
                return Infsign[resultsign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1086
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1087
        resultexp = self._exp + other._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1088
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1089
        # Special case for multiplying by zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1090
        if not self or not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1091
            ans = _dec_from_triple(resultsign, '0', resultexp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1092
            # Fixing in case the exponent is out of bounds
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1093
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1094
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1095
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1096
        # Special case for multiplying by power of 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1097
        if self._int == '1':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1098
            ans = _dec_from_triple(resultsign, other._int, resultexp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1099
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1100
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1101
        if other._int == '1':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1102
            ans = _dec_from_triple(resultsign, self._int, resultexp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1103
            ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1104
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1105
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1106
        op1 = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1107
        op2 = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1108
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1109
        ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1110
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1111
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1112
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1113
    __rmul__ = __mul__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1114
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1115
    def __div__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1116
        """Return self / other."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1117
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1118
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1119
            return NotImplemented
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1120
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1121
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1122
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1123
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1124
        sign = self._sign ^ other._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1125
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1126
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1127
            ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1128
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1129
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1130
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1131
            if self._isinfinity() and other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1132
                return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1133
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1134
            if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1135
                return Infsign[sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1136
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1137
            if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1138
                context._raise_error(Clamped, 'Division by infinity')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1139
                return _dec_from_triple(sign, '0', context.Etiny())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1140
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1141
        # Special cases for zeroes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1142
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1143
            if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1144
                return context._raise_error(DivisionUndefined, '0 / 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1145
            return context._raise_error(DivisionByZero, 'x / 0', sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1146
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1147
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1148
            exp = self._exp - other._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1149
            coeff = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1150
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1151
            # OK, so neither = 0, INF or NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1152
            shift = len(other._int) - len(self._int) + context.prec + 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1153
            exp = self._exp - other._exp - shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1154
            op1 = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1155
            op2 = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1156
            if shift >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1157
                coeff, remainder = divmod(op1.int * 10**shift, op2.int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1158
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1159
                coeff, remainder = divmod(op1.int, op2.int * 10**-shift)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1160
            if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1161
                # result is not exact; adjust to ensure correct rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1162
                if coeff % 5 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1163
                    coeff += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1164
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1165
                # result is exact; get as close to ideal exponent as possible
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1166
                ideal_exp = self._exp - other._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1167
                while exp < ideal_exp and coeff % 10 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1168
                    coeff //= 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1169
                    exp += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1170
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1171
        ans = _dec_from_triple(sign, str(coeff), exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1172
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1173
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1174
    __truediv__ = __div__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1175
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1176
    def _divide(self, other, context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1177
        """Return (self // other, self % other), to context.prec precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1178
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1179
        Assumes that neither self nor other is a NaN, that self is not
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1180
        infinite and that other is nonzero.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1181
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1182
        sign = self._sign ^ other._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1183
        if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1184
            ideal_exp = self._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1185
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1186
            ideal_exp = min(self._exp, other._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1187
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1188
        expdiff = self.adjusted() - other.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1189
        if not self or other._isinfinity() or expdiff <= -2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1190
            return (_dec_from_triple(sign, '0', 0),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1191
                    self._rescale(ideal_exp, context.rounding))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1192
        if expdiff <= context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1193
            op1 = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1194
            op2 = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1195
            if op1.exp >= op2.exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1196
                op1.int *= 10**(op1.exp - op2.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1197
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1198
                op2.int *= 10**(op2.exp - op1.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1199
            q, r = divmod(op1.int, op2.int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1200
            if q < 10**context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1201
                return (_dec_from_triple(sign, str(q), 0),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1202
                        _dec_from_triple(self._sign, str(r), ideal_exp))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1203
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1204
        # Here the quotient is too large to be representable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1205
        ans = context._raise_error(DivisionImpossible,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1206
                                   'quotient too large in //, % or divmod')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1207
        return ans, ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1208
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1209
    def __rdiv__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1210
        """Swaps self/other and returns __div__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1211
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1212
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1213
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1214
        return other.__div__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1215
    __rtruediv__ = __rdiv__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1216
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1217
    def __divmod__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1218
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1219
        Return (self // other, self % other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1220
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1221
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1222
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1223
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1224
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1225
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1226
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1227
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1228
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1229
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1230
            return (ans, ans)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1231
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1232
        sign = self._sign ^ other._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1233
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1234
            if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1235
                ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1236
                return ans, ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1237
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1238
                return (Infsign[sign],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1239
                        context._raise_error(InvalidOperation, 'INF % x'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1240
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1241
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1242
            if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1243
                ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1244
                return ans, ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1245
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1246
                return (context._raise_error(DivisionByZero, 'x // 0', sign),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1247
                        context._raise_error(InvalidOperation, 'x % 0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1248
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1249
        quotient, remainder = self._divide(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1250
        remainder = remainder._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1251
        return quotient, remainder
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1252
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1253
    def __rdivmod__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1254
        """Swaps self/other and returns __divmod__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1255
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1256
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1257
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1258
        return other.__divmod__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1259
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1260
    def __mod__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1261
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1262
        self % other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1263
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1264
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1265
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1266
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1267
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1268
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1269
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1270
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1271
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1272
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1273
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1274
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1275
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1276
            return context._raise_error(InvalidOperation, 'INF % x')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1277
        elif not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1278
            if self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1279
                return context._raise_error(InvalidOperation, 'x % 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1280
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1281
                return context._raise_error(DivisionUndefined, '0 % 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1282
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1283
        remainder = self._divide(other, context)[1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1284
        remainder = remainder._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1285
        return remainder
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1286
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1287
    def __rmod__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1288
        """Swaps self/other and returns __mod__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1289
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1290
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1291
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1292
        return other.__mod__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1293
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1294
    def remainder_near(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1295
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1296
        Remainder nearest to 0-  abs(remainder-near) <= other/2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1297
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1298
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1299
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1300
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1301
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1302
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1303
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1304
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1305
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1306
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1307
        # self == +/-infinity -> InvalidOperation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1308
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1309
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1310
                                        'remainder_near(infinity, x)')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1311
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1312
        # other == 0 -> either InvalidOperation or DivisionUndefined
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1313
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1314
            if self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1315
                return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1316
                                            'remainder_near(x, 0)')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1317
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1318
                return context._raise_error(DivisionUndefined,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1319
                                            'remainder_near(0, 0)')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1320
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1321
        # other = +/-infinity -> remainder = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1322
        if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1323
            ans = Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1324
            return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1325
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1326
        # self = 0 -> remainder = self, with ideal exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1327
        ideal_exponent = min(self._exp, other._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1328
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1329
            ans = _dec_from_triple(self._sign, '0', ideal_exponent)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1330
            return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1331
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1332
        # catch most cases of large or small quotient
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1333
        expdiff = self.adjusted() - other.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1334
        if expdiff >= context.prec + 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1335
            # expdiff >= prec+1 => abs(self/other) > 10**prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1336
            return context._raise_error(DivisionImpossible)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1337
        if expdiff <= -2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1338
            # expdiff <= -2 => abs(self/other) < 0.1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1339
            ans = self._rescale(ideal_exponent, context.rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1340
            return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1341
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1342
        # adjust both arguments to have the same exponent, then divide
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1343
        op1 = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1344
        op2 = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1345
        if op1.exp >= op2.exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1346
            op1.int *= 10**(op1.exp - op2.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1347
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1348
            op2.int *= 10**(op2.exp - op1.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1349
        q, r = divmod(op1.int, op2.int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1350
        # remainder is r*10**ideal_exponent; other is +/-op2.int *
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1351
        # 10**ideal_exponent.   Apply correction to ensure that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1352
        # abs(remainder) <= abs(other)/2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1353
        if 2*r + (q&1) > op2.int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1354
            r -= op2.int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1355
            q += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1356
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1357
        if q >= 10**context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1358
            return context._raise_error(DivisionImpossible)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1359
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1360
        # result has same sign as self unless r is negative
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1361
        sign = self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1362
        if r < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1363
            sign = 1-sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1364
            r = -r
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1365
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1366
        ans = _dec_from_triple(sign, str(r), ideal_exponent)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1367
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1368
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1369
    def __floordiv__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1370
        """self // other"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1371
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1372
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1373
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1374
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1375
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1376
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1377
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1378
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1379
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1380
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1381
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1382
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1383
            if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1384
                return context._raise_error(InvalidOperation, 'INF // INF')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1385
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1386
                return Infsign[self._sign ^ other._sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1387
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1388
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1389
            if self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1390
                return context._raise_error(DivisionByZero, 'x // 0',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1391
                                            self._sign ^ other._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1392
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1393
                return context._raise_error(DivisionUndefined, '0 // 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1394
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1395
        return self._divide(other, context)[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1396
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1397
    def __rfloordiv__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1398
        """Swaps self/other and returns __floordiv__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1399
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1400
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1401
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1402
        return other.__floordiv__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1403
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1404
    def __float__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1405
        """Float representation."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1406
        return float(str(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1407
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1408
    def __int__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1409
        """Converts self to an int, truncating if necessary."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1410
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1411
            if self._isnan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1412
                context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1413
                return context._raise_error(InvalidContext)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1414
            elif self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1415
                raise OverflowError("Cannot convert infinity to long")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1416
        s = (-1)**self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1417
        if self._exp >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1418
            return s*int(self._int)*10**self._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1419
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1420
            return s*int(self._int[:self._exp] or '0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1421
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1422
    def __long__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1423
        """Converts to a long.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1424
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1425
        Equivalent to long(int(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1426
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1427
        return long(self.__int__())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1428
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1429
    def _fix_nan(self, context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1430
        """Decapitate the payload of a NaN to fit the context"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1431
        payload = self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1432
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1433
        # maximum length of payload is precision if _clamp=0,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1434
        # precision-1 if _clamp=1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1435
        max_payload_len = context.prec - context._clamp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1436
        if len(payload) > max_payload_len:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1437
            payload = payload[len(payload)-max_payload_len:].lstrip('0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1438
            return _dec_from_triple(self._sign, payload, self._exp, True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1439
        return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1440
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1441
    def _fix(self, context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1442
        """Round if it is necessary to keep self within prec precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1443
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1444
        Rounds and fixes the exponent.  Does not raise on a sNaN.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1445
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1446
        Arguments:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1447
        self - Decimal instance
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1448
        context - context used.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1449
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1450
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1451
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1452
            if self._isnan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1453
                # decapitate payload if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1454
                return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1455
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1456
                # self is +/-Infinity; return unaltered
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1457
                return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1458
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1459
        # if self is zero then exponent should be between Etiny and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1460
        # Emax if _clamp==0, and between Etiny and Etop if _clamp==1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1461
        Etiny = context.Etiny()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1462
        Etop = context.Etop()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1463
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1464
            exp_max = [context.Emax, Etop][context._clamp]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1465
            new_exp = min(max(self._exp, Etiny), exp_max)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1466
            if new_exp != self._exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1467
                context._raise_error(Clamped)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1468
                return _dec_from_triple(self._sign, '0', new_exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1469
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1470
                return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1471
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1472
        # exp_min is the smallest allowable exponent of the result,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1473
        # equal to max(self.adjusted()-context.prec+1, Etiny)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1474
        exp_min = len(self._int) + self._exp - context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1475
        if exp_min > Etop:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1476
            # overflow: exp_min > Etop iff self.adjusted() > Emax
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1477
            context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1478
            context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1479
            return context._raise_error(Overflow, 'above Emax', self._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1480
        self_is_subnormal = exp_min < Etiny
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1481
        if self_is_subnormal:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1482
            context._raise_error(Subnormal)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1483
            exp_min = Etiny
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1484
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1485
        # round if self has too many digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1486
        if self._exp < exp_min:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1487
            context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1488
            digits = len(self._int) + self._exp - exp_min
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1489
            if digits < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1490
                self = _dec_from_triple(self._sign, '1', exp_min-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1491
                digits = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1492
            this_function = getattr(self, self._pick_rounding_function[context.rounding])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1493
            changed = this_function(digits)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1494
            coeff = self._int[:digits] or '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1495
            if changed == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1496
                coeff = str(int(coeff)+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1497
            ans = _dec_from_triple(self._sign, coeff, exp_min)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1498
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1499
            if changed:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1500
                context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1501
                if self_is_subnormal:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1502
                    context._raise_error(Underflow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1503
                    if not ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1504
                        # raise Clamped on underflow to 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1505
                        context._raise_error(Clamped)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1506
                elif len(ans._int) == context.prec+1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1507
                    # we get here only if rescaling rounds the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1508
                    # cofficient up to exactly 10**context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1509
                    if ans._exp < Etop:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1510
                        ans = _dec_from_triple(ans._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1511
                                                   ans._int[:-1], ans._exp+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1512
                    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1513
                        # Inexact and Rounded have already been raised
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1514
                        ans = context._raise_error(Overflow, 'above Emax',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1515
                                                   self._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1516
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1517
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1518
        # fold down if _clamp == 1 and self has too few digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1519
        if context._clamp == 1 and self._exp > Etop:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1520
            context._raise_error(Clamped)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1521
            self_padded = self._int + '0'*(self._exp - Etop)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1522
            return _dec_from_triple(self._sign, self_padded, Etop)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1523
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1524
        # here self was representable to begin with; return unchanged
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1525
        return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1526
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1527
    _pick_rounding_function = {}
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1528
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1529
    # for each of the rounding functions below:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1530
    #   self is a finite, nonzero Decimal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1531
    #   prec is an integer satisfying 0 <= prec < len(self._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1532
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1533
    # each function returns either -1, 0, or 1, as follows:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1534
    #   1 indicates that self should be rounded up (away from zero)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1535
    #   0 indicates that self should be truncated, and that all the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1536
    #     digits to be truncated are zeros (so the value is unchanged)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1537
    #  -1 indicates that there are nonzero digits to be truncated
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1538
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1539
    def _round_down(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1540
        """Also known as round-towards-0, truncate."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1541
        if _all_zeros(self._int, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1542
            return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1543
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1544
            return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1545
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1546
    def _round_up(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1547
        """Rounds away from 0."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1548
        return -self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1549
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1550
    def _round_half_up(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1551
        """Rounds 5 up (away from 0)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1552
        if self._int[prec] in '56789':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1553
            return 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1554
        elif _all_zeros(self._int, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1555
            return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1556
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1557
            return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1558
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1559
    def _round_half_down(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1560
        """Round 5 down"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1561
        if _exact_half(self._int, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1562
            return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1563
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1564
            return self._round_half_up(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1565
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1566
    def _round_half_even(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1567
        """Round 5 to even, rest to nearest."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1568
        if _exact_half(self._int, prec) and \
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1569
                (prec == 0 or self._int[prec-1] in '02468'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1570
            return -1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1571
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1572
            return self._round_half_up(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1573
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1574
    def _round_ceiling(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1575
        """Rounds up (not away from 0 if negative.)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1576
        if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1577
            return self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1578
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1579
            return -self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1580
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1581
    def _round_floor(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1582
        """Rounds down (not towards 0 if negative)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1583
        if not self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1584
            return self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1585
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1586
            return -self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1587
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1588
    def _round_05up(self, prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1589
        """Round down unless digit prec-1 is 0 or 5."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1590
        if prec and self._int[prec-1] not in '05':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1591
            return self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1592
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1593
            return -self._round_down(prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1594
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1595
    def fma(self, other, third, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1596
        """Fused multiply-add.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1597
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1598
        Returns self*other+third with no rounding of the intermediate
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1599
        product self*other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1600
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1601
        self and other are multiplied together, with no rounding of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1602
        the result.  The third operand is then added to the result,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1603
        and a single final rounding is performed.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1604
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1605
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1606
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1607
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1608
        # compute product; raise InvalidOperation if either operand is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1609
        # a signaling NaN or if the product is zero times infinity.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1610
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1611
            if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1612
                context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1613
            if self._exp == 'N':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1614
                return context._raise_error(InvalidOperation, 'sNaN', self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1615
            if other._exp == 'N':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1616
                return context._raise_error(InvalidOperation, 'sNaN', other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1617
            if self._exp == 'n':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1618
                product = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1619
            elif other._exp == 'n':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1620
                product = other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1621
            elif self._exp == 'F':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1622
                if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1623
                    return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1624
                                                'INF * 0 in fma')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1625
                product = Infsign[self._sign ^ other._sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1626
            elif other._exp == 'F':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1627
                if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1628
                    return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1629
                                                '0 * INF in fma')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1630
                product = Infsign[self._sign ^ other._sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1631
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1632
            product = _dec_from_triple(self._sign ^ other._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1633
                                       str(int(self._int) * int(other._int)),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1634
                                       self._exp + other._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1635
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1636
        third = _convert_other(third, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1637
        return product.__add__(third, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1638
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1639
    def _power_modulo(self, other, modulo, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1640
        """Three argument version of __pow__"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1641
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1642
        # if can't convert other and modulo to Decimal, raise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1643
        # TypeError; there's no point returning NotImplemented (no
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1644
        # equivalent of __rpow__ for three argument pow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1645
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1646
        modulo = _convert_other(modulo, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1647
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1648
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1649
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1650
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1651
        # deal with NaNs: if there are any sNaNs then first one wins,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1652
        # (i.e. behaviour for NaNs is identical to that of fma)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1653
        self_is_nan = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1654
        other_is_nan = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1655
        modulo_is_nan = modulo._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1656
        if self_is_nan or other_is_nan or modulo_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1657
            if self_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1658
                return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1659
                                        self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1660
            if other_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1661
                return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1662
                                        other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1663
            if modulo_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1664
                return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1665
                                        modulo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1666
            if self_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1667
                return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1668
            if other_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1669
                return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1670
            return modulo._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1671
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1672
        # check inputs: we apply same restrictions as Python's pow()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1673
        if not (self._isinteger() and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1674
                other._isinteger() and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1675
                modulo._isinteger()):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1676
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1677
                                        'pow() 3rd argument not allowed '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1678
                                        'unless all arguments are integers')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1679
        if other < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1680
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1681
                                        'pow() 2nd argument cannot be '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1682
                                        'negative when 3rd argument specified')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1683
        if not modulo:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1684
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1685
                                        'pow() 3rd argument cannot be 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1686
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1687
        # additional restriction for decimal: the modulus must be less
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1688
        # than 10**prec in absolute value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1689
        if modulo.adjusted() >= context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1690
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1691
                                        'insufficient precision: pow() 3rd '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1692
                                        'argument must not have more than '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1693
                                        'precision digits')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1694
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1695
        # define 0**0 == NaN, for consistency with two-argument pow
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1696
        # (even though it hurts!)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1697
        if not other and not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1698
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1699
                                        'at least one of pow() 1st argument '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1700
                                        'and 2nd argument must be nonzero ;'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1701
                                        '0**0 is not defined')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1702
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1703
        # compute sign of result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1704
        if other._iseven():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1705
            sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1706
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1707
            sign = self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1708
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1709
        # convert modulo to a Python integer, and self and other to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1710
        # Decimal integers (i.e. force their exponents to be >= 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1711
        modulo = abs(int(modulo))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1712
        base = _WorkRep(self.to_integral_value())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1713
        exponent = _WorkRep(other.to_integral_value())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1714
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1715
        # compute result using integer pow()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1716
        base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1717
        for i in xrange(exponent.exp):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1718
            base = pow(base, 10, modulo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1719
        base = pow(base, exponent.int, modulo)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1720
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1721
        return _dec_from_triple(sign, str(base), 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1722
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1723
    def _power_exact(self, other, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1724
        """Attempt to compute self**other exactly.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1725
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1726
        Given Decimals self and other and an integer p, attempt to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1727
        compute an exact result for the power self**other, with p
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1728
        digits of precision.  Return None if self**other is not
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1729
        exactly representable in p digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1730
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1731
        Assumes that elimination of special cases has already been
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1732
        performed: self and other must both be nonspecial; self must
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1733
        be positive and not numerically equal to 1; other must be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1734
        nonzero.  For efficiency, other._exp should not be too large,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1735
        so that 10**abs(other._exp) is a feasible calculation."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1736
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1737
        # In the comments below, we write x for the value of self and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1738
        # y for the value of other.  Write x = xc*10**xe and y =
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1739
        # yc*10**ye.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1740
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1741
        # The main purpose of this method is to identify the *failure*
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1742
        # of x**y to be exactly representable with as little effort as
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1743
        # possible.  So we look for cheap and easy tests that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1744
        # eliminate the possibility of x**y being exact.  Only if all
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1745
        # these tests are passed do we go on to actually compute x**y.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1746
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1747
        # Here's the main idea.  First normalize both x and y.  We
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1748
        # express y as a rational m/n, with m and n relatively prime
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1749
        # and n>0.  Then for x**y to be exactly representable (at
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1750
        # *any* precision), xc must be the nth power of a positive
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1751
        # integer and xe must be divisible by n.  If m is negative
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1752
        # then additionally xc must be a power of either 2 or 5, hence
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1753
        # a power of 2**n or 5**n.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1754
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1755
        # There's a limit to how small |y| can be: if y=m/n as above
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1756
        # then:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1757
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1758
        #  (1) if xc != 1 then for the result to be representable we
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1759
        #      need xc**(1/n) >= 2, and hence also xc**|y| >= 2.  So
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1760
        #      if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <=
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1761
        #      2**(1/|y|), hence xc**|y| < 2 and the result is not
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1762
        #      representable.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1763
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1764
        #  (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1.  Hence if
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1765
        #      |y| < 1/|xe| then the result is not representable.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1766
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1767
        # Note that since x is not equal to 1, at least one of (1) and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1768
        # (2) must apply.  Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) <
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1769
        # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1770
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1771
        # There's also a limit to how large y can be, at least if it's
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1772
        # positive: the normalized result will have coefficient xc**y,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1773
        # so if it's representable then xc**y < 10**p, and y <
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1774
        # p/log10(xc).  Hence if y*log10(xc) >= p then the result is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1775
        # not exactly representable.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1776
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1777
        # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1778
        # so |y| < 1/xe and the result is not representable.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1779
        # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y|
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1780
        # < 1/nbits(xc).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1781
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1782
        x = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1783
        xc, xe = x.int, x.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1784
        while xc % 10 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1785
            xc //= 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1786
            xe += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1787
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1788
        y = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1789
        yc, ye = y.int, y.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1790
        while yc % 10 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1791
            yc //= 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1792
            ye += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1793
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1794
        # case where xc == 1: result is 10**(xe*y), with xe*y
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1795
        # required to be an integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1796
        if xc == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1797
            if ye >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1798
                exponent = xe*yc*10**ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1799
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1800
                exponent, remainder = divmod(xe*yc, 10**-ye)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1801
                if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1802
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1803
            if y.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1804
                exponent = -exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1805
            # if other is a nonnegative integer, use ideal exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1806
            if other._isinteger() and other._sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1807
                ideal_exponent = self._exp*int(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1808
                zeros = min(exponent-ideal_exponent, p-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1809
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1810
                zeros = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1811
            return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1812
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1813
        # case where y is negative: xc must be either a power
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1814
        # of 2 or a power of 5.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1815
        if y.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1816
            last_digit = xc % 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1817
            if last_digit in (2,4,6,8):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1818
                # quick test for power of 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1819
                if xc & -xc != xc:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1820
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1821
                # now xc is a power of 2; e is its exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1822
                e = _nbits(xc)-1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1823
                # find e*y and xe*y; both must be integers
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1824
                if ye >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1825
                    y_as_int = yc*10**ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1826
                    e = e*y_as_int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1827
                    xe = xe*y_as_int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1828
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1829
                    ten_pow = 10**-ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1830
                    e, remainder = divmod(e*yc, ten_pow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1831
                    if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1832
                        return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1833
                    xe, remainder = divmod(xe*yc, ten_pow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1834
                    if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1835
                        return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1836
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1837
                if e*65 >= p*93: # 93/65 > log(10)/log(5)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1838
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1839
                xc = 5**e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1840
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1841
            elif last_digit == 5:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1842
                # e >= log_5(xc) if xc is a power of 5; we have
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1843
                # equality all the way up to xc=5**2658
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1844
                e = _nbits(xc)*28//65
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1845
                xc, remainder = divmod(5**e, xc)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1846
                if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1847
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1848
                while xc % 5 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1849
                    xc //= 5
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1850
                    e -= 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1851
                if ye >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1852
                    y_as_integer = yc*10**ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1853
                    e = e*y_as_integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1854
                    xe = xe*y_as_integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1855
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1856
                    ten_pow = 10**-ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1857
                    e, remainder = divmod(e*yc, ten_pow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1858
                    if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1859
                        return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1860
                    xe, remainder = divmod(xe*yc, ten_pow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1861
                    if remainder:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1862
                        return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1863
                if e*3 >= p*10: # 10/3 > log(10)/log(2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1864
                    return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1865
                xc = 2**e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1866
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1867
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1868
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1869
            if xc >= 10**p:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1870
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1871
            xe = -e-xe
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1872
            return _dec_from_triple(0, str(xc), xe)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1873
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1874
        # now y is positive; find m and n such that y = m/n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1875
        if ye >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1876
            m, n = yc*10**ye, 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1877
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1878
            if xe != 0 and len(str(abs(yc*xe))) <= -ye:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1879
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1880
            xc_bits = _nbits(xc)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1881
            if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1882
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1883
            m, n = yc, 10**(-ye)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1884
            while m % 2 == n % 2 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1885
                m //= 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1886
                n //= 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1887
            while m % 5 == n % 5 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1888
                m //= 5
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1889
                n //= 5
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1890
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1891
        # compute nth root of xc*10**xe
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1892
        if n > 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1893
            # if 1 < xc < 2**n then xc isn't an nth power
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1894
            if xc != 1 and xc_bits <= n:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1895
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1896
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1897
            xe, rem = divmod(xe, n)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1898
            if rem != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1899
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1900
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1901
            # compute nth root of xc using Newton's method
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1902
            a = 1L << -(-_nbits(xc)//n) # initial estimate
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1903
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1904
                q, r = divmod(xc, a**(n-1))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1905
                if a <= q:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1906
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1907
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1908
                    a = (a*(n-1) + q)//n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1909
            if not (a == q and r == 0):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1910
                return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1911
            xc = a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1912
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1913
        # now xc*10**xe is the nth root of the original xc*10**xe
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1914
        # compute mth power of xc*10**xe
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1915
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1916
        # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m >
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1917
        # 10**p and the result is not representable.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1918
        if xc > 1 and m > p*100//_log10_lb(xc):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1919
            return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1920
        xc = xc**m
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1921
        xe *= m
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1922
        if xc > 10**p:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1923
            return None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1924
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1925
        # by this point the result *is* exactly representable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1926
        # adjust the exponent to get as close as possible to the ideal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1927
        # exponent, if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1928
        str_xc = str(xc)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1929
        if other._isinteger() and other._sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1930
            ideal_exponent = self._exp*int(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1931
            zeros = min(xe-ideal_exponent, p-len(str_xc))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1932
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1933
            zeros = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1934
        return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1935
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1936
    def __pow__(self, other, modulo=None, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1937
        """Return self ** other [ % modulo].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1938
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1939
        With two arguments, compute self**other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1940
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1941
        With three arguments, compute (self**other) % modulo.  For the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1942
        three argument form, the following restrictions on the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1943
        arguments hold:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1944
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1945
         - all three arguments must be integral
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1946
         - other must be nonnegative
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1947
         - either self or other (or both) must be nonzero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1948
         - modulo must be nonzero and must have at most p digits,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1949
           where p is the context precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1950
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1951
        If any of these restrictions is violated the InvalidOperation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1952
        flag is raised.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1953
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1954
        The result of pow(self, other, modulo) is identical to the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1955
        result that would be obtained by computing (self**other) %
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1956
        modulo with unbounded precision, but is computed more
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1957
        efficiently.  It is always exact.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1958
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1959
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1960
        if modulo is not None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1961
            return self._power_modulo(other, modulo, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1962
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1963
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1964
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1965
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1966
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1967
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1968
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1969
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1970
        # either argument is a NaN => result is NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1971
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1972
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1973
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1974
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1975
        # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1976
        if not other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1977
            if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1978
                return context._raise_error(InvalidOperation, '0 ** 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1979
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1980
                return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1981
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1982
        # result has sign 1 iff self._sign is 1 and other is an odd integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1983
        result_sign = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1984
        if self._sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1985
            if other._isinteger():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1986
                if not other._iseven():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1987
                    result_sign = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1988
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1989
                # -ve**noninteger = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1990
                # (-0)**noninteger = 0**noninteger
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1991
                if self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1992
                    return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1993
                        'x ** y with x negative and y not an integer')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1994
            # negate self, without doing any unwanted rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1995
            self = self.copy_negate()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1996
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1997
        # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1998
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  1999
            if other._sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2000
                return _dec_from_triple(result_sign, '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2001
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2002
                return Infsign[result_sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2003
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2004
        # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2005
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2006
            if other._sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2007
                return Infsign[result_sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2008
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2009
                return _dec_from_triple(result_sign, '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2010
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2011
        # 1**other = 1, but the choice of exponent and the flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2012
        # depend on the exponent of self, and on whether other is a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2013
        # positive integer, a negative integer, or neither
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2014
        if self == Dec_p1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2015
            if other._isinteger():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2016
                # exp = max(self._exp*max(int(other), 0),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2017
                # 1-context.prec) but evaluating int(other) directly
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2018
                # is dangerous until we know other is small (other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2019
                # could be 1e999999999)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2020
                if other._sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2021
                    multiplier = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2022
                elif other > context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2023
                    multiplier = context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2024
                else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2025
                    multiplier = int(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2026
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2027
                exp = self._exp * multiplier
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2028
                if exp < 1-context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2029
                    exp = 1-context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2030
                    context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2031
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2032
                context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2033
                context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2034
                exp = 1-context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2035
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2036
            return _dec_from_triple(result_sign, '1'+'0'*-exp, exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2037
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2038
        # compute adjusted exponent of self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2039
        self_adj = self.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2040
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2041
        # self ** infinity is infinity if self > 1, 0 if self < 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2042
        # self ** -infinity is infinity if self < 1, 0 if self > 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2043
        if other._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2044
            if (other._sign == 0) == (self_adj < 0):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2045
                return _dec_from_triple(result_sign, '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2046
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2047
                return Infsign[result_sign]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2048
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2049
        # from here on, the result always goes through the call
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2050
        # to _fix at the end of this function.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2051
        ans = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2052
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2053
        # crude test to catch cases of extreme overflow/underflow.  If
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2054
        # log10(self)*other >= 10**bound and bound >= len(str(Emax))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2055
        # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2056
        # self**other >= 10**(Emax+1), so overflow occurs.  The test
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2057
        # for underflow is similar.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2058
        bound = self._log10_exp_bound() + other.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2059
        if (self_adj >= 0) == (other._sign == 0):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2060
            # self > 1 and other +ve, or self < 1 and other -ve
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2061
            # possibility of overflow
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2062
            if bound >= len(str(context.Emax)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2063
                ans = _dec_from_triple(result_sign, '1', context.Emax+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2064
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2065
            # self > 1 and other -ve, or self < 1 and other +ve
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2066
            # possibility of underflow to 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2067
            Etiny = context.Etiny()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2068
            if bound >= len(str(-Etiny)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2069
                ans = _dec_from_triple(result_sign, '1', Etiny-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2070
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2071
        # try for an exact result with precision +1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2072
        if ans is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2073
            ans = self._power_exact(other, context.prec + 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2074
            if ans is not None and result_sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2075
                ans = _dec_from_triple(1, ans._int, ans._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2076
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2077
        # usual case: inexact result, x**y computed directly as exp(y*log(x))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2078
        if ans is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2079
            p = context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2080
            x = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2081
            xc, xe = x.int, x.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2082
            y = _WorkRep(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2083
            yc, ye = y.int, y.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2084
            if y.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2085
                yc = -yc
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2086
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2087
            # compute correctly rounded result:  start with precision +3,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2088
            # then increase precision until result is unambiguously roundable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2089
            extra = 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2090
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2091
                coeff, exp = _dpower(xc, xe, yc, ye, p+extra)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2092
                if coeff % (5*10**(len(str(coeff))-p-1)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2093
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2094
                extra += 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2095
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2096
            ans = _dec_from_triple(result_sign, str(coeff), exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2097
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2098
        # the specification says that for non-integer other we need to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2099
        # raise Inexact, even when the result is actually exact.  In
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2100
        # the same way, we need to raise Underflow here if the result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2101
        # is subnormal.  (The call to _fix will take care of raising
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2102
        # Rounded and Subnormal, as usual.)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2103
        if not other._isinteger():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2104
            context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2105
            # pad with zeros up to length context.prec+1 if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2106
            if len(ans._int) <= context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2107
                expdiff = context.prec+1 - len(ans._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2108
                ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2109
                                       ans._exp-expdiff)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2110
            if ans.adjusted() < context.Emin:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2111
                context._raise_error(Underflow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2112
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2113
        # unlike exp, ln and log10, the power function respects the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2114
        # rounding mode; no need to use ROUND_HALF_EVEN here
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2115
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2116
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2117
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2118
    def __rpow__(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2119
        """Swaps self/other and returns __pow__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2120
        other = _convert_other(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2121
        if other is NotImplemented:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2122
            return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2123
        return other.__pow__(self, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2124
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2125
    def normalize(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2126
        """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2127
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2128
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2129
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2130
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2131
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2132
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2133
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2134
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2135
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2136
        dup = self._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2137
        if dup._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2138
            return dup
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2139
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2140
        if not dup:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2141
            return _dec_from_triple(dup._sign, '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2142
        exp_max = [context.Emax, context.Etop()][context._clamp]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2143
        end = len(dup._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2144
        exp = dup._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2145
        while dup._int[end-1] == '0' and exp < exp_max:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2146
            exp += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2147
            end -= 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2148
        return _dec_from_triple(dup._sign, dup._int[:end], exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2149
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2150
    def quantize(self, exp, rounding=None, context=None, watchexp=True):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2151
        """Quantize self so its exponent is the same as that of exp.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2152
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2153
        Similar to self._rescale(exp._exp) but with error checking.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2154
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2155
        exp = _convert_other(exp, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2156
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2157
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2158
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2159
        if rounding is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2160
            rounding = context.rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2161
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2162
        if self._is_special or exp._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2163
            ans = self._check_nans(exp, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2164
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2165
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2166
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2167
            if exp._isinfinity() or self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2168
                if exp._isinfinity() and self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2169
                    return Decimal(self)  # if both are inf, it is OK
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2170
                return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2171
                                        'quantize with one INF')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2172
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2173
        # if we're not watching exponents, do a simple rescale
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2174
        if not watchexp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2175
            ans = self._rescale(exp._exp, rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2176
            # raise Inexact and Rounded where appropriate
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2177
            if ans._exp > self._exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2178
                context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2179
                if ans != self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2180
                    context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2181
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2182
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2183
        # exp._exp should be between Etiny and Emax
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2184
        if not (context.Etiny() <= exp._exp <= context.Emax):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2185
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2186
                   'target exponent out of bounds in quantize')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2187
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2188
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2189
            ans = _dec_from_triple(self._sign, '0', exp._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2190
            return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2191
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2192
        self_adjusted = self.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2193
        if self_adjusted > context.Emax:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2194
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2195
                                        'exponent of quantize result too large for current context')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2196
        if self_adjusted - exp._exp + 1 > context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2197
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2198
                                        'quantize result has too many digits for current context')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2199
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2200
        ans = self._rescale(exp._exp, rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2201
        if ans.adjusted() > context.Emax:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2202
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2203
                                        'exponent of quantize result too large for current context')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2204
        if len(ans._int) > context.prec:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2205
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2206
                                        'quantize result has too many digits for current context')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2207
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2208
        # raise appropriate flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2209
        if ans._exp > self._exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2210
            context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2211
            if ans != self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2212
                context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2213
        if ans and ans.adjusted() < context.Emin:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2214
            context._raise_error(Subnormal)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2215
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2216
        # call to fix takes care of any necessary folddown
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2217
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2218
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2219
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2220
    def same_quantum(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2221
        """Return True if self and other have the same exponent; otherwise
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2222
        return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2223
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2224
        If either operand is a special value, the following rules are used:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2225
           * return True if both operands are infinities
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2226
           * return True if both operands are NaNs
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2227
           * otherwise, return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2228
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2229
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2230
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2231
            return (self.is_nan() and other.is_nan() or
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2232
                    self.is_infinite() and other.is_infinite())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2233
        return self._exp == other._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2234
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2235
    def _rescale(self, exp, rounding):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2236
        """Rescale self so that the exponent is exp, either by padding with zeros
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2237
        or by truncating digits, using the given rounding mode.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2238
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2239
        Specials are returned without change.  This operation is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2240
        quiet: it raises no flags, and uses no information from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2241
        context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2242
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2243
        exp = exp to scale to (an integer)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2244
        rounding = rounding mode
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2245
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2246
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2247
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2248
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2249
            return _dec_from_triple(self._sign, '0', exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2250
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2251
        if self._exp >= exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2252
            # pad answer with zeros if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2253
            return _dec_from_triple(self._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2254
                                        self._int + '0'*(self._exp - exp), exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2255
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2256
        # too many digits; round and lose data.  If self.adjusted() <
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2257
        # exp-1, replace self by 10**(exp-1) before rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2258
        digits = len(self._int) + self._exp - exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2259
        if digits < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2260
            self = _dec_from_triple(self._sign, '1', exp-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2261
            digits = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2262
        this_function = getattr(self, self._pick_rounding_function[rounding])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2263
        changed = this_function(digits)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2264
        coeff = self._int[:digits] or '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2265
        if changed == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2266
            coeff = str(int(coeff)+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2267
        return _dec_from_triple(self._sign, coeff, exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2268
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2269
    def to_integral_exact(self, rounding=None, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2270
        """Rounds to a nearby integer.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2271
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2272
        If no rounding mode is specified, take the rounding mode from
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2273
        the context.  This method raises the Rounded and Inexact flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2274
        when appropriate.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2275
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2276
        See also: to_integral_value, which does exactly the same as
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2277
        this method except that it doesn't raise Inexact or Rounded.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2278
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2279
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2280
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2281
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2282
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2283
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2284
        if self._exp >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2285
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2286
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2287
            return _dec_from_triple(self._sign, '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2288
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2289
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2290
        if rounding is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2291
            rounding = context.rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2292
        context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2293
        ans = self._rescale(0, rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2294
        if ans != self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2295
            context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2296
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2297
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2298
    def to_integral_value(self, rounding=None, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2299
        """Rounds to the nearest integer, without raising inexact, rounded."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2300
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2301
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2302
        if rounding is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2303
            rounding = context.rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2304
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2305
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2306
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2307
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2308
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2309
        if self._exp >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2310
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2311
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2312
            return self._rescale(0, rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2313
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2314
    # the method name changed, but we provide also the old one, for compatibility
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2315
    to_integral = to_integral_value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2316
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2317
    def sqrt(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2318
        """Return the square root of self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2319
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2320
            ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2321
            if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2322
                return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2323
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2324
            if self._isinfinity() and self._sign == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2325
                return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2326
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2327
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2328
            # exponent = self._exp // 2.  sqrt(-0) = -0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2329
            ans = _dec_from_triple(self._sign, '0', self._exp // 2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2330
            return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2331
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2332
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2333
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2334
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2335
        if self._sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2336
            return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2337
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2338
        # At this point self represents a positive number.  Let p be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2339
        # the desired precision and express self in the form c*100**e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2340
        # with c a positive real number and e an integer, c and e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2341
        # being chosen so that 100**(p-1) <= c < 100**p.  Then the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2342
        # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2343
        # <= sqrt(c) < 10**p, so the closest representable Decimal at
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2344
        # precision p is n*10**e where n = round_half_even(sqrt(c)),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2345
        # the closest integer to sqrt(c) with the even integer chosen
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2346
        # in the case of a tie.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2347
        #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2348
        # To ensure correct rounding in all cases, we use the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2349
        # following trick: we compute the square root to an extra
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2350
        # place (precision p+1 instead of precision p), rounding down.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2351
        # Then, if the result is inexact and its last digit is 0 or 5,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2352
        # we increase the last digit to 1 or 6 respectively; if it's
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2353
        # exact we leave the last digit alone.  Now the final round to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2354
        # p places (or fewer in the case of underflow) will round
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2355
        # correctly and raise the appropriate flags.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2356
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2357
        # use an extra digit of precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2358
        prec = context.prec+1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2359
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2360
        # write argument in the form c*100**e where e = self._exp//2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2361
        # is the 'ideal' exponent, to be used if the square root is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2362
        # exactly representable.  l is the number of 'digits' of c in
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2363
        # base 100, so that 100**(l-1) <= c < 100**l.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2364
        op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2365
        e = op.exp >> 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2366
        if op.exp & 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2367
            c = op.int * 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2368
            l = (len(self._int) >> 1) + 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2369
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2370
            c = op.int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2371
            l = len(self._int)+1 >> 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2372
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2373
        # rescale so that c has exactly prec base 100 'digits'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2374
        shift = prec-l
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2375
        if shift >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2376
            c *= 100**shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2377
            exact = True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2378
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2379
            c, remainder = divmod(c, 100**-shift)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2380
            exact = not remainder
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2381
        e -= shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2382
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2383
        # find n = floor(sqrt(c)) using Newton's method
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2384
        n = 10**prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2385
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2386
            q = c//n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2387
            if n <= q:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2388
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2389
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2390
                n = n + q >> 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2391
        exact = exact and n*n == c
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2392
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2393
        if exact:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2394
            # result is exact; rescale to use ideal exponent e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2395
            if shift >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2396
                # assert n % 10**shift == 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2397
                n //= 10**shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2398
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2399
                n *= 10**-shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2400
            e += shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2401
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2402
            # result is not exact; fix last digit as described above
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2403
            if n % 5 == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2404
                n += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2405
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2406
        ans = _dec_from_triple(0, str(n), e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2407
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2408
        # round, and fit to current context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2409
        context = context._shallow_copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2410
        rounding = context._set_rounding(ROUND_HALF_EVEN)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2411
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2412
        context.rounding = rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2413
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2414
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2415
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2416
    def max(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2417
        """Returns the larger value.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2418
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2419
        Like max(self, other) except if one is not a number, returns
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2420
        NaN (and signals if one is sNaN).  Also rounds.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2421
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2422
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2423
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2424
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2425
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2426
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2427
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2428
            # If one operand is a quiet NaN and the other is number, then the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2429
            # number is always returned
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2430
            sn = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2431
            on = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2432
            if sn or on:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2433
                if on == 1 and sn != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2434
                    return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2435
                if sn == 1 and on != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2436
                    return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2437
                return self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2438
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2439
        c = self.__cmp__(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2440
        if c == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2441
            # If both operands are finite and equal in numerical value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2442
            # then an ordering is applied:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2443
            #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2444
            # If the signs differ then max returns the operand with the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2445
            # positive sign and min returns the operand with the negative sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2446
            #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2447
            # If the signs are the same then the exponent is used to select
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2448
            # the result.  This is exactly the ordering used in compare_total.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2449
            c = self.compare_total(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2450
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2451
        if c == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2452
            ans = other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2453
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2454
            ans = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2455
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2456
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2457
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2458
    def min(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2459
        """Returns the smaller value.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2460
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2461
        Like min(self, other) except if one is not a number, returns
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2462
        NaN (and signals if one is sNaN).  Also rounds.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2463
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2464
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2465
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2466
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2467
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2468
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2469
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2470
            # If one operand is a quiet NaN and the other is number, then the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2471
            # number is always returned
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2472
            sn = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2473
            on = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2474
            if sn or on:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2475
                if on == 1 and sn != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2476
                    return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2477
                if sn == 1 and on != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2478
                    return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2479
                return self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2480
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2481
        c = self.__cmp__(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2482
        if c == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2483
            c = self.compare_total(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2484
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2485
        if c == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2486
            ans = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2487
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2488
            ans = other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2489
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2490
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2491
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2492
    def _isinteger(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2493
        """Returns whether self is an integer"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2494
        if self._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2495
            return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2496
        if self._exp >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2497
            return True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2498
        rest = self._int[self._exp:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2499
        return rest == '0'*len(rest)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2500
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2501
    def _iseven(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2502
        """Returns True if self is even.  Assumes self is an integer."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2503
        if not self or self._exp > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2504
            return True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2505
        return self._int[-1+self._exp] in '02468'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2506
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2507
    def adjusted(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2508
        """Return the adjusted exponent of self"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2509
        try:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2510
            return self._exp + len(self._int) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2511
        # If NaN or Infinity, self._exp is string
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2512
        except TypeError:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2513
            return 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2514
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2515
    def canonical(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2516
        """Returns the same Decimal object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2517
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2518
        As we do not have different encodings for the same number, the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2519
        received object already is in its canonical form.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2520
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2521
        return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2522
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2523
    def compare_signal(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2524
        """Compares self to the other operand numerically.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2525
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2526
        It's pretty much like compare(), but all NaNs signal, with signaling
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2527
        NaNs taking precedence over quiet NaNs.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2528
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2529
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2530
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2531
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2532
        self_is_nan = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2533
        other_is_nan = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2534
        if self_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2535
            return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2536
                                        self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2537
        if other_is_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2538
            return context._raise_error(InvalidOperation, 'sNaN',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2539
                                        other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2540
        if self_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2541
            return context._raise_error(InvalidOperation, 'NaN in compare_signal',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2542
                                        self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2543
        if other_is_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2544
            return context._raise_error(InvalidOperation, 'NaN in compare_signal',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2545
                                        other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2546
        return self.compare(other, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2547
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2548
    def compare_total(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2549
        """Compares self to other using the abstract representations.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2550
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2551
        This is not like the standard compare, which use their numerical
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2552
        value. Note that a total ordering is defined for all possible abstract
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2553
        representations.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2554
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2555
        # if one is negative and the other is positive, it's easy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2556
        if self._sign and not other._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2557
            return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2558
        if not self._sign and other._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2559
            return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2560
        sign = self._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2561
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2562
        # let's handle both NaN types
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2563
        self_nan = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2564
        other_nan = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2565
        if self_nan or other_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2566
            if self_nan == other_nan:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2567
                if self._int < other._int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2568
                    if sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2569
                        return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2570
                    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2571
                        return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2572
                if self._int > other._int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2573
                    if sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2574
                        return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2575
                    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2576
                        return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2577
                return Dec_0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2578
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2579
            if sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2580
                if self_nan == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2581
                    return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2582
                if other_nan == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2583
                    return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2584
                if self_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2585
                    return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2586
                if other_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2587
                    return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2588
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2589
                if self_nan == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2590
                    return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2591
                if other_nan == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2592
                    return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2593
                if self_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2594
                    return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2595
                if other_nan == 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2596
                    return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2597
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2598
        if self < other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2599
            return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2600
        if self > other:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2601
            return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2602
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2603
        if self._exp < other._exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2604
            if sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2605
                return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2606
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2607
                return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2608
        if self._exp > other._exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2609
            if sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2610
                return Dec_n1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2611
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2612
                return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2613
        return Dec_0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2614
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2615
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2616
    def compare_total_mag(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2617
        """Compares self to other using abstract repr., ignoring sign.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2618
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2619
        Like compare_total, but with operand's sign ignored and assumed to be 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2620
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2621
        s = self.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2622
        o = other.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2623
        return s.compare_total(o)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2624
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2625
    def copy_abs(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2626
        """Returns a copy with the sign set to 0. """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2627
        return _dec_from_triple(0, self._int, self._exp, self._is_special)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2628
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2629
    def copy_negate(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2630
        """Returns a copy with the sign inverted."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2631
        if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2632
            return _dec_from_triple(0, self._int, self._exp, self._is_special)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2633
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2634
            return _dec_from_triple(1, self._int, self._exp, self._is_special)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2635
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2636
    def copy_sign(self, other):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2637
        """Returns self with the sign of other."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2638
        return _dec_from_triple(other._sign, self._int,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2639
                                self._exp, self._is_special)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2640
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2641
    def exp(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2642
        """Returns e ** self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2643
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2644
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2645
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2646
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2647
        # exp(NaN) = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2648
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2649
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2650
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2651
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2652
        # exp(-Infinity) = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2653
        if self._isinfinity() == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2654
            return Dec_0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2655
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2656
        # exp(0) = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2657
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2658
            return Dec_p1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2659
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2660
        # exp(Infinity) = Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2661
        if self._isinfinity() == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2662
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2663
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2664
        # the result is now guaranteed to be inexact (the true
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2665
        # mathematical result is transcendental). There's no need to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2666
        # raise Rounded and Inexact here---they'll always be raised as
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2667
        # a result of the call to _fix.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2668
        p = context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2669
        adj = self.adjusted()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2670
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2671
        # we only need to do any computation for quite a small range
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2672
        # of adjusted exponents---for example, -29 <= adj <= 10 for
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2673
        # the default context.  For smaller exponent the result is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2674
        # indistinguishable from 1 at the given precision, while for
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2675
        # larger exponent the result either overflows or underflows.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2676
        if self._sign == 0 and adj > len(str((context.Emax+1)*3)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2677
            # overflow
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2678
            ans = _dec_from_triple(0, '1', context.Emax+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2679
        elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2680
            # underflow to 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2681
            ans = _dec_from_triple(0, '1', context.Etiny()-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2682
        elif self._sign == 0 and adj < -p:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2683
            # p+1 digits; final round will raise correct flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2684
            ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2685
        elif self._sign == 1 and adj < -p-1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2686
            # p+1 digits; final round will raise correct flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2687
            ans = _dec_from_triple(0, '9'*(p+1), -p-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2688
        # general case
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2689
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2690
            op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2691
            c, e = op.int, op.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2692
            if op.sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2693
                c = -c
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2694
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2695
            # compute correctly rounded result: increase precision by
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2696
            # 3 digits at a time until we get an unambiguously
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2697
            # roundable result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2698
            extra = 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2699
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2700
                coeff, exp = _dexp(c, e, p+extra)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2701
                if coeff % (5*10**(len(str(coeff))-p-1)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2702
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2703
                extra += 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2704
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2705
            ans = _dec_from_triple(0, str(coeff), exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2706
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2707
        # at this stage, ans should round correctly with *any*
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2708
        # rounding mode, not just with ROUND_HALF_EVEN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2709
        context = context._shallow_copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2710
        rounding = context._set_rounding(ROUND_HALF_EVEN)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2711
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2712
        context.rounding = rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2713
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2714
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2715
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2716
    def is_canonical(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2717
        """Return True if self is canonical; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2718
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2719
        Currently, the encoding of a Decimal instance is always
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2720
        canonical, so this method returns True for any Decimal.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2721
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2722
        return True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2723
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2724
    def is_finite(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2725
        """Return True if self is finite; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2726
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2727
        A Decimal instance is considered finite if it is neither
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2728
        infinite nor a NaN.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2729
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2730
        return not self._is_special
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2731
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2732
    def is_infinite(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2733
        """Return True if self is infinite; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2734
        return self._exp == 'F'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2735
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2736
    def is_nan(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2737
        """Return True if self is a qNaN or sNaN; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2738
        return self._exp in ('n', 'N')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2739
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2740
    def is_normal(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2741
        """Return True if self is a normal number; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2742
        if self._is_special or not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2743
            return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2744
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2745
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2746
        return context.Emin <= self.adjusted() <= context.Emax
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2747
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2748
    def is_qnan(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2749
        """Return True if self is a quiet NaN; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2750
        return self._exp == 'n'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2751
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2752
    def is_signed(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2753
        """Return True if self is negative; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2754
        return self._sign == 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2755
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2756
    def is_snan(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2757
        """Return True if self is a signaling NaN; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2758
        return self._exp == 'N'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2759
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2760
    def is_subnormal(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2761
        """Return True if self is subnormal; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2762
        if self._is_special or not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2763
            return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2764
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2765
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2766
        return self.adjusted() < context.Emin
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2767
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2768
    def is_zero(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2769
        """Return True if self is a zero; otherwise return False."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2770
        return not self._is_special and self._int == '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2771
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2772
    def _ln_exp_bound(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2773
        """Compute a lower bound for the adjusted exponent of self.ln().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2774
        In other words, compute r such that self.ln() >= 10**r.  Assumes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2775
        that self is finite and positive and that self != 1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2776
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2777
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2778
        # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2779
        adj = self._exp + len(self._int) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2780
        if adj >= 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2781
            # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2782
            return len(str(adj*23//10)) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2783
        if adj <= -2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2784
            # argument <= 0.1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2785
            return len(str((-1-adj)*23//10)) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2786
        op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2787
        c, e = op.int, op.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2788
        if adj == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2789
            # 1 < self < 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2790
            num = str(c-10**-e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2791
            den = str(c)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2792
            return len(num) - len(den) - (num < den)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2793
        # adj == -1, 0.1 <= self < 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2794
        return e + len(str(10**-e - c)) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2795
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2796
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2797
    def ln(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2798
        """Returns the natural (base e) logarithm of self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2799
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2800
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2801
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2802
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2803
        # ln(NaN) = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2804
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2805
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2806
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2807
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2808
        # ln(0.0) == -Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2809
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2810
            return negInf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2811
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2812
        # ln(Infinity) = Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2813
        if self._isinfinity() == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2814
            return Inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2815
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2816
        # ln(1.0) == 0.0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2817
        if self == Dec_p1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2818
            return Dec_0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2819
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2820
        # ln(negative) raises InvalidOperation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2821
        if self._sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2822
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2823
                                        'ln of a negative value')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2824
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2825
        # result is irrational, so necessarily inexact
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2826
        op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2827
        c, e = op.int, op.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2828
        p = context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2829
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2830
        # correctly rounded result: repeatedly increase precision by 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2831
        # until we get an unambiguously roundable result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2832
        places = p - self._ln_exp_bound() + 2 # at least p+3 places
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2833
        while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2834
            coeff = _dlog(c, e, places)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2835
            # assert len(str(abs(coeff)))-p >= 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2836
            if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2837
                break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2838
            places += 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2839
        ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2840
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2841
        context = context._shallow_copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2842
        rounding = context._set_rounding(ROUND_HALF_EVEN)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2843
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2844
        context.rounding = rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2845
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2846
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2847
    def _log10_exp_bound(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2848
        """Compute a lower bound for the adjusted exponent of self.log10().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2849
        In other words, find r such that self.log10() >= 10**r.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2850
        Assumes that self is finite and positive and that self != 1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2851
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2852
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2853
        # For x >= 10 or x < 0.1 we only need a bound on the integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2854
        # part of log10(self), and this comes directly from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2855
        # exponent of x.  For 0.1 <= x <= 10 we use the inequalities
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2856
        # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| >
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2857
        # (1-1/x)/2.31 > 0.  If x < 1 then |log10(x)| > (1-x)/2.31 > 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2858
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2859
        adj = self._exp + len(self._int) - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2860
        if adj >= 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2861
            # self >= 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2862
            return len(str(adj))-1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2863
        if adj <= -2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2864
            # self < 0.1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2865
            return len(str(-1-adj))-1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2866
        op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2867
        c, e = op.int, op.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2868
        if adj == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2869
            # 1 < self < 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2870
            num = str(c-10**-e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2871
            den = str(231*c)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2872
            return len(num) - len(den) - (num < den) + 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2873
        # adj == -1, 0.1 <= self < 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2874
        num = str(10**-e-c)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2875
        return len(num) + e - (num < "231") - 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2876
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2877
    def log10(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2878
        """Returns the base 10 logarithm of self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2879
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2880
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2881
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2882
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2883
        # log10(NaN) = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2884
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2885
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2886
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2887
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2888
        # log10(0.0) == -Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2889
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2890
            return negInf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2891
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2892
        # log10(Infinity) = Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2893
        if self._isinfinity() == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2894
            return Inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2895
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2896
        # log10(negative or -Infinity) raises InvalidOperation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2897
        if self._sign == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2898
            return context._raise_error(InvalidOperation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2899
                                        'log10 of a negative value')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2900
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2901
        # log10(10**n) = n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2902
        if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2903
            # answer may need rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2904
            ans = Decimal(self._exp + len(self._int) - 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2905
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2906
            # result is irrational, so necessarily inexact
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2907
            op = _WorkRep(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2908
            c, e = op.int, op.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2909
            p = context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2910
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2911
            # correctly rounded result: repeatedly increase precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2912
            # until result is unambiguously roundable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2913
            places = p-self._log10_exp_bound()+2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2914
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2915
                coeff = _dlog10(c, e, places)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2916
                # assert len(str(abs(coeff)))-p >= 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2917
                if coeff % (5*10**(len(str(abs(coeff)))-p-1)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2918
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2919
                places += 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2920
            ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2921
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2922
        context = context._shallow_copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2923
        rounding = context._set_rounding(ROUND_HALF_EVEN)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2924
        ans = ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2925
        context.rounding = rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2926
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2927
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2928
    def logb(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2929
        """ Returns the exponent of the magnitude of self's MSD.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2930
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2931
        The result is the integer which is the exponent of the magnitude
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2932
        of the most significant digit of self (as though it were truncated
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2933
        to a single digit while maintaining the value of that digit and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2934
        without limiting the resulting exponent).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2935
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2936
        # logb(NaN) = NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2937
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2938
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2939
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2940
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2941
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2942
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2943
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2944
        # logb(+/-Inf) = +Inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2945
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2946
            return Inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2947
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2948
        # logb(0) = -Inf, DivisionByZero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2949
        if not self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2950
            return context._raise_error(DivisionByZero, 'logb(0)', 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2951
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2952
        # otherwise, simply return the adjusted exponent of self, as a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2953
        # Decimal.  Note that no attempt is made to fit the result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2954
        # into the current context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2955
        return Decimal(self.adjusted())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2956
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2957
    def _islogical(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2958
        """Return True if self is a logical operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2959
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2960
        For being logical, it must be a finite numbers with a sign of 0,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2961
        an exponent of 0, and a coefficient whose digits must all be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2962
        either 0 or 1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2963
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2964
        if self._sign != 0 or self._exp != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2965
            return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2966
        for dig in self._int:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2967
            if dig not in '01':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2968
                return False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2969
        return True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2970
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2971
    def _fill_logical(self, context, opa, opb):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2972
        dif = context.prec - len(opa)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2973
        if dif > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2974
            opa = '0'*dif + opa
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2975
        elif dif < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2976
            opa = opa[-context.prec:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2977
        dif = context.prec - len(opb)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2978
        if dif > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2979
            opb = '0'*dif + opb
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2980
        elif dif < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2981
            opb = opb[-context.prec:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2982
        return opa, opb
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2983
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2984
    def logical_and(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2985
        """Applies an 'and' operation between self and other's digits."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2986
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2987
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2988
        if not self._islogical() or not other._islogical():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2989
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2990
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2991
        # fill to context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2992
        (opa, opb) = self._fill_logical(context, self._int, other._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2993
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2994
        # make the operation, and clean starting zeroes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2995
        result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2996
        return _dec_from_triple(0, result.lstrip('0') or '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2997
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2998
    def logical_invert(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  2999
        """Invert all its digits."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3000
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3001
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3002
        return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3003
                                context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3004
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3005
    def logical_or(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3006
        """Applies an 'or' operation between self and other's digits."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3007
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3008
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3009
        if not self._islogical() or not other._islogical():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3010
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3011
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3012
        # fill to context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3013
        (opa, opb) = self._fill_logical(context, self._int, other._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3014
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3015
        # make the operation, and clean starting zeroes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3016
        result = "".join(str(int(a)|int(b)) for a,b in zip(opa,opb))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3017
        return _dec_from_triple(0, result.lstrip('0') or '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3018
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3019
    def logical_xor(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3020
        """Applies an 'xor' operation between self and other's digits."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3021
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3022
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3023
        if not self._islogical() or not other._islogical():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3024
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3025
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3026
        # fill to context.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3027
        (opa, opb) = self._fill_logical(context, self._int, other._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3028
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3029
        # make the operation, and clean starting zeroes
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3030
        result = "".join(str(int(a)^int(b)) for a,b in zip(opa,opb))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3031
        return _dec_from_triple(0, result.lstrip('0') or '0', 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3032
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3033
    def max_mag(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3034
        """Compares the values numerically with their sign ignored."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3035
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3036
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3037
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3038
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3039
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3040
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3041
            # If one operand is a quiet NaN and the other is number, then the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3042
            # number is always returned
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3043
            sn = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3044
            on = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3045
            if sn or on:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3046
                if on == 1 and sn != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3047
                    return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3048
                if sn == 1 and on != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3049
                    return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3050
                return self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3051
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3052
        c = self.copy_abs().__cmp__(other.copy_abs())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3053
        if c == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3054
            c = self.compare_total(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3055
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3056
        if c == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3057
            ans = other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3058
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3059
            ans = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3060
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3061
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3062
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3063
    def min_mag(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3064
        """Compares the values numerically with their sign ignored."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3065
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3066
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3067
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3068
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3069
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3070
        if self._is_special or other._is_special:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3071
            # If one operand is a quiet NaN and the other is number, then the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3072
            # number is always returned
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3073
            sn = self._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3074
            on = other._isnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3075
            if sn or on:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3076
                if on == 1 and sn != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3077
                    return self._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3078
                if sn == 1 and on != 2:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3079
                    return other._fix_nan(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3080
                return self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3081
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3082
        c = self.copy_abs().__cmp__(other.copy_abs())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3083
        if c == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3084
            c = self.compare_total(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3085
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3086
        if c == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3087
            ans = self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3088
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3089
            ans = other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3090
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3091
        return ans._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3092
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3093
    def next_minus(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3094
        """Returns the largest representable number smaller than itself."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3095
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3096
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3097
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3098
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3099
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3100
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3101
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3102
        if self._isinfinity() == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3103
            return negInf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3104
        if self._isinfinity() == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3105
            return _dec_from_triple(0, '9'*context.prec, context.Etop())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3106
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3107
        context = context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3108
        context._set_rounding(ROUND_FLOOR)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3109
        context._ignore_all_flags()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3110
        new_self = self._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3111
        if new_self != self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3112
            return new_self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3113
        return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3114
                            context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3115
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3116
    def next_plus(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3117
        """Returns the smallest representable number larger than itself."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3118
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3119
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3120
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3121
        ans = self._check_nans(context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3122
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3123
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3124
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3125
        if self._isinfinity() == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3126
            return Inf
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3127
        if self._isinfinity() == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3128
            return _dec_from_triple(1, '9'*context.prec, context.Etop())
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3129
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3130
        context = context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3131
        context._set_rounding(ROUND_CEILING)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3132
        context._ignore_all_flags()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3133
        new_self = self._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3134
        if new_self != self:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3135
            return new_self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3136
        return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3137
                            context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3138
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3139
    def next_toward(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3140
        """Returns the number closest to self, in the direction towards other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3141
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3142
        The result is the closest representable number to self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3143
        (excluding self) that is in the direction towards other,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3144
        unless both have the same value.  If the two operands are
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3145
        numerically equal, then the result is a copy of self with the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3146
        sign set to be the same as the sign of other.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3147
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3148
        other = _convert_other(other, raiseit=True)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3149
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3150
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3151
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3152
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3153
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3154
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3155
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3156
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3157
        comparison = self.__cmp__(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3158
        if comparison == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3159
            return self.copy_sign(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3160
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3161
        if comparison == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3162
            ans = self.next_plus(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3163
        else: # comparison == 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3164
            ans = self.next_minus(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3165
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3166
        # decide which flags to raise using value of ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3167
        if ans._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3168
            context._raise_error(Overflow,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3169
                                 'Infinite result from next_toward',
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3170
                                 ans._sign)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3171
            context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3172
            context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3173
        elif ans.adjusted() < context.Emin:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3174
            context._raise_error(Underflow)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3175
            context._raise_error(Subnormal)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3176
            context._raise_error(Rounded)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3177
            context._raise_error(Inexact)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3178
            # if precision == 1 then we don't raise Clamped for a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3179
            # result 0E-Etiny.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3180
            if not ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3181
                context._raise_error(Clamped)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3182
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3183
        return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3184
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3185
    def number_class(self, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3186
        """Returns an indication of the class of self.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3187
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3188
        The class is one of the following strings:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3189
          sNaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3190
          NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3191
          -Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3192
          -Normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3193
          -Subnormal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3194
          -Zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3195
          +Zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3196
          +Subnormal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3197
          +Normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3198
          +Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3199
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3200
        if self.is_snan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3201
            return "sNaN"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3202
        if self.is_qnan():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3203
            return "NaN"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3204
        inf = self._isinfinity()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3205
        if inf == 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3206
            return "+Infinity"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3207
        if inf == -1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3208
            return "-Infinity"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3209
        if self.is_zero():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3210
            if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3211
                return "-Zero"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3212
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3213
                return "+Zero"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3214
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3215
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3216
        if self.is_subnormal(context=context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3217
            if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3218
                return "-Subnormal"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3219
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3220
                return "+Subnormal"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3221
        # just a normal, regular, boring number, :)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3222
        if self._sign:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3223
            return "-Normal"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3224
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3225
            return "+Normal"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3226
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3227
    def radix(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3228
        """Just returns 10, as this is Decimal, :)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3229
        return Decimal(10)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3230
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3231
    def rotate(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3232
        """Returns a rotated copy of self, value-of-other times."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3233
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3234
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3235
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3236
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3237
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3238
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3239
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3240
        if other._exp != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3241
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3242
        if not (-context.prec <= int(other) <= context.prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3243
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3244
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3245
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3246
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3247
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3248
        # get values, pad if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3249
        torot = int(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3250
        rotdig = self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3251
        topad = context.prec - len(rotdig)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3252
        if topad:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3253
            rotdig = '0'*topad + rotdig
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3254
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3255
        # let's rotate!
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3256
        rotated = rotdig[torot:] + rotdig[:torot]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3257
        return _dec_from_triple(self._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3258
                                rotated.lstrip('0') or '0', self._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3259
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3260
    def scaleb (self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3261
        """Returns self operand after adding the second value to its exp."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3262
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3263
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3264
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3265
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3266
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3267
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3268
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3269
        if other._exp != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3270
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3271
        liminf = -2 * (context.Emax + context.prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3272
        limsup =  2 * (context.Emax + context.prec)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3273
        if not (liminf <= int(other) <= limsup):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3274
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3275
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3276
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3277
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3278
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3279
        d = _dec_from_triple(self._sign, self._int, self._exp + int(other))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3280
        d = d._fix(context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3281
        return d
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3282
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3283
    def shift(self, other, context=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3284
        """Returns a shifted copy of self, value-of-other times."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3285
        if context is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3286
            context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3287
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3288
        ans = self._check_nans(other, context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3289
        if ans:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3290
            return ans
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3291
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3292
        if other._exp != 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3293
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3294
        if not (-context.prec <= int(other) <= context.prec):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3295
            return context._raise_error(InvalidOperation)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3296
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3297
        if self._isinfinity():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3298
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3299
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3300
        # get values, pad if necessary
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3301
        torot = int(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3302
        if not torot:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3303
            return Decimal(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3304
        rotdig = self._int
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3305
        topad = context.prec - len(rotdig)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3306
        if topad:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3307
            rotdig = '0'*topad + rotdig
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3308
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3309
        # let's shift!
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3310
        if torot < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3311
            rotated = rotdig[:torot]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3312
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3313
            rotated = rotdig + '0'*torot
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3314
            rotated = rotated[-context.prec:]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3315
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3316
        return _dec_from_triple(self._sign,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3317
                                    rotated.lstrip('0') or '0', self._exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3318
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3319
    # Support for pickling, copy, and deepcopy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3320
    def __reduce__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3321
        return (self.__class__, (str(self),))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3322
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3323
    def __copy__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3324
        if type(self) == Decimal:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3325
            return self     # I'm immutable; therefore I am my own clone
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3326
        return self.__class__(str(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3327
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3328
    def __deepcopy__(self, memo):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3329
        if type(self) == Decimal:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3330
            return self     # My components are also immutable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3331
        return self.__class__(str(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3332
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3333
def _dec_from_triple(sign, coefficient, exponent, special=False):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3334
    """Create a decimal instance directly, without any validation,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3335
    normalization (e.g. removal of leading zeros) or argument
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3336
    conversion.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3337
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3338
    This function is for *internal use only*.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3339
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3340
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3341
    self = object.__new__(Decimal)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3342
    self._sign = sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3343
    self._int = coefficient
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3344
    self._exp = exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3345
    self._is_special = special
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3346
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3347
    return self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3348
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3349
##### Context class #######################################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3350
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3351
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3352
# get rounding method function:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3353
rounding_functions = [name for name in Decimal.__dict__.keys()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3354
                                    if name.startswith('_round_')]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3355
for name in rounding_functions:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3356
    # name is like _round_half_even, goes to the global ROUND_HALF_EVEN value.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3357
    globalname = name[1:].upper()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3358
    val = globals()[globalname]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3359
    Decimal._pick_rounding_function[val] = name
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3360
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3361
del name, val, globalname, rounding_functions
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3362
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3363
class _ContextManager(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3364
    """Context manager class to support localcontext().
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3365
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3366
      Sets a copy of the supplied context in __enter__() and restores
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3367
      the previous decimal context in __exit__()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3368
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3369
    def __init__(self, new_context):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3370
        self.new_context = new_context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3371
    def __enter__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3372
        self.saved_context = getcontext()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3373
        setcontext(self.new_context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3374
        return self.new_context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3375
    def __exit__(self, t, v, tb):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3376
        setcontext(self.saved_context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3377
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3378
class Context(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3379
    """Contains the context for a Decimal instance.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3380
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3381
    Contains:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3382
    prec - precision (for use in rounding, division, square roots..)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3383
    rounding - rounding type (how you round)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3384
    traps - If traps[exception] = 1, then the exception is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3385
                    raised when it is caused.  Otherwise, a value is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3386
                    substituted in.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3387
    flags  - When an exception is caused, flags[exception] is incremented.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3388
             (Whether or not the trap_enabler is set)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3389
             Should be reset by user of Decimal instance.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3390
    Emin -   Minimum exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3391
    Emax -   Maximum exponent
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3392
    capitals -      If 1, 1*10^1 is printed as 1E+1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3393
                    If 0, printed as 1e1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3394
    _clamp - If 1, change exponents if too high (Default 0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3395
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3396
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3397
    def __init__(self, prec=None, rounding=None,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3398
                 traps=None, flags=None,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3399
                 Emin=None, Emax=None,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3400
                 capitals=None, _clamp=0,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3401
                 _ignored_flags=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3402
        if flags is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3403
            flags = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3404
        if _ignored_flags is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3405
            _ignored_flags = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3406
        if not isinstance(flags, dict):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3407
            flags = dict([(s,s in flags) for s in _signals])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3408
            del s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3409
        if traps is not None and not isinstance(traps, dict):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3410
            traps = dict([(s,s in traps) for s in _signals])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3411
            del s
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3412
        for name, val in locals().items():
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3413
            if val is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3414
                setattr(self, name, _copy.copy(getattr(DefaultContext, name)))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3415
            else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3416
                setattr(self, name, val)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3417
        del self.self
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3418
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3419
    def __repr__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3420
        """Show the current context."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3421
        s = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3422
        s.append('Context(prec=%(prec)d, rounding=%(rounding)s, '
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3423
                 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3424
                 % vars(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3425
        names = [f.__name__ for f, v in self.flags.items() if v]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3426
        s.append('flags=[' + ', '.join(names) + ']')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3427
        names = [t.__name__ for t, v in self.traps.items() if v]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3428
        s.append('traps=[' + ', '.join(names) + ']')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3429
        return ', '.join(s) + ')'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3430
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3431
    def clear_flags(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3432
        """Reset all flags to zero"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3433
        for flag in self.flags:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3434
            self.flags[flag] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3435
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3436
    def _shallow_copy(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3437
        """Returns a shallow copy from self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3438
        nc = Context(self.prec, self.rounding, self.traps,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3439
                     self.flags, self.Emin, self.Emax,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3440
                     self.capitals, self._clamp, self._ignored_flags)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3441
        return nc
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3442
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3443
    def copy(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3444
        """Returns a deep copy from self."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3445
        nc = Context(self.prec, self.rounding, self.traps.copy(),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3446
                     self.flags.copy(), self.Emin, self.Emax,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3447
                     self.capitals, self._clamp, self._ignored_flags)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3448
        return nc
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3449
    __copy__ = copy
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3450
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3451
    def _raise_error(self, condition, explanation = None, *args):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3452
        """Handles an error
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3453
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3454
        If the flag is in _ignored_flags, returns the default response.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3455
        Otherwise, it increments the flag, then, if the corresponding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3456
        trap_enabler is set, it reaises the exception.  Otherwise, it returns
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3457
        the default value after incrementing the flag.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3458
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3459
        error = _condition_map.get(condition, condition)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3460
        if error in self._ignored_flags:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3461
            # Don't touch the flag
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3462
            return error().handle(self, *args)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3463
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3464
        self.flags[error] += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3465
        if not self.traps[error]:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3466
            # The errors define how to handle themselves.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3467
            return condition().handle(self, *args)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3468
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3469
        # Errors should only be risked on copies of the context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3470
        # self._ignored_flags = []
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3471
        raise error, explanation
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3472
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3473
    def _ignore_all_flags(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3474
        """Ignore all flags, if they are raised"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3475
        return self._ignore_flags(*_signals)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3476
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3477
    def _ignore_flags(self, *flags):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3478
        """Ignore the flags, if they are raised"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3479
        # Do not mutate-- This way, copies of a context leave the original
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3480
        # alone.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3481
        self._ignored_flags = (self._ignored_flags + list(flags))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3482
        return list(flags)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3483
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3484
    def _regard_flags(self, *flags):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3485
        """Stop ignoring the flags, if they are raised"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3486
        if flags and isinstance(flags[0], (tuple,list)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3487
            flags = flags[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3488
        for flag in flags:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3489
            self._ignored_flags.remove(flag)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3490
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3491
    def __hash__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3492
        """A Context cannot be hashed."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3493
        # We inherit object.__hash__, so we must deny this explicitly
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3494
        raise TypeError("Cannot hash a Context.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3495
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3496
    def Etiny(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3497
        """Returns Etiny (= Emin - prec + 1)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3498
        return int(self.Emin - self.prec + 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3499
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3500
    def Etop(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3501
        """Returns maximum exponent (= Emax - prec + 1)"""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3502
        return int(self.Emax - self.prec + 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3503
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3504
    def _set_rounding(self, type):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3505
        """Sets the rounding type.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3506
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3507
        Sets the rounding type, and returns the current (previous)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3508
        rounding type.  Often used like:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3509
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3510
        context = context.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3511
        # so you don't change the calling context
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3512
        # if an error occurs in the middle.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3513
        rounding = context._set_rounding(ROUND_UP)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3514
        val = self.__sub__(other, context=context)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3515
        context._set_rounding(rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3516
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3517
        This will make it round up for that operation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3518
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3519
        rounding = self.rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3520
        self.rounding= type
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3521
        return rounding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3522
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3523
    def create_decimal(self, num='0'):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3524
        """Creates a new Decimal instance but using self as context."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3525
        d = Decimal(num, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3526
        if d._isnan() and len(d._int) > self.prec - self._clamp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3527
            return self._raise_error(ConversionSyntax,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3528
                                     "diagnostic info too long in NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3529
        return d._fix(self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3530
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3531
    # Methods
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3532
    def abs(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3533
        """Returns the absolute value of the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3534
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3535
        If the operand is negative, the result is the same as using the minus
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3536
        operation on the operand.  Otherwise, the result is the same as using
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3537
        the plus operation on the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3538
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3539
        >>> ExtendedContext.abs(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3540
        Decimal("2.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3541
        >>> ExtendedContext.abs(Decimal('-100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3542
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3543
        >>> ExtendedContext.abs(Decimal('101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3544
        Decimal("101.5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3545
        >>> ExtendedContext.abs(Decimal('-101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3546
        Decimal("101.5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3547
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3548
        return a.__abs__(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3549
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3550
    def add(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3551
        """Return the sum of the two operands.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3552
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3553
        >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3554
        Decimal("19.00")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3555
        >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3556
        Decimal("1.02E+4")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3557
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3558
        return a.__add__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3559
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3560
    def _apply(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3561
        return str(a._fix(self))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3562
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3563
    def canonical(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3564
        """Returns the same Decimal object.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3565
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3566
        As we do not have different encodings for the same number, the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3567
        received object already is in its canonical form.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3568
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3569
        >>> ExtendedContext.canonical(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3570
        Decimal("2.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3571
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3572
        return a.canonical(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3573
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3574
    def compare(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3575
        """Compares values numerically.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3576
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3577
        If the signs of the operands differ, a value representing each operand
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3578
        ('-1' if the operand is less than zero, '0' if the operand is zero or
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3579
        negative zero, or '1' if the operand is greater than zero) is used in
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3580
        place of that operand for the comparison instead of the actual
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3581
        operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3582
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3583
        The comparison is then effected by subtracting the second operand from
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3584
        the first and then returning a value according to the result of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3585
        subtraction: '-1' if the result is less than zero, '0' if the result is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3586
        zero or negative zero, or '1' if the result is greater than zero.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3587
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3588
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3589
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3590
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3591
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3592
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3593
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3594
        >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3595
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3596
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3597
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3598
        >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3599
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3600
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3601
        return a.compare(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3602
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3603
    def compare_signal(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3604
        """Compares the values of the two operands numerically.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3605
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3606
        It's pretty much like compare(), but all NaNs signal, with signaling
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3607
        NaNs taking precedence over quiet NaNs.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3608
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3609
        >>> c = ExtendedContext
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3610
        >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3611
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3612
        >>> c.compare_signal(Decimal('2.1'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3613
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3614
        >>> c.flags[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3615
        >>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3616
        0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3617
        >>> c.compare_signal(Decimal('NaN'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3618
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3619
        >>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3620
        1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3621
        >>> c.flags[InvalidOperation] = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3622
        >>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3623
        0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3624
        >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3625
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3626
        >>> print c.flags[InvalidOperation]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3627
        1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3628
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3629
        return a.compare_signal(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3630
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3631
    def compare_total(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3632
        """Compares two operands using their abstract representation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3633
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3634
        This is not like the standard compare, which use their numerical
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3635
        value. Note that a total ordering is defined for all possible abstract
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3636
        representations.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3637
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3638
        >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3639
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3640
        >>> ExtendedContext.compare_total(Decimal('-127'),  Decimal('12'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3641
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3642
        >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3643
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3644
        >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3645
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3646
        >>> ExtendedContext.compare_total(Decimal('12.3'),  Decimal('12.300'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3647
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3648
        >>> ExtendedContext.compare_total(Decimal('12.3'),  Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3649
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3650
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3651
        return a.compare_total(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3652
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3653
    def compare_total_mag(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3654
        """Compares two operands using their abstract representation ignoring sign.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3655
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3656
        Like compare_total, but with operand's sign ignored and assumed to be 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3657
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3658
        return a.compare_total_mag(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3659
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3660
    def copy_abs(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3661
        """Returns a copy of the operand with the sign set to 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3662
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3663
        >>> ExtendedContext.copy_abs(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3664
        Decimal("2.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3665
        >>> ExtendedContext.copy_abs(Decimal('-100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3666
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3667
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3668
        return a.copy_abs()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3669
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3670
    def copy_decimal(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3671
        """Returns a copy of the decimal objet.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3672
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3673
        >>> ExtendedContext.copy_decimal(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3674
        Decimal("2.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3675
        >>> ExtendedContext.copy_decimal(Decimal('-1.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3676
        Decimal("-1.00")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3677
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3678
        return Decimal(a)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3679
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3680
    def copy_negate(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3681
        """Returns a copy of the operand with the sign inverted.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3682
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3683
        >>> ExtendedContext.copy_negate(Decimal('101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3684
        Decimal("-101.5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3685
        >>> ExtendedContext.copy_negate(Decimal('-101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3686
        Decimal("101.5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3687
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3688
        return a.copy_negate()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3689
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3690
    def copy_sign(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3691
        """Copies the second operand's sign to the first one.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3692
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3693
        In detail, it returns a copy of the first operand with the sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3694
        equal to the sign of the second operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3695
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3696
        >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3697
        Decimal("1.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3698
        >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3699
        Decimal("1.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3700
        >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3701
        Decimal("-1.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3702
        >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3703
        Decimal("-1.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3704
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3705
        return a.copy_sign(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3706
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3707
    def divide(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3708
        """Decimal division in a specified context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3709
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3710
        >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3711
        Decimal("0.333333333")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3712
        >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3713
        Decimal("0.666666667")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3714
        >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3715
        Decimal("2.5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3716
        >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3717
        Decimal("0.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3718
        >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3719
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3720
        >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3721
        Decimal("4.00")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3722
        >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3723
        Decimal("1.20")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3724
        >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3725
        Decimal("10")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3726
        >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3727
        Decimal("1000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3728
        >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3729
        Decimal("1.20E+6")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3730
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3731
        return a.__div__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3732
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3733
    def divide_int(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3734
        """Divides two numbers and returns the integer part of the result.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3735
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3736
        >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3737
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3738
        >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3739
        Decimal("3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3740
        >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3741
        Decimal("3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3742
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3743
        return a.__floordiv__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3744
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3745
    def divmod(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3746
        return a.__divmod__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3747
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3748
    def exp(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3749
        """Returns e ** a.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3750
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3751
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3752
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3753
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3754
        >>> c.exp(Decimal('-Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3755
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3756
        >>> c.exp(Decimal('-1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3757
        Decimal("0.367879441")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3758
        >>> c.exp(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3759
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3760
        >>> c.exp(Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3761
        Decimal("2.71828183")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3762
        >>> c.exp(Decimal('0.693147181'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3763
        Decimal("2.00000000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3764
        >>> c.exp(Decimal('+Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3765
        Decimal("Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3766
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3767
        return a.exp(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3768
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3769
    def fma(self, a, b, c):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3770
        """Returns a multiplied by b, plus c.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3771
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3772
        The first two operands are multiplied together, using multiply,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3773
        the third operand is then added to the result of that
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3774
        multiplication, using add, all with only one final rounding.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3775
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3776
        >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3777
        Decimal("22")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3778
        >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3779
        Decimal("-8")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3780
        >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3781
        Decimal("1.38435736E+12")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3782
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3783
        return a.fma(b, c, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3784
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3785
    def is_canonical(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3786
        """Return True if the operand is canonical; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3787
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3788
        Currently, the encoding of a Decimal instance is always
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3789
        canonical, so this method returns True for any Decimal.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3790
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3791
        >>> ExtendedContext.is_canonical(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3792
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3793
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3794
        return a.is_canonical()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3795
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3796
    def is_finite(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3797
        """Return True if the operand is finite; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3798
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3799
        A Decimal instance is considered finite if it is neither
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3800
        infinite nor a NaN.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3801
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3802
        >>> ExtendedContext.is_finite(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3803
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3804
        >>> ExtendedContext.is_finite(Decimal('-0.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3805
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3806
        >>> ExtendedContext.is_finite(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3807
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3808
        >>> ExtendedContext.is_finite(Decimal('Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3809
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3810
        >>> ExtendedContext.is_finite(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3811
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3812
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3813
        return a.is_finite()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3814
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3815
    def is_infinite(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3816
        """Return True if the operand is infinite; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3817
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3818
        >>> ExtendedContext.is_infinite(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3819
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3820
        >>> ExtendedContext.is_infinite(Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3821
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3822
        >>> ExtendedContext.is_infinite(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3823
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3824
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3825
        return a.is_infinite()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3826
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3827
    def is_nan(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3828
        """Return True if the operand is a qNaN or sNaN;
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3829
        otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3830
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3831
        >>> ExtendedContext.is_nan(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3832
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3833
        >>> ExtendedContext.is_nan(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3834
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3835
        >>> ExtendedContext.is_nan(Decimal('-sNaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3836
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3837
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3838
        return a.is_nan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3839
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3840
    def is_normal(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3841
        """Return True if the operand is a normal number;
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3842
        otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3843
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3844
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3845
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3846
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3847
        >>> c.is_normal(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3848
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3849
        >>> c.is_normal(Decimal('0.1E-999'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3850
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3851
        >>> c.is_normal(Decimal('0.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3852
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3853
        >>> c.is_normal(Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3854
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3855
        >>> c.is_normal(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3856
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3857
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3858
        return a.is_normal(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3859
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3860
    def is_qnan(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3861
        """Return True if the operand is a quiet NaN; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3862
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3863
        >>> ExtendedContext.is_qnan(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3864
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3865
        >>> ExtendedContext.is_qnan(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3866
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3867
        >>> ExtendedContext.is_qnan(Decimal('sNaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3868
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3869
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3870
        return a.is_qnan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3871
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3872
    def is_signed(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3873
        """Return True if the operand is negative; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3874
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3875
        >>> ExtendedContext.is_signed(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3876
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3877
        >>> ExtendedContext.is_signed(Decimal('-12'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3878
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3879
        >>> ExtendedContext.is_signed(Decimal('-0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3880
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3881
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3882
        return a.is_signed()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3883
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3884
    def is_snan(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3885
        """Return True if the operand is a signaling NaN;
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3886
        otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3887
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3888
        >>> ExtendedContext.is_snan(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3889
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3890
        >>> ExtendedContext.is_snan(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3891
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3892
        >>> ExtendedContext.is_snan(Decimal('sNaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3893
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3894
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3895
        return a.is_snan()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3896
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3897
    def is_subnormal(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3898
        """Return True if the operand is subnormal; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3899
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3900
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3901
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3902
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3903
        >>> c.is_subnormal(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3904
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3905
        >>> c.is_subnormal(Decimal('0.1E-999'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3906
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3907
        >>> c.is_subnormal(Decimal('0.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3908
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3909
        >>> c.is_subnormal(Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3910
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3911
        >>> c.is_subnormal(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3912
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3913
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3914
        return a.is_subnormal(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3915
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3916
    def is_zero(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3917
        """Return True if the operand is a zero; otherwise return False.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3918
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3919
        >>> ExtendedContext.is_zero(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3920
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3921
        >>> ExtendedContext.is_zero(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3922
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3923
        >>> ExtendedContext.is_zero(Decimal('-0E+2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3924
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3925
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3926
        return a.is_zero()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3927
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3928
    def ln(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3929
        """Returns the natural (base e) logarithm of the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3930
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3931
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3932
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3933
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3934
        >>> c.ln(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3935
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3936
        >>> c.ln(Decimal('1.000'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3937
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3938
        >>> c.ln(Decimal('2.71828183'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3939
        Decimal("1.00000000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3940
        >>> c.ln(Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3941
        Decimal("2.30258509")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3942
        >>> c.ln(Decimal('+Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3943
        Decimal("Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3944
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3945
        return a.ln(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3946
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3947
    def log10(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3948
        """Returns the base 10 logarithm of the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3949
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3950
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3951
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3952
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3953
        >>> c.log10(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3954
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3955
        >>> c.log10(Decimal('0.001'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3956
        Decimal("-3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3957
        >>> c.log10(Decimal('1.000'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3958
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3959
        >>> c.log10(Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3960
        Decimal("0.301029996")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3961
        >>> c.log10(Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3962
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3963
        >>> c.log10(Decimal('70'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3964
        Decimal("1.84509804")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3965
        >>> c.log10(Decimal('+Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3966
        Decimal("Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3967
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3968
        return a.log10(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3969
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3970
    def logb(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3971
        """ Returns the exponent of the magnitude of the operand's MSD.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3972
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3973
        The result is the integer which is the exponent of the magnitude
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3974
        of the most significant digit of the operand (as though the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3975
        operand were truncated to a single digit while maintaining the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3976
        value of that digit and without limiting the resulting exponent).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3977
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3978
        >>> ExtendedContext.logb(Decimal('250'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3979
        Decimal("2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3980
        >>> ExtendedContext.logb(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3981
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3982
        >>> ExtendedContext.logb(Decimal('0.03'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3983
        Decimal("-2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3984
        >>> ExtendedContext.logb(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3985
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3986
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3987
        return a.logb(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3988
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3989
    def logical_and(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3990
        """Applies the logical operation 'and' between each operand's digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3991
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3992
        The operands must be both logical numbers.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3993
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3994
        >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3995
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3996
        >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3997
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3998
        >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  3999
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4000
        >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4001
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4002
        >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4003
        Decimal("1000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4004
        >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4005
        Decimal("10")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4006
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4007
        return a.logical_and(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4008
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4009
    def logical_invert(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4010
        """Invert all the digits in the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4011
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4012
        The operand must be a logical number.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4013
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4014
        >>> ExtendedContext.logical_invert(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4015
        Decimal("111111111")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4016
        >>> ExtendedContext.logical_invert(Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4017
        Decimal("111111110")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4018
        >>> ExtendedContext.logical_invert(Decimal('111111111'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4019
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4020
        >>> ExtendedContext.logical_invert(Decimal('101010101'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4021
        Decimal("10101010")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4022
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4023
        return a.logical_invert(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4024
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4025
    def logical_or(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4026
        """Applies the logical operation 'or' between each operand's digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4027
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4028
        The operands must be both logical numbers.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4029
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4030
        >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4031
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4032
        >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4033
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4034
        >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4035
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4036
        >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4037
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4038
        >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4039
        Decimal("1110")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4040
        >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4041
        Decimal("1110")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4042
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4043
        return a.logical_or(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4044
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4045
    def logical_xor(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4046
        """Applies the logical operation 'xor' between each operand's digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4047
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4048
        The operands must be both logical numbers.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4049
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4050
        >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4051
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4052
        >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4053
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4054
        >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4055
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4056
        >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4057
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4058
        >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4059
        Decimal("110")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4060
        >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4061
        Decimal("1101")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4062
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4063
        return a.logical_xor(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4064
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4065
    def max(self, a,b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4066
        """max compares two values numerically and returns the maximum.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4067
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4068
        If either operand is a NaN then the general rules apply.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4069
        Otherwise, the operands are compared as as though by the compare
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4070
        operation.  If they are numerically equal then the left-hand operand
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4071
        is chosen as the result.  Otherwise the maximum (closer to positive
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4072
        infinity) of the two operands is chosen as the result.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4073
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4074
        >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4075
        Decimal("3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4076
        >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4077
        Decimal("3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4078
        >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4079
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4080
        >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4081
        Decimal("7")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4082
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4083
        return a.max(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4084
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4085
    def max_mag(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4086
        """Compares the values numerically with their sign ignored."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4087
        return a.max_mag(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4088
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4089
    def min(self, a,b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4090
        """min compares two values numerically and returns the minimum.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4091
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4092
        If either operand is a NaN then the general rules apply.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4093
        Otherwise, the operands are compared as as though by the compare
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4094
        operation.  If they are numerically equal then the left-hand operand
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4095
        is chosen as the result.  Otherwise the minimum (closer to negative
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4096
        infinity) of the two operands is chosen as the result.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4097
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4098
        >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4099
        Decimal("2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4100
        >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4101
        Decimal("-10")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4102
        >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4103
        Decimal("1.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4104
        >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4105
        Decimal("7")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4106
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4107
        return a.min(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4108
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4109
    def min_mag(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4110
        """Compares the values numerically with their sign ignored."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4111
        return a.min_mag(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4112
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4113
    def minus(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4114
        """Minus corresponds to unary prefix minus in Python.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4115
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4116
        The operation is evaluated using the same rules as subtract; the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4117
        operation minus(a) is calculated as subtract('0', a) where the '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4118
        has the same exponent as the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4119
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4120
        >>> ExtendedContext.minus(Decimal('1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4121
        Decimal("-1.3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4122
        >>> ExtendedContext.minus(Decimal('-1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4123
        Decimal("1.3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4124
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4125
        return a.__neg__(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4126
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4127
    def multiply(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4128
        """multiply multiplies two operands.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4129
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4130
        If either operand is a special value then the general rules apply.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4131
        Otherwise, the operands are multiplied together ('long multiplication'),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4132
        resulting in a number which may be as long as the sum of the lengths
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4133
        of the two operands.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4134
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4135
        >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4136
        Decimal("3.60")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4137
        >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4138
        Decimal("21")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4139
        >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4140
        Decimal("0.72")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4141
        >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4142
        Decimal("-0.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4143
        >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4144
        Decimal("4.28135971E+11")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4145
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4146
        return a.__mul__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4147
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4148
    def next_minus(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4149
        """Returns the largest representable number smaller than a.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4150
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4151
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4152
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4153
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4154
        >>> ExtendedContext.next_minus(Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4155
        Decimal("0.999999999")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4156
        >>> c.next_minus(Decimal('1E-1007'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4157
        Decimal("0E-1007")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4158
        >>> ExtendedContext.next_minus(Decimal('-1.00000003'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4159
        Decimal("-1.00000004")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4160
        >>> c.next_minus(Decimal('Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4161
        Decimal("9.99999999E+999")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4162
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4163
        return a.next_minus(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4164
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4165
    def next_plus(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4166
        """Returns the smallest representable number larger than a.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4167
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4168
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4169
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4170
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4171
        >>> ExtendedContext.next_plus(Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4172
        Decimal("1.00000001")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4173
        >>> c.next_plus(Decimal('-1E-1007'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4174
        Decimal("-0E-1007")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4175
        >>> ExtendedContext.next_plus(Decimal('-1.00000003'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4176
        Decimal("-1.00000002")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4177
        >>> c.next_plus(Decimal('-Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4178
        Decimal("-9.99999999E+999")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4179
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4180
        return a.next_plus(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4181
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4182
    def next_toward(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4183
        """Returns the number closest to a, in direction towards b.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4184
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4185
        The result is the closest representable number from the first
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4186
        operand (but not the first operand) that is in the direction
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4187
        towards the second operand, unless the operands have the same
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4188
        value.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4189
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4190
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4191
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4192
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4193
        >>> c.next_toward(Decimal('1'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4194
        Decimal("1.00000001")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4195
        >>> c.next_toward(Decimal('-1E-1007'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4196
        Decimal("-0E-1007")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4197
        >>> c.next_toward(Decimal('-1.00000003'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4198
        Decimal("-1.00000002")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4199
        >>> c.next_toward(Decimal('1'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4200
        Decimal("0.999999999")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4201
        >>> c.next_toward(Decimal('1E-1007'), Decimal('-100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4202
        Decimal("0E-1007")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4203
        >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4204
        Decimal("-1.00000004")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4205
        >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4206
        Decimal("-0.00")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4207
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4208
        return a.next_toward(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4209
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4210
    def normalize(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4211
        """normalize reduces an operand to its simplest form.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4212
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4213
        Essentially a plus operation with all trailing zeros removed from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4214
        result.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4215
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4216
        >>> ExtendedContext.normalize(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4217
        Decimal("2.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4218
        >>> ExtendedContext.normalize(Decimal('-2.0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4219
        Decimal("-2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4220
        >>> ExtendedContext.normalize(Decimal('1.200'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4221
        Decimal("1.2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4222
        >>> ExtendedContext.normalize(Decimal('-120'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4223
        Decimal("-1.2E+2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4224
        >>> ExtendedContext.normalize(Decimal('120.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4225
        Decimal("1.2E+2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4226
        >>> ExtendedContext.normalize(Decimal('0.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4227
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4228
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4229
        return a.normalize(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4230
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4231
    def number_class(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4232
        """Returns an indication of the class of the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4233
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4234
        The class is one of the following strings:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4235
          -sNaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4236
          -NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4237
          -Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4238
          -Normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4239
          -Subnormal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4240
          -Zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4241
          +Zero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4242
          +Subnormal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4243
          +Normal
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4244
          +Infinity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4245
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4246
        >>> c = Context(ExtendedContext)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4247
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4248
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4249
        >>> c.number_class(Decimal('Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4250
        '+Infinity'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4251
        >>> c.number_class(Decimal('1E-10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4252
        '+Normal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4253
        >>> c.number_class(Decimal('2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4254
        '+Normal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4255
        >>> c.number_class(Decimal('0.1E-999'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4256
        '+Subnormal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4257
        >>> c.number_class(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4258
        '+Zero'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4259
        >>> c.number_class(Decimal('-0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4260
        '-Zero'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4261
        >>> c.number_class(Decimal('-0.1E-999'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4262
        '-Subnormal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4263
        >>> c.number_class(Decimal('-1E-10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4264
        '-Normal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4265
        >>> c.number_class(Decimal('-2.50'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4266
        '-Normal'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4267
        >>> c.number_class(Decimal('-Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4268
        '-Infinity'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4269
        >>> c.number_class(Decimal('NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4270
        'NaN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4271
        >>> c.number_class(Decimal('-NaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4272
        'NaN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4273
        >>> c.number_class(Decimal('sNaN'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4274
        'sNaN'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4275
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4276
        return a.number_class(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4277
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4278
    def plus(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4279
        """Plus corresponds to unary prefix plus in Python.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4280
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4281
        The operation is evaluated using the same rules as add; the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4282
        operation plus(a) is calculated as add('0', a) where the '0'
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4283
        has the same exponent as the operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4284
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4285
        >>> ExtendedContext.plus(Decimal('1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4286
        Decimal("1.3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4287
        >>> ExtendedContext.plus(Decimal('-1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4288
        Decimal("-1.3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4289
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4290
        return a.__pos__(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4291
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4292
    def power(self, a, b, modulo=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4293
        """Raises a to the power of b, to modulo if given.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4294
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4295
        With two arguments, compute a**b.  If a is negative then b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4296
        must be integral.  The result will be inexact unless b is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4297
        integral and the result is finite and can be expressed exactly
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4298
        in 'precision' digits.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4299
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4300
        With three arguments, compute (a**b) % modulo.  For the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4301
        three argument form, the following restrictions on the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4302
        arguments hold:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4303
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4304
         - all three arguments must be integral
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4305
         - b must be nonnegative
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4306
         - at least one of a or b must be nonzero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4307
         - modulo must be nonzero and have at most 'precision' digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4308
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4309
        The result of pow(a, b, modulo) is identical to the result
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4310
        that would be obtained by computing (a**b) % modulo with
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4311
        unbounded precision, but is computed more efficiently.  It is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4312
        always exact.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4313
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4314
        >>> c = ExtendedContext.copy()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4315
        >>> c.Emin = -999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4316
        >>> c.Emax = 999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4317
        >>> c.power(Decimal('2'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4318
        Decimal("8")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4319
        >>> c.power(Decimal('-2'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4320
        Decimal("-8")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4321
        >>> c.power(Decimal('2'), Decimal('-3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4322
        Decimal("0.125")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4323
        >>> c.power(Decimal('1.7'), Decimal('8'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4324
        Decimal("69.7575744")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4325
        >>> c.power(Decimal('10'), Decimal('0.301029996'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4326
        Decimal("2.00000000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4327
        >>> c.power(Decimal('Infinity'), Decimal('-1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4328
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4329
        >>> c.power(Decimal('Infinity'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4330
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4331
        >>> c.power(Decimal('Infinity'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4332
        Decimal("Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4333
        >>> c.power(Decimal('-Infinity'), Decimal('-1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4334
        Decimal("-0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4335
        >>> c.power(Decimal('-Infinity'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4336
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4337
        >>> c.power(Decimal('-Infinity'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4338
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4339
        >>> c.power(Decimal('-Infinity'), Decimal('2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4340
        Decimal("Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4341
        >>> c.power(Decimal('0'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4342
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4343
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4344
        >>> c.power(Decimal('3'), Decimal('7'), Decimal('16'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4345
        Decimal("11")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4346
        >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4347
        Decimal("-11")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4348
        >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4349
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4350
        >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4351
        Decimal("11")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4352
        >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4353
        Decimal("11729830")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4354
        >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4355
        Decimal("-0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4356
        >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4357
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4358
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4359
        return a.__pow__(b, modulo, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4360
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4361
    def quantize(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4362
        """Returns a value equal to 'a' (rounded), having the exponent of 'b'.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4363
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4364
        The coefficient of the result is derived from that of the left-hand
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4365
        operand.  It may be rounded using the current rounding setting (if the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4366
        exponent is being increased), multiplied by a positive power of ten (if
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4367
        the exponent is being decreased), or is unchanged (if the exponent is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4368
        already equal to that of the right-hand operand).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4369
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4370
        Unlike other operations, if the length of the coefficient after the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4371
        quantize operation would be greater than precision then an Invalid
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4372
        operation condition is raised.  This guarantees that, unless there is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4373
        an error condition, the exponent of the result of a quantize is always
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4374
        equal to that of the right-hand operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4375
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4376
        Also unlike other operations, quantize will never raise Underflow, even
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4377
        if the result is subnormal and inexact.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4378
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4379
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4380
        Decimal("2.170")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4381
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4382
        Decimal("2.17")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4383
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4384
        Decimal("2.2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4385
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4386
        Decimal("2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4387
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4388
        Decimal("0E+1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4389
        >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4390
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4391
        >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4392
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4393
        >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4394
        Decimal("-0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4395
        >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4396
        Decimal("-0E+5")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4397
        >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4398
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4399
        >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4400
        Decimal("NaN")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4401
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4402
        Decimal("217.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4403
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4404
        Decimal("217")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4405
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4406
        Decimal("2.2E+2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4407
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4408
        Decimal("2E+2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4409
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4410
        return a.quantize(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4411
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4412
    def radix(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4413
        """Just returns 10, as this is Decimal, :)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4414
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4415
        >>> ExtendedContext.radix()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4416
        Decimal("10")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4417
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4418
        return Decimal(10)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4419
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4420
    def remainder(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4421
        """Returns the remainder from integer division.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4422
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4423
        The result is the residue of the dividend after the operation of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4424
        calculating integer division as described for divide-integer, rounded
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4425
        to precision digits if necessary.  The sign of the result, if
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4426
        non-zero, is the same as that of the original dividend.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4427
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4428
        This operation will fail under the same conditions as integer division
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4429
        (that is, if integer division on the same two operands would fail, the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4430
        remainder cannot be calculated).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4431
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4432
        >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4433
        Decimal("2.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4434
        >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4435
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4436
        >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4437
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4438
        >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4439
        Decimal("0.2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4440
        >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4441
        Decimal("0.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4442
        >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4443
        Decimal("1.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4444
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4445
        return a.__mod__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4446
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4447
    def remainder_near(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4448
        """Returns to be "a - b * n", where n is the integer nearest the exact
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4449
        value of "x / b" (if two integers are equally near then the even one
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4450
        is chosen).  If the result is equal to 0 then its sign will be the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4451
        sign of a.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4452
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4453
        This operation will fail under the same conditions as integer division
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4454
        (that is, if integer division on the same two operands would fail, the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4455
        remainder cannot be calculated).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4456
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4457
        >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4458
        Decimal("-0.9")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4459
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4460
        Decimal("-2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4461
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4462
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4463
        >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4464
        Decimal("-1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4465
        >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4466
        Decimal("0.2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4467
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4468
        Decimal("0.1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4469
        >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4470
        Decimal("-0.3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4471
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4472
        return a.remainder_near(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4473
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4474
    def rotate(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4475
        """Returns a rotated copy of a, b times.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4476
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4477
        The coefficient of the result is a rotated copy of the digits in
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4478
        the coefficient of the first operand.  The number of places of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4479
        rotation is taken from the absolute value of the second operand,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4480
        with the rotation being to the left if the second operand is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4481
        positive or to the right otherwise.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4482
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4483
        >>> ExtendedContext.rotate(Decimal('34'), Decimal('8'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4484
        Decimal("400000003")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4485
        >>> ExtendedContext.rotate(Decimal('12'), Decimal('9'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4486
        Decimal("12")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4487
        >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4488
        Decimal("891234567")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4489
        >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4490
        Decimal("123456789")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4491
        >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4492
        Decimal("345678912")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4493
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4494
        return a.rotate(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4495
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4496
    def same_quantum(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4497
        """Returns True if the two operands have the same exponent.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4498
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4499
        The result is never affected by either the sign or the coefficient of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4500
        either operand.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4501
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4502
        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4503
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4504
        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4505
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4506
        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4507
        False
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4508
        >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4509
        True
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4510
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4511
        return a.same_quantum(b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4512
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4513
    def scaleb (self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4514
        """Returns the first operand after adding the second value its exp.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4515
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4516
        >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4517
        Decimal("0.0750")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4518
        >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4519
        Decimal("7.50")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4520
        >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4521
        Decimal("7.50E+3")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4522
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4523
        return a.scaleb (b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4524
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4525
    def shift(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4526
        """Returns a shifted copy of a, b times.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4527
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4528
        The coefficient of the result is a shifted copy of the digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4529
        in the coefficient of the first operand.  The number of places
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4530
        to shift is taken from the absolute value of the second operand,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4531
        with the shift being to the left if the second operand is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4532
        positive or to the right otherwise.  Digits shifted into the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4533
        coefficient are zeros.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4534
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4535
        >>> ExtendedContext.shift(Decimal('34'), Decimal('8'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4536
        Decimal("400000000")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4537
        >>> ExtendedContext.shift(Decimal('12'), Decimal('9'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4538
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4539
        >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4540
        Decimal("1234567")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4541
        >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4542
        Decimal("123456789")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4543
        >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4544
        Decimal("345678900")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4545
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4546
        return a.shift(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4547
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4548
    def sqrt(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4549
        """Square root of a non-negative number to context precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4550
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4551
        If the result must be inexact, it is rounded using the round-half-even
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4552
        algorithm.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4553
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4554
        >>> ExtendedContext.sqrt(Decimal('0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4555
        Decimal("0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4556
        >>> ExtendedContext.sqrt(Decimal('-0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4557
        Decimal("-0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4558
        >>> ExtendedContext.sqrt(Decimal('0.39'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4559
        Decimal("0.624499800")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4560
        >>> ExtendedContext.sqrt(Decimal('100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4561
        Decimal("10")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4562
        >>> ExtendedContext.sqrt(Decimal('1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4563
        Decimal("1")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4564
        >>> ExtendedContext.sqrt(Decimal('1.0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4565
        Decimal("1.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4566
        >>> ExtendedContext.sqrt(Decimal('1.00'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4567
        Decimal("1.0")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4568
        >>> ExtendedContext.sqrt(Decimal('7'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4569
        Decimal("2.64575131")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4570
        >>> ExtendedContext.sqrt(Decimal('10'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4571
        Decimal("3.16227766")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4572
        >>> ExtendedContext.prec
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4573
        9
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4574
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4575
        return a.sqrt(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4576
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4577
    def subtract(self, a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4578
        """Return the difference between the two operands.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4579
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4580
        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4581
        Decimal("0.23")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4582
        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4583
        Decimal("0.00")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4584
        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4585
        Decimal("-0.77")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4586
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4587
        return a.__sub__(b, context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4588
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4589
    def to_eng_string(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4590
        """Converts a number to a string, using scientific notation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4591
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4592
        The operation is not affected by the context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4593
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4594
        return a.to_eng_string(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4595
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4596
    def to_sci_string(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4597
        """Converts a number to a string, using scientific notation.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4598
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4599
        The operation is not affected by the context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4600
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4601
        return a.__str__(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4602
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4603
    def to_integral_exact(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4604
        """Rounds to an integer.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4605
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4606
        When the operand has a negative exponent, the result is the same
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4607
        as using the quantize() operation using the given operand as the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4608
        left-hand-operand, 1E+0 as the right-hand-operand, and the precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4609
        of the operand as the precision setting; Inexact and Rounded flags
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4610
        are allowed in this operation.  The rounding mode is taken from the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4611
        context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4612
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4613
        >>> ExtendedContext.to_integral_exact(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4614
        Decimal("2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4615
        >>> ExtendedContext.to_integral_exact(Decimal('100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4616
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4617
        >>> ExtendedContext.to_integral_exact(Decimal('100.0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4618
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4619
        >>> ExtendedContext.to_integral_exact(Decimal('101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4620
        Decimal("102")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4621
        >>> ExtendedContext.to_integral_exact(Decimal('-101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4622
        Decimal("-102")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4623
        >>> ExtendedContext.to_integral_exact(Decimal('10E+5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4624
        Decimal("1.0E+6")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4625
        >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4626
        Decimal("7.89E+77")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4627
        >>> ExtendedContext.to_integral_exact(Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4628
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4629
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4630
        return a.to_integral_exact(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4631
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4632
    def to_integral_value(self, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4633
        """Rounds to an integer.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4634
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4635
        When the operand has a negative exponent, the result is the same
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4636
        as using the quantize() operation using the given operand as the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4637
        left-hand-operand, 1E+0 as the right-hand-operand, and the precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4638
        of the operand as the precision setting, except that no flags will
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4639
        be set.  The rounding mode is taken from the context.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4640
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4641
        >>> ExtendedContext.to_integral_value(Decimal('2.1'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4642
        Decimal("2")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4643
        >>> ExtendedContext.to_integral_value(Decimal('100'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4644
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4645
        >>> ExtendedContext.to_integral_value(Decimal('100.0'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4646
        Decimal("100")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4647
        >>> ExtendedContext.to_integral_value(Decimal('101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4648
        Decimal("102")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4649
        >>> ExtendedContext.to_integral_value(Decimal('-101.5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4650
        Decimal("-102")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4651
        >>> ExtendedContext.to_integral_value(Decimal('10E+5'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4652
        Decimal("1.0E+6")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4653
        >>> ExtendedContext.to_integral_value(Decimal('7.89E+77'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4654
        Decimal("7.89E+77")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4655
        >>> ExtendedContext.to_integral_value(Decimal('-Inf'))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4656
        Decimal("-Infinity")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4657
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4658
        return a.to_integral_value(context=self)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4659
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4660
    # the method name changed, but we provide also the old one, for compatibility
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4661
    to_integral = to_integral_value
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4662
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4663
class _WorkRep(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4664
    __slots__ = ('sign','int','exp')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4665
    # sign: 0 or 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4666
    # int:  int or long
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4667
    # exp:  None, int, or string
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4668
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4669
    def __init__(self, value=None):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4670
        if value is None:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4671
            self.sign = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4672
            self.int = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4673
            self.exp = None
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4674
        elif isinstance(value, Decimal):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4675
            self.sign = value._sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4676
            self.int = int(value._int)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4677
            self.exp = value._exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4678
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4679
            # assert isinstance(value, tuple)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4680
            self.sign = value[0]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4681
            self.int = value[1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4682
            self.exp = value[2]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4683
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4684
    def __repr__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4685
        return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4686
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4687
    __str__ = __repr__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4688
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4689
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4690
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4691
def _normalize(op1, op2, prec = 0):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4692
    """Normalizes op1, op2 to have the same exp and length of coefficient.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4693
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4694
    Done during addition.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4695
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4696
    if op1.exp < op2.exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4697
        tmp = op2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4698
        other = op1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4699
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4700
        tmp = op1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4701
        other = op2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4702
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4703
    # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4704
    # Then adding 10**exp to tmp has the same effect (after rounding)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4705
    # as adding any positive quantity smaller than 10**exp; similarly
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4706
    # for subtraction.  So if other is smaller than 10**exp we replace
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4707
    # it with 10**exp.  This avoids tmp.exp - other.exp getting too large.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4708
    tmp_len = len(str(tmp.int))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4709
    other_len = len(str(other.int))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4710
    exp = tmp.exp + min(-1, tmp_len - prec - 2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4711
    if other_len + other.exp - 1 < exp:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4712
        other.int = 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4713
        other.exp = exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4714
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4715
    tmp.int *= 10 ** (tmp.exp - other.exp)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4716
    tmp.exp = other.exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4717
    return op1, op2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4718
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4719
##### Integer arithmetic functions used by ln, log10, exp and __pow__ #####
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4720
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4721
# This function from Tim Peters was taken from here:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4722
# http://mail.python.org/pipermail/python-list/1999-July/007758.html
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4723
# The correction being in the function definition is for speed, and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4724
# the whole function is not resolved with math.log because of avoiding
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4725
# the use of floats.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4726
def _nbits(n, correction = {
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4727
        '0': 4, '1': 3, '2': 2, '3': 2,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4728
        '4': 1, '5': 1, '6': 1, '7': 1,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4729
        '8': 0, '9': 0, 'a': 0, 'b': 0,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4730
        'c': 0, 'd': 0, 'e': 0, 'f': 0}):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4731
    """Number of bits in binary representation of the positive integer n,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4732
    or 0 if n == 0.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4733
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4734
    if n < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4735
        raise ValueError("The argument to _nbits should be nonnegative.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4736
    hex_n = "%x" % n
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4737
    return 4*len(hex_n) - correction[hex_n[0]]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4738
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4739
def _sqrt_nearest(n, a):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4740
    """Closest integer to the square root of the positive integer n.  a is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4741
    an initial approximation to the square root.  Any positive integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4742
    will do for a, but the closer a is to the square root of n the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4743
    faster convergence will be.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4744
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4745
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4746
    if n <= 0 or a <= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4747
        raise ValueError("Both arguments to _sqrt_nearest should be positive.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4748
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4749
    b=0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4750
    while a != b:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4751
        b, a = a, a--n//a>>1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4752
    return a
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4753
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4754
def _rshift_nearest(x, shift):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4755
    """Given an integer x and a nonnegative integer shift, return closest
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4756
    integer to x / 2**shift; use round-to-even in case of a tie.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4757
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4758
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4759
    b, q = 1L << shift, x >> shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4760
    return q + (2*(x & (b-1)) + (q&1) > b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4761
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4762
def _div_nearest(a, b):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4763
    """Closest integer to a/b, a and b positive integers; rounds to even
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4764
    in the case of a tie.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4765
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4766
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4767
    q, r = divmod(a, b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4768
    return q + (2*r + (q&1) > b)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4769
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4770
def _ilog(x, M, L = 8):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4771
    """Integer approximation to M*log(x/M), with absolute error boundable
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4772
    in terms only of x/M.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4773
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4774
    Given positive integers x and M, return an integer approximation to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4775
    M * log(x/M).  For L = 8 and 0.1 <= x/M <= 10 the difference
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4776
    between the approximation and the exact result is at most 22.  For
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4777
    L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15.  In
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4778
    both cases these are upper bounds on the error; it will usually be
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4779
    much smaller."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4780
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4781
    # The basic algorithm is the following: let log1p be the function
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4782
    # log1p(x) = log(1+x).  Then log(x/M) = log1p((x-M)/M).  We use
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4783
    # the reduction
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4784
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4785
    #    log1p(y) = 2*log1p(y/(1+sqrt(1+y)))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4786
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4787
    # repeatedly until the argument to log1p is small (< 2**-L in
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4788
    # absolute value).  For small y we can use the Taylor series
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4789
    # expansion
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4790
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4791
    #    log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4792
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4793
    # truncating at T such that y**T is small enough.  The whole
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4794
    # computation is carried out in a form of fixed-point arithmetic,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4795
    # with a real number z being represented by an integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4796
    # approximation to z*M.  To avoid loss of precision, the y below
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4797
    # is actually an integer approximation to 2**R*y*M, where R is the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4798
    # number of reductions performed so far.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4799
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4800
    y = x-M
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4801
    # argument reduction; R = number of reductions performed
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4802
    R = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4803
    while (R <= L and long(abs(y)) << L-R >= M or
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4804
           R > L and abs(y) >> R-L >= M):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4805
        y = _div_nearest(long(M*y) << 1,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4806
                         M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4807
        R += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4808
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4809
    # Taylor series with T terms
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4810
    T = -int(-10*len(str(M))//(3*L))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4811
    yshift = _rshift_nearest(y, R)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4812
    w = _div_nearest(M, T)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4813
    for k in xrange(T-1, 0, -1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4814
        w = _div_nearest(M, k) - _div_nearest(yshift*w, M)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4815
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4816
    return _div_nearest(w*y, M)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4817
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4818
def _dlog10(c, e, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4819
    """Given integers c, e and p with c > 0, p >= 0, compute an integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4820
    approximation to 10**p * log10(c*10**e), with an absolute error of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4821
    at most 1.  Assumes that c*10**e is not exactly 1."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4822
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4823
    # increase precision by 2; compensate for this by dividing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4824
    # final result by 100
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4825
    p += 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4826
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4827
    # write c*10**e as d*10**f with either:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4828
    #   f >= 0 and 1 <= d <= 10, or
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4829
    #   f <= 0 and 0.1 <= d <= 1.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4830
    # Thus for c*10**e close to 1, f = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4831
    l = len(str(c))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4832
    f = e+l - (e+l >= 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4833
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4834
    if p > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4835
        M = 10**p
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4836
        k = e+p-f
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4837
        if k >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4838
            c *= 10**k
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4839
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4840
            c = _div_nearest(c, 10**-k)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4841
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4842
        log_d = _ilog(c, M) # error < 5 + 22 = 27
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4843
        log_10 = _log10_digits(p) # error < 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4844
        log_d = _div_nearest(log_d*M, log_10)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4845
        log_tenpower = f*M # exact
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4846
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4847
        log_d = 0  # error < 2.31
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4848
        log_tenpower = div_nearest(f, 10**-p) # error < 0.5
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4849
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4850
    return _div_nearest(log_tenpower+log_d, 100)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4851
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4852
def _dlog(c, e, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4853
    """Given integers c, e and p with c > 0, compute an integer
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4854
    approximation to 10**p * log(c*10**e), with an absolute error of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4855
    at most 1.  Assumes that c*10**e is not exactly 1."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4856
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4857
    # Increase precision by 2. The precision increase is compensated
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4858
    # for at the end with a division by 100.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4859
    p += 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4860
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4861
    # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4862
    # or f <= 0 and 0.1 <= d <= 1.  Then we can compute 10**p * log(c*10**e)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4863
    # as 10**p * log(d) + 10**p*f * log(10).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4864
    l = len(str(c))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4865
    f = e+l - (e+l >= 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4866
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4867
    # compute approximation to 10**p*log(d), with error < 27
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4868
    if p > 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4869
        k = e+p-f
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4870
        if k >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4871
            c *= 10**k
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4872
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4873
            c = _div_nearest(c, 10**-k)  # error of <= 0.5 in c
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4874
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4875
        # _ilog magnifies existing error in c by a factor of at most 10
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4876
        log_d = _ilog(c, 10**p) # error < 5 + 22 = 27
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4877
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4878
        # p <= 0: just approximate the whole thing by 0; error < 2.31
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4879
        log_d = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4880
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4881
    # compute approximation to f*10**p*log(10), with error < 11.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4882
    if f:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4883
        extra = len(str(abs(f)))-1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4884
        if p + extra >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4885
            # error in f * _log10_digits(p+extra) < |f| * 1 = |f|
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4886
            # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4887
            f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4888
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4889
            f_log_ten = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4890
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4891
        f_log_ten = 0
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4892
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4893
    # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4894
    return _div_nearest(f_log_ten + log_d, 100)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4895
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4896
class _Log10Memoize(object):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4897
    """Class to compute, store, and allow retrieval of, digits of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4898
    constant log(10) = 2.302585....  This constant is needed by
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4899
    Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4900
    def __init__(self):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4901
        self.digits = "23025850929940456840179914546843642076011014886"
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4902
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4903
    def getdigits(self, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4904
        """Given an integer p >= 0, return floor(10**p)*log(10).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4905
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4906
        For example, self.getdigits(3) returns 2302.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4907
        """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4908
        # digits are stored as a string, for quick conversion to
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4909
        # integer in the case that we've already computed enough
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4910
        # digits; the stored digits should always be correct
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4911
        # (truncated, not rounded to nearest).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4912
        if p < 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4913
            raise ValueError("p should be nonnegative")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4914
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4915
        if p >= len(self.digits):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4916
            # compute p+3, p+6, p+9, ... digits; continue until at
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4917
            # least one of the extra digits is nonzero
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4918
            extra = 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4919
            while True:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4920
                # compute p+extra digits, correct to within 1ulp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4921
                M = 10**(p+extra+2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4922
                digits = str(_div_nearest(_ilog(10*M, M), 100))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4923
                if digits[-extra:] != '0'*extra:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4924
                    break
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4925
                extra += 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4926
            # keep all reliable digits so far; remove trailing zeros
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4927
            # and next nonzero digit
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4928
            self.digits = digits.rstrip('0')[:-1]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4929
        return int(self.digits[:p+1])
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4930
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4931
_log10_digits = _Log10Memoize().getdigits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4932
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4933
def _iexp(x, M, L=8):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4934
    """Given integers x and M, M > 0, such that x/M is small in absolute
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4935
    value, compute an integer approximation to M*exp(x/M).  For 0 <=
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4936
    x/M <= 2.4, the absolute error in the result is bounded by 60 (and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4937
    is usually much smaller)."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4938
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4939
    # Algorithm: to compute exp(z) for a real number z, first divide z
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4940
    # by a suitable power R of 2 so that |z/2**R| < 2**-L.  Then
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4941
    # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4942
    # series
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4943
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4944
    #     expm1(x) = x + x**2/2! + x**3/3! + ...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4945
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4946
    # Now use the identity
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4947
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4948
    #     expm1(2x) = expm1(x)*(expm1(x)+2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4949
    #
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4950
    # R times to compute the sequence expm1(z/2**R),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4951
    # expm1(z/2**(R-1)), ... , exp(z/2), exp(z).
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4952
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4953
    # Find R such that x/2**R/M <= 2**-L
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4954
    R = _nbits((long(x)<<L)//M)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4955
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4956
    # Taylor series.  (2**L)**T > M
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4957
    T = -int(-10*len(str(M))//(3*L))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4958
    y = _div_nearest(x, T)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4959
    Mshift = long(M)<<R
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4960
    for i in xrange(T-1, 0, -1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4961
        y = _div_nearest(x*(Mshift + y), Mshift * i)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4962
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4963
    # Expansion
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4964
    for k in xrange(R-1, -1, -1):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4965
        Mshift = long(M)<<(k+2)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4966
        y = _div_nearest(y*(y+Mshift), Mshift)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4967
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4968
    return M+y
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4969
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4970
def _dexp(c, e, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4971
    """Compute an approximation to exp(c*10**e), with p decimal places of
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4972
    precision.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4973
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4974
    Returns integers d, f such that:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4975
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4976
      10**(p-1) <= d <= 10**p, and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4977
      (d-1)*10**f < exp(c*10**e) < (d+1)*10**f
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4978
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4979
    In other words, d*10**f is an approximation to exp(c*10**e) with p
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4980
    digits of precision, and with an error in d of at most 1.  This is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4981
    almost, but not quite, the same as the error being < 1ulp: when d
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4982
    = 10**(p-1) the error could be up to 10 ulp."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4983
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4984
    # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4985
    p += 2
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4986
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4987
    # compute log(10) with extra precision = adjusted exponent of c*10**e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4988
    extra = max(0, e + len(str(c)) - 1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4989
    q = p + extra
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4990
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4991
    # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q),
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4992
    # rounding down
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4993
    shift = e+q
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4994
    if shift >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4995
        cshift = c*10**shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4996
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4997
        cshift = c//10**-shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4998
    quot, rem = divmod(cshift, _log10_digits(q))
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  4999
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5000
    # reduce remainder back to original precision
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5001
    rem = _div_nearest(rem, 10**extra)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5002
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5003
    # error in result of _iexp < 120;  error after division < 0.62
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5004
    return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5005
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5006
def _dpower(xc, xe, yc, ye, p):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5007
    """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5008
    y = yc*10**ye, compute x**y.  Returns a pair of integers (c, e) such that:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5009
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5010
      10**(p-1) <= c <= 10**p, and
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5011
      (c-1)*10**e < x**y < (c+1)*10**e
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5012
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5013
    in other words, c*10**e is an approximation to x**y with p digits
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5014
    of precision, and with an error in c of at most 1.  (This is
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5015
    almost, but not quite, the same as the error being < 1ulp: when c
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5016
    == 10**(p-1) we can only guarantee error < 10ulp.)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5017
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5018
    We assume that: x is positive and not equal to 1, and y is nonzero.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5019
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5020
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5021
    # Find b such that 10**(b-1) <= |y| <= 10**b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5022
    b = len(str(abs(yc))) + ye
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5023
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5024
    # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5025
    lxc = _dlog(xc, xe, p+b+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5026
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5027
    # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5028
    shift = ye-b
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5029
    if shift >= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5030
        pc = lxc*yc*10**shift
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5031
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5032
        pc = _div_nearest(lxc*yc, 10**-shift)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5033
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5034
    if pc == 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5035
        # we prefer a result that isn't exactly 1; this makes it
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5036
        # easier to compute a correctly rounded result in __pow__
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5037
        if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5038
            coeff, exp = 10**(p-1)+1, 1-p
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5039
        else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5040
            coeff, exp = 10**p-1, -p
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5041
    else:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5042
        coeff, exp = _dexp(pc, -(p+1), p+1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5043
        coeff = _div_nearest(coeff, 10)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5044
        exp += 1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5045
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5046
    return coeff, exp
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5047
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5048
def _log10_lb(c, correction = {
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5049
        '1': 100, '2': 70, '3': 53, '4': 40, '5': 31,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5050
        '6': 23, '7': 16, '8': 10, '9': 5}):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5051
    """Compute a lower bound for 100*log10(c) for a positive integer c."""
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5052
    if c <= 0:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5053
        raise ValueError("The argument to _log10_lb should be nonnegative.")
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5054
    str_c = str(c)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5055
    return 100*len(str_c) - correction[str_c[0]]
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5056
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5057
##### Helper Functions ####################################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5058
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5059
def _convert_other(other, raiseit=False):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5060
    """Convert other to Decimal.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5061
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5062
    Verifies that it's ok to use in an implicit construction.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5063
    """
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5064
    if isinstance(other, Decimal):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5065
        return other
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5066
    if isinstance(other, (int, long)):
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5067
        return Decimal(other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5068
    if raiseit:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5069
        raise TypeError("Unable to convert %s to Decimal" % other)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5070
    return NotImplemented
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5071
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5072
##### Setup Specific Contexts ############################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5073
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5074
# The default context prototype used by Context()
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5075
# Is mutable, so that new contexts can have different default values
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5076
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5077
DefaultContext = Context(
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5078
        prec=28, rounding=ROUND_HALF_EVEN,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5079
        traps=[DivisionByZero, Overflow, InvalidOperation],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5080
        flags=[],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5081
        Emax=999999999,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5082
        Emin=-999999999,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5083
        capitals=1
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5084
)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5085
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5086
# Pre-made alternate contexts offered by the specification
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5087
# Don't change these; the user should be able to select these
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5088
# contexts and be able to reproduce results from other implementations
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5089
# of the spec.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5090
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5091
BasicContext = Context(
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5092
        prec=9, rounding=ROUND_HALF_UP,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5093
        traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5094
        flags=[],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5095
)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5096
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5097
ExtendedContext = Context(
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5098
        prec=9, rounding=ROUND_HALF_EVEN,
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5099
        traps=[],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5100
        flags=[],
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5101
)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5102
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5103
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5104
##### crud for parsing strings #############################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5105
import re
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5106
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5107
# Regular expression used for parsing numeric strings.  Additional
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5108
# comments:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5109
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5110
# 1. Uncomment the two '\s*' lines to allow leading and/or trailing
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5111
# whitespace.  But note that the specification disallows whitespace in
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5112
# a numeric string.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5113
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5114
# 2. For finite numbers (not infinities and NaNs) the body of the
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5115
# number between the optional sign and the optional exponent must have
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5116
# at least one decimal digit, possibly after the decimal point.  The
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5117
# lookahead expression '(?=\d|\.\d)' checks this.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5118
#
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5119
# As the flag UNICODE is not enabled here, we're explicitly avoiding any
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5120
# other meaning for \d than the numbers [0-9].
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5121
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5122
import re
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5123
_parser = re.compile(r"""     # A numeric string consists of:
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5124
#    \s*
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5125
    (?P<sign>[-+])?           # an optional sign, followed by either...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5126
    (
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5127
        (?=\d|\.\d)           # ...a number (with at least one digit)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5128
        (?P<int>\d*)          # consisting of a (possibly empty) integer part
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5129
        (\.(?P<frac>\d*))?    # followed by an optional fractional part
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5130
        (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5131
    |
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5132
        Inf(inity)?           # ...an infinity, or...
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5133
    |
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5134
        (?P<signal>s)?        # ...an (optionally signaling)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5135
        NaN                   # NaN
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5136
        (?P<diag>\d*)         # with (possibly empty) diagnostic information.
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5137
    )
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5138
#    \s*
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5139
    $
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5140
""", re.VERBOSE | re.IGNORECASE).match
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5141
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5142
_all_zeros = re.compile('0*$').match
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5143
_exact_half = re.compile('50*$').match
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5144
del re
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5145
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5146
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5147
##### Useful Constants (internal use only) ################################
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5148
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5149
# Reusable defaults
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5150
Inf = Decimal('Inf')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5151
negInf = Decimal('-Inf')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5152
NaN = Decimal('NaN')
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5153
Dec_0 = Decimal(0)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5154
Dec_p1 = Decimal(1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5155
Dec_n1 = Decimal(-1)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5156
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5157
# Infsign[sign] is infinity w/ that sign
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5158
Infsign = (Inf, negInf)
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5159
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5160
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5161
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5162
if __name__ == '__main__':
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5163
    import doctest, sys
ae805ac0140d DP tools release version Revision: 200912
Deepak Modgil <Deepak.Modgil@Nokia.com>
parents:
diff changeset
  5164
    doctest.testmod(sys.modules[__name__])