diff -r 3a880d640981 Lib/decimal.py --- a/Lib/decimal.py Tue Sep 18 07:21:18 2012 +0300 +++ b/Lib/decimal.py Tue Sep 18 09:49:58 2012 +0200 @@ -6050,6 +6050,21 @@ ##### Setup Specific Contexts ############################################ +class IEEEContext(Context): + """Return the decimal IEEE 754 context. + n should be a multiple of 32.""" + def __init__(self, n): + assert n >= 32 and n % 32 == 0 + + prec = 9 * n//32 - 2 + Emax = 3 * 2**(n//16 + 3) + + super(IEEEContext, self).__init__( + prec=prec, + Emax=Emax, + ) + + # The default context prototype used by Context() # Is mutable, so that new contexts can have different default values diff -r 3a880d640981 Lib/test/test_decimal.py --- a/Lib/test/test_decimal.py Tue Sep 18 07:21:18 2012 +0300 +++ b/Lib/test/test_decimal.py Tue Sep 18 09:49:58 2012 +0200 @@ -3644,6 +3644,18 @@ class PySpecialContexts(SpecialContexts): decimal = P +class TestIEEEContext(unittest.TestCase): + + def test_precision(self): + decimal32 = self.decimal.IEEEContext(32) + decimal64 = self.decimal.IEEEContext(64) + decimal128 = self.decimal.IEEEContext(128) + decimal256 = self.decimal.IEEEContext(256) + + # example ieee754, section 3.6 + self.assertEquals(decimal256.prec, 70) + self.assertEquals(decimal256.Emax, 1572864) + class ContextInputValidation(unittest.TestCase): def test_invalid_context(self):