### osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

# extend methods of decimal module

```On Thursday, February 27, 2014 10:24:23 AM UTC-6, Oscar Benjamin wrote:

>>>> from decimal import Decimal as D
> >>> D(0.1)
> Decimal('0.1000000000000000055511151231257827021181583404541015625')
>

hi Oscar,  well,  that's not what I'm doing with my  D()...  I'm not just making D() mimic Decimal... look inside it...  there's a  str()  call....   consider the following experiment and you'll see what I'm talking about...

Decimal does not keep  0.1  as a  floating point format (regardless of size) which is why banking can use Decimal without having to worry about the floating formatting issue...  in other words,  0.0 is not stored in Decimal as any kind of floating value...  its not rounded.... it really is   Decimal('0.1').

Ok, so for the experiment:   consider this exchange from IDLE:

>>> ==================== RESTART ==
>>> from dmath import *
>>> pi = D(piagm(32))
>>> pi
Decimal('3.14159265358979323846264338327950')
>>> pi * Decimal(.1)
Decimal('0.31415926535897934128560682837111')
>>>
>>> pi * D(.1)
Decimal('0.31415926535897932384626433832795')
>>>
>>>

You will notice that   Decimal(.1)  and  my  D(.1)  work against  PI  differently... in fact, Decimal(.1) decimates PI....   <sorry for the PUN>

The reason is that Decimal(.1) stores the erroneous float in the Decimal object including the float error for  .1    and   D(.1)  works correctly  because the D(.1) function in my dmath.py first converts the .1 to a string value before handing it to Decimal's constructor(s)

Now, try this experiment:    again from IDLE and dmath.py

>>> ===================== RESTART =
>>> from dmath import *
>>> pi = D(piagm(32))
>>> pi
Decimal('3.14159265358979323846264338327950')
>>>
>>> pi * Decimal(str(.1))
Decimal('0.31415926535897932384626433832795')
>>>
>>> pi * D(.1)
Decimal('0.31415926535897932384626433832795')
>>>

You see?   All of my functions make certain that when the Decimal objects are created that the string constructor gets called....   so that, in this case,   .1  really is  0.1   precisely, not floating format.

and take a look at this:

>>> ================ RESTART ==
>>> from dmath import *
>>> D(.1)
Decimal('0.1')
>>>

With my dmath  D()  function the object holds  0.1  precisely...   its not the float 0.1 rounded ...

shifting gears a bit....

The real reason I pushed a commit of a gzip tarball to code.google is because I've never used code.google before (thought I'd give it a try) and I didn't realize that its only pull / download is a ZIP...  so I am going to take your advice and remove the tarballs and just put the code up there... if I can get my GIT to work ... its GIVing me fits (probably because I don't have it configured right on this system.

Thanks for the discussion...  its helping me get this stuff into my brain, and its giving me a chance to discuss what's at issue between floats and Decimal.

kind regards,
marcus

```