#!/usr/bin/env python
# vim: set fileencoding=UTF-8 :


__author__ = 'Xitong Gao'
__email__ = 'xtg08@ic.ac.uk'


import decimal
decimal.getcontext().traps[decimal.Inexact] = True


class ExactDecimal(decimal.Decimal):

    def __init__(self, v):
        if not isinstance(v, str):
            raise ValueError('Value is not string, could be inexact')
        super(ExactDecimal, self).__init__(v)

    def _exact_operation(self, op, other):
        while True:
            try:
                return decimal.Decimal.__dict__['__%s__' % op](self, other)
            except decimal.Inexact:
                decimal.getcontext().prec += 1

    def __add__(self, other):
        return self._exact_operation('add', other)

    def __sub__(self, other):
        return self._exact_operation('sub', other)

    def __mul__(self, other):
        return self._exact_operation('mul', other)


class AbstractInterval(object):

    def __init__(self, min_val, max_val):
        pass
        

class AbstractErrorSemantics(object):

    def __init__(self, ):
        pass


if __name__ == '__main__':
    print ExactDecimal('2.89638143766236577531434781') / \
            ExactDecimal('6.7952943728618031095770')
    print decimal.getcontext().prec

@@ -110,173 +110,165 @@
 '%0A%0A%0A
-import decimal%0Adecimal.getcontext().traps%5Bdecimal.Inexact%5D = True%0A%0A%0Aclass ExactDecimal(decimal.Decimal):%0A%0A    def __init__(self, v):%0A        if not isinstance(v, str
+%0A%0Aclass Interval(object):%0A%0A    def __init__(self, (min_val, max_val)):%0A        self.min, self.max = min_val, max_val%0A        if type(min_val) != type(max_val
 ):%0A 
@@ -288,20 +288,19 @@
 ise 
-Valu
+Typ
 eError('
 Valu
@@ -299,333 +299,121 @@
 or('
-Value is not string, could be inexact')%0A        super(ExactDecimal, self).__init__(v)%0A%0A    def _exact_operation(self, op, other):%0A        while True:%0A            try:%0A                return decimal.Decimal.__dict__%5B'__%25s__' %25 op%5D(self, other)%0A            except decimal.Inexact:%0A                decimal.getcontext().prec += 1
+min_val and max_val must be of the same type')%0A%0A    def __iter__(self):%0A        return iter((self.min, self.max))
 %0A%0A  
@@ -459,42 +459,61 @@
 urn 
-self._exact_operation('add',
+Interval(%5Bself.min + other.min, self.max +
  other
+.max%5D
 )%0A%0A 
@@ -560,263 +560,249 @@
 urn 
-self._exact_operation('sub', other)%0A%0A    def __mul__(self, other):%0A        return self._exact_operation('mul', other)%0A%0A%0Aclass AbstractInterval(object):%0A%0A    def __init__(self, min_val, max_val):%0A        pass%0A        %0A%0Aclass AbstractErrorSemantics(object):
+Interval(%5Bself.min - other.max, self.max - other.min%5D)%0A%0A    def __mul__(self, other):%0A        v = (self.min * other.min, self.min * other.max,%0A             self.max * other.min, self.max * other.max)%0A        return Interval(%5Bmin(v), max(v)%5D)
 %0A%0A  
@@ -809,29 +809,26 @@
   def __
-init
+str
 __(self
-, 
 ):%0A     
@@ -834,186 +834,55 @@
     
-pass%0A%0A%0Aif __name__ == '__main__':%0A    print ExactDecimal('2.89638143766236577531434781') / %5C%0A            ExactDecimal('6.7952943728618031095770')%0A    print decimal.getcontext().prec
+return '%5B%25s, %25s%5D' %25 (str(self.min), str(self.max))
 %0A

Add Interval