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: decimal sqrt method doesn't use round-half-even
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: facundobatista Nosy List: facundobatista, mark.dickinson, tim.peters
Priority: normal Keywords:

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

Messages (8)
msg32126 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-05-25 22:52
According to version 1.66 of Cowlishaw's `General Decimal Arithmetic
Specification' the square-root operation in the decimal module should
round using the round-half-even algorithm (regardless of the rounding
setting in the current context).  It doesn't appear to do so:

>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(9123455**2).sqrt()
Decimal("9.12345E+6")

The exact value of this square root is exactly halfway between two
representable Decimals, so using round-half-even with 6 digits I'd
expect the answer to be rounded to the neighboring representable
Decimal with *even* last digit---that is,

Decimal("9.12346E+6")

This bug only seems to occur when the number of significant digits in
the argument exceeds the current precision (indeed, if the number of
sig. digits in the argument is less than or equal to the current
precision then it's impossible for the square root to be halfway
between two representable floats).

It seems to me that this is a minor bug that will occur rarely and is
unlikely to have any serious effect even when it does occur; however,
it does seem to be a deviation from the specification.
msg32127 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2007-05-25 23:24
Doesn't the spec also require that /inputs/ get rounded to working precision /before/ operations are performed?  If so, the exact square 83237431137025 is rounded down to 83237400000000, and sqrt(83237400000000) is 9.12345E+6 rounded to 6 digits.
msg32128 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-05-25 23:30
I don't think inputs should be rounded; note 1 near the start of the `Arithmetic Operations' section of the specification says:

"Operands may have more than precision digits and are not rounded before use."
msg32129 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-06-22 06:16
Some more strange results for sqrt():  with Emax = 9, Emin = -9 and prec = 3:

1. In the following, should the Subnormal and Underflow flags be set?  The result before rounding is subnormal, even though the post-rounding result is not, and the spec seems to say that those flags should be set in this situation.  But Cowlishaw's reference implementation (version 3.50) doesn't set these flags. (If 9.998 is replaced by 9.99 then the flags *are* set, which seems inconsistent.)

>>> Decimal("9.998E-19").sqrt()
Decimal("1.00E-9")
>>> getcontext()
Context(prec=3, rounding=ROUND_HALF_EVEN, Emin=-9, Emax=9, capitals=1, flags=[Rounded, Inexact], traps=[DivisionByZero, Overflow, InvalidOperation])

2. As I understand the spec, the following result is incorrect:

>>> Decimal("1.12E-19").sqrt()
Decimal("3.4E-10")

(The true value of the square root is 3.34664...e-10, which should surely be rounded to 3.3E-10, not 3.4E-10?).
But again, Cowlishaw's reference implementation also gives 3.4E-10.

3. Similarly for the following

>>> Decimal("4.21E-20").sqrt()
Decimal("2.0E-10")

The answer should, I think, be 2.1E-10;  here the reference implementation also gives 2.1E-10.

4. And I can't justify this one at all...

>>> Decimal("1.01001").sqrt()
Decimal("1.01")

Shouldn't this be 1.00?  But again Python agrees with the reference implementation.

Either all this is pretty mixed up, or I'm fundamentally misunderstanding things.  I have a patch that I think fixes all these bugs;  if there's any agreement that they really are bugs then I'll post it.  Can anyone shed any light on the above results?
msg32130 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-06-22 06:35
One more result. This is definitely getting suspicious now--I'll submit my patch.

>>> from decimal import *
>>> getcontext().prec = 3
>>> Decimal(11772).sqrt()
Decimal("109")
>>> Decimal(11774).sqrt()
Decimal("108")

msg32131 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2007-06-22 16:06
Of course you're right, the spec does say inputs shouldn't be rounded.  And that last example is horrendous:  sqrt should definitely be monotonic (a floating function f is "monotonic" if it guarantees f(x) >= f(y) whenever x >= y; you found x and y such that x > y but sqrt(x) < sqrt(y) -- ouch!).
msg32132 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-06-22 18:57
See patch 1741308.  I'll contact Mike Cowlishaw to verify that the reference implementation really is buggy.
msg32133 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2007-08-02 03:15
Fixed in the revisions 56654 to 56656, in the decimal-branch, using patches generated by Mark, thanks!
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44997
2007-05-25 22:52:08mark.dickinsoncreate