On Saturday, March 8, 2014 2:16:00 PM UTC-6, Mark H. Harris wrote:
On Saturday, March 8, 2014 12:49:02 PM UTC-6, Mark Dickinson wrote:
- if we're aiming to eliminate surprises, the 'fix' doesn't go far enough: Decimal(1.1 + 2.2) will still surprise, as will {snip}
How about this then: I think I've got it...
>>> ===== RESTART =========================
>>> from pdeclib import *
>>> d(1.1+2.2)
Decimal('3.3')
>>> sqrt(1.1+2.2)**2
Decimal('3.29999999999999999999999999999999999999999')
>>> x=d(1.1)
>>> y=d(2.2)
>>> sqrt(x+y)**2
Decimal('3.29999999999999999999999999999999999999999')
>>>
Code Below--------------------------
#*****************************************************************/
# sqrt(x) return decimal sqrt(x) rounded to context precision
#*****************************************************************/
def sqrt(x):
""" sqrt(x) square root function
(x may be string, int, float, or decimal)
(returns decimal rounded to context precision)
"""
with localcontext(ctx=None) as cmngr:
cmngr.prec+=14
if (isinstance(x, float)==True):
y=x.__round__(15)
sqr=Decimal(repr(y)).sqrt()
else:
sqr=Decimal(x).sqrt()
return +sqr
-------------------- what say you? ---------------------