[Python-ideas] Python Numbers as Human Concept Decimal System

Andrew Barnert abarnert at yahoo.com
Sat Mar 8 22:35:14 CET 2014


On Mar 8, 2014, at 13:01, "Mark H. Harris" <harrismh777 at gmail.com> wrote:

> 
> 
> 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? ---------------------

It looks like you're trying to emulate a pocket calculator here. The question is, why are you accepting floats in the first place if that's your goal?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140308/ad55f1ce/attachment.html>


More information about the Python-ideas mailing list