This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fix Decimal.sqrt bugs described in #1725899
Type: Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: facundobatista Nosy List: facundobatista, mark.dickinson, nnorwitz
Priority: normal Keywords: patch

Created on 2007-06-22 06:54 by mark.dickinson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
decimal_sqrt.patch mark.dickinson, 2007-06-22 06:54 Patch for Decimal.sqrt()
decimal_sqrt_2.patch mark.dickinson, 2007-07-02 14:34
decimal_sqrt_3.patch mark.dickinson, 2007-07-18 03:37 updated patch for Decimal.sqrt and Decimal._fixexponents
Messages (7)
msg52792 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-06-22 06:54
This patch fixes a number of (yet-to-be-confirmed-as-) bugs in Decimal.sqrt();  see bug report 1725899 for details of these.

As a side benefit the modified version of Decimal.sqrt() runs significantly faster than the original, since it's based on integer arithmetic instead of carrying out Newton's method entirely in decimal;  on my iBook G4 the speedup is between 20 and 25 times with the default precision.
msg52793 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-06-22 07:09
Facundo, could you take a look?
msg52794 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2007-06-22 12:29
Yes, I'll handle this and the mentioned bug. Thanks.
msg52795 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-07-02 14:33
Here's an updated patch, with:
 -- several hundred extra testcases, in a new file squareroot_extra.decTest
 -- fixes for two bugs in the previous patch; one where the context rounding mode was occasionally used instead of ROUND-HALF-EVEN, and one involving underflow to 0.
 -- a fix for the following problem in Decimal._fixexponents():  (should I submit a separate patch for this instead?)

>>> from decimal import *
>>> getcontext().Emax = 9
>>> getcontext().Emin = -9
>>> getcontext()._clamp = 1
>>> +Decimal("1E10")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/lib/python2.5/decimal.py", line 935, in __pos__
    ans = self._fix(context)
  File "/opt/local/lib/python2.5/decimal.py", line 1530, in _fix
    ans = self._fixexponents(context)
  File "/opt/local/lib/python2.5/decimal.py", line 1565, in _fixexponents
    ans = ans._rescale(Etop, context=context)
  File "/opt/local/lib/python2.5/decimal.py", line 1913, in _rescale
    return context._raise_error(InvalidOperation, 'Rescale > prec')
  File "/opt/local/lib/python2.5/decimal.py", line 2325, in _raise_error
    raise error, explanation
decimal.InvalidOperation: Rescale > prec

I've been in contact with Cowlishaw;  he's agreed that it looks as though the C reference implementation needs a few changes, but hasn't had a chance to look at things properly yet;  I've sent him the extra testcases above as well.  Please let me know if there's anything else I can do to help.

Mark
msg52796 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-07-02 14:34
File Added: decimal_sqrt_2.patch
msg52797 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-07-18 03:37
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
msg52798 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2007-08-02 03:13
Applied two modified patchs, see revisions 56654 to 56656. the main difference is that Mark regenerated the diff against the decimal-branch, where I'm developing the decimal module.

He is making a great job in this regard, thanks Mark!
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45120
2007-06-22 06:54:03mark.dickinsoncreate