[Python-ideas] Python Numbers as Human Concept Decimal System
Mark H. Harris
harrismh777 at gmail.com
Sat Mar 8 21:16:00 CET 2014
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}
>
On the other hand, how is this now possible?
>>> ====== RESTART ==========================
>>> from pdeclib import *
>>> d(1.1+2.2)
Decimal('3.3')
>>> sqrt(1.1+2.2)
Decimal('1.81659021245849499925351968583091621951684')
>>> sqrt(1.1+2.2)**2
Decimal('3.29999999999999999999999999999999999999999')
>>>
def sqrt(x):
""" sqrt(x) square root function
(x may be string, int, float, or decimal)
(returns decimal rounded to context precision)
"""
y=x.__round__(15)
with localcontext(ctx=None) as cmngr:
cmngr.prec+=14
sqr=Decimal(repr(y)).sqrt()
return +sqr
What say you?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140308/221d8c53/attachment.html>
More information about the Python-ideas
mailing list