# [ python-Bugs-1725899 ] decimal sqrt method doesn't use round-half-even

Sat May 26 01:30:33 CEST 2007

```Bugs item #1725899, was opened at 2007-05-25 22:52
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1725899&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Mark Dickinson (marketdickinson)
Assigned to: Nobody/Anonymous (nobody)
Summary: decimal sqrt method doesn't use round-half-even

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Mark Dickinson (marketdickinson)
Date: 2007-05-25 23:30

Message:
Logged In: YES
user_id=703403
Originator: YES

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."

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2007-05-25 23:24

Message:
Logged In: YES
user_id=31435
Originator: NO

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.

----------------------------------------------------------------------

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1725899&group_id=5470
```