固定点的Python十进制上下文

时间:2015-03-03 08:46:02

标签: python decimal fixed-point exponent

我想计算前面有3个位置的数字和小数点后面的2个位置(当然,2和3是可配置的)。我认为通过例子来解释是最简单的:

0.01和999.99是正数的下限和上限。当然,还有0.00,负数从-999.99到-0.01。每两个连续数字之间的距离为0.01。

7.80 + 1.20应为9.00,999.00 + 1.00应为OverflowError。 0.20 * 0.40应该是0.08,0.34 * 0.20应该是0.07(这可以设置一个标志来表示它是四舍五入的,但它不能引发任何异常)。 0.34 * 0.01应为0.00(与前一个条件相同)。

事实上,我想要" ints"从0到99999,只是在第三个数字后写一个点,在乘法时缩小100倍,在分割时缩小100倍。应该可以找到完全相同的上下文,对吧?

问题是,我无法找到Emin,Emax,clamp和prec的正确设置,它将做我想要的。例如,我尝试将Emin和Emax设置为0,但这会引发太多的InvalidOperations。我唯一知道的是舍入应该是ROUND_HALF_EVEN。 : - )

1 个答案:

答案 0 :(得分:3)

来自文档:

  

Q值。一旦我有有效的两位输入,我如何在整个应用程序中保持该不变量?

     

一个。加法,减法和乘以整数等操作会自动保留固定点。其他操作,如除法和非整数乘法,将改变小数位数,需要跟进量化()步骤:

>>> TWOPLACES = Decimal(10) ** -2   # same as Decimal('0.01')
>>> a = Decimal('102.72')           # Initial fixed-point values
>>> b = Decimal('3.17')
>>> a + b                           # Addition preserves fixed-point
Decimal('105.89')
>>> a - b
Decimal('99.55')
>>> a * 42                          # So does integer multiplication
Decimal('4314.24')
>>> (a * b).quantize(TWOPLACES)     # Must quantize non-integer multiplication
Decimal('325.62')
>>> (b / a).quantize(TWOPLACES)     # And quantize division
Decimal('0.03')
  

在开发定点应用程序时,定义函数来处理量子化()步骤很方便:

>>> def mul(x, y, fp=TWOPLACES):
...     return (x * y).quantize(fp)
>>> def div(x, y, fp=TWOPLACES):
...     return (x / y).quantize(fp)    
>>> mul(a, b)                       # Automatically preserve fixed-point
Decimal('325.62')
>>> div(b, a)
Decimal('0.03')

似乎解决方案是将精度设置为5,将Emax设置为2并使用这些量化函数。

con = decimal.getcontext()
con.prec = 5
con.Emax = 2
con.Emin = 0

try:
    Decimal(1) * 1000
except decimal.Overflow as e:
    print(e)
else:
    assert False

assert Decimal("0.99") * 1000 == Decimal("990.00")
assert div(Decimal(1), 3) == Decimal("0.33")

创建固定点小数类

似乎很容易将十进制模块修改为固定点(以丢失浮点小数为代价)。这是因为Decimal类在模块decimal中由全局名称引用。我们可以弹出我们的兼容课程,事情会很好。首先,您需要阻止python导入C _decimal模块并使其使用decimal模块的pure-python实现(因此我们可以覆盖Decimal的私有方法)。完成后,您只需要覆盖一个方法 - _fix。它被称为创建的每个新Decimal,它可能不会遵守当前的十进制上下文。

模块设置

# setup python to not import _decimal (c implementation of Decimal) if present
import sys

if "_decimal" in sys.modules or "decimal" in sys.modules:
    raise ImportError("fixedpointdecimal and the original decimal module do not work"
        " together")

import builtins
_original_import = __import__
def _import(name, *args, **kwargs):
    if name == "_decimal":
        raise ImportError
    return _original_import(name, *args, **kwargs)
builtins.__import__ = _import

# import pure-python implementation of decimal
import decimal

# clean up
builtins.__import__ = _original_import # restore original __import__
del sys, builtins, _original_import, _import # clean up namespace

主要十进制类

from decimal import *

class FixedPointDecimal(Decimal):

    def _fix(self, context):
        # always fit to 2dp
        return super()._fix(context)._rescale(-2, context.rounding)
        # use context to find number of decimal places to use
        # return super()._fix(context)._rescale(-context.decimal_places, context.rounding)

# setup decimal module to use FixedPointDecimal
decimal.Decimal = FixedPointDecimal
Decimal = FixedPointDecimal

<强>测试

getcontext().prec = 5
getcontext().Emax = 2
a = Decimal("0.34")
b = Decimal("0.20")
assert a * b == Decimal("0.07")

使用可自定义的上下文

上下文类用于跟踪用于控制如何创建新小数的变量。这样每个程序甚至线程都能够设置它想要用于小数的小数位数。修改Context类有点啰嗦。以下是创建兼容Context的完整类。

class FixedPointContext(Context):

    def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
                       capitals=None, clamp=None, flags=None, traps=None,
                       _ignored_flags=None, decimal_places=None):
        super().__init__(prec, rounding, Emin, Emax, capitals, clamp, flags, 
                traps, _ignored_flags)
        try:
            dc = DefaultContext
        except NameError:
            pass
        self.decimal_places = decimal_places if decimal_places is not None else dc.decimal_places

    def __setattr__(self, name, value):
        if name == "decimal_places":
            object.__setattr__(self, name, value)
        else:
            super().__setattr__(name, value)

    def __reduce__(self):
        flags = [sig for sig, v in self.flags.items() if v]
        traps = [sig for sig, v in self.traps.items() if v]
        return (self.__class__,
                (self.prec, self.rounding, self.Emin, self.Emax,
                 self.capitals, self.clamp, flags, traps, self._ignored_flags,
                 self.decimal_places))

    def __repr__(self):
        """Show the current context."""
        s = []
        s.append('Context(prec=%(prec)d, rounding=%(rounding)s, '
                 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, '
                 'clamp=%(clamp)d, decimal_places=%(decimal_places)d'
                 % vars(self))
        names = [f.__name__ for f, v in self.flags.items() if v]
        s.append('flags=[' + ', '.join(names) + ']')
        names = [t.__name__ for t, v in self.traps.items() if v]
        s.append('traps=[' + ', '.join(names) + ']')
        return ', '.join(s) + ')'

    def _shallow_copy(self):
        """Returns a shallow copy from self."""
        nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
                     self.capitals, self.clamp, self.flags, self.traps,
                     self._ignored_flags, self.decimal_places)
        return nc

    def copy(self):
        """Returns a deep copy from self."""
        nc = Context(self.prec, self.rounding, self.Emin, self.Emax,
                     self.capitals, self.clamp,
                     self.flags.copy(), self.traps.copy(),
                     self._ignored_flags, self.decimal_places)
        return nc
    __copy__ = copy

# reinitialise default context
DefaultContext = FixedPointContext(decimal_places=2)

# copy changes over to decimal module
decimal.Context = FixedPointContext
decimal.DefaultContext = DefaultContext
Context = FixedPointContext

# test
decimal.getcontext().decimal_places = 1
decimal.getcontext().prec = 5
decimal.getcontext().Emax = 2
a = Decimal("0.34")
b = Decimal("0.20")
assert a * b == Decimal("0.1")