Final(?) version of the patch is attached. The extra testcases for sqrt have been vetted, corrected (see below) and slightly expanded by Mike Cowlishaw, and are, I believe, going to be included in the next update on his website. The file squareroot_extra.decTest included in the patch is exactly the file I received back from Cowlishaw, with no changes. The code and comments have been cleaned up a little.
The patch also fixes another bug in the _fixexponents method: in the following, the Underflow flag should not be raised.
Python 2.5.1 (r251:54863, May 10 2007, 20:59:25)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> c = getcontext()
>>> c.Emin = -9
>>> c.Emax = 9
>>> Decimal(2).sqrt() # Inexact but not Subnormal
Decimal("1.414213562373095048801688724")
>>> +Decimal("100E-38") # Subnormal but not Inexact
Decimal("1E-36")
>>> c
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-9, Emax=9, capitals=1, flags=[Rounded, Underflow, Subnormal, Inexact], traps=[DivisionByZero, Overflow, InvalidOperation])
There's still one outstanding issue: Cowlishaw says that for a general arithmetic operation, underflow of a nonzero result to zero should result in the Clamped flag being raised; I believe that the current testcases from Cowlishaw's website reflect this, while the older ones in the decimaltestdata directory don't. (This is the source of the only corrections to the previous test cases.) Currently sqrt() does raise Clamped on underflow to 0, but none of the other arithmetic operations do, which is inconsistent. I'd be happy to fix this, but I think discussion is needed first: should sqrt be `fixed' to not raise Clamped here (this would be trivial to do); should
the other arithmetic operations be fixed to raise Clamped on underflow to 0 (also trivial, except that it would require updating many
of the testcases), or should things be left as they are? Any suggestions?
(By the way, I also have a working Decimal.log, if anyone's interested...)
Mark
Mark
File Added: decimal_sqrt_3.patch
|