Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67178

Re: extend methods of decimal module

Newsgroups comp.lang.python
Date 2014-02-27 15:43 -0800
References (5 earlier) <55525f2c-fd3a-4927-b642-2dbf5eae7e9b@googlegroups.com> <mailman.7422.1393512211.18130.python-list@python.org> <360e87d2-4daf-4222-8ebe-51f3e4d1fade@googlegroups.com> <mailman.7429.1393518292.18130.python-list@python.org> <9f7b535f-9e5d-45df-96f6-6cd8f6b4a524@googlegroups.com>
Message-ID <fda2f422-2534-40ef-8afc-d3dae3ca2ed4@googlegroups.com> (permalink)
Subject Re: extend methods of decimal module
From Wolfgang <xpysol@gmail.com>

Show all headers | View raw


On Friday, February 28, 2014 12:00:45 AM UTC+1, Mark H. Harris wrote:
> 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...
> >>> 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)

What Oscar tried to make clear is that Decimal(.1) does not store an "erroneous" float in the Decimal object, but an "honest" one.
Maybe this becomes clearer with calculations instead of literal values:

>>> 1000000000000000055511151231257827021181583404541015625/10**55
0.1

now try:
D(1000000000000000055511151231257827021181583404541015625/10**55)
vs
Decimal(1000000000000000055511151231257827021181583404541015625/10**55)
for an example that it is not better, in general, to go through the str()-mediated rounding you are doing.

Of course, if the user *intended* that float(.1) to mean exactly 0.1 then your D class does a good job at guessing, but only then.
The Decimal class, on the other hand, leaves the choice to the user as you are illustrating perfectly in your own example below:

> Now, try this experiment:    again from IDLE and dmath.py 
> 
> >>> from dmath import *
> >>> pi = D(piagm(32))
> >>> pi
> Decimal('3.14159265358979323846264338327950')
> >>> 
> >>> pi * Decimal(str(.1))
> Decimal('0.31415926535897932384626433832795')
> >>> 
> >>> pi * D(.1)
> Decimal('0.31415926535897932384626433832795')

see? Decimal(.1) gives the exact representation of the float, but Decimal('.1') is the way to get your D()'s default behavior.
 
 
> 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.

Yes, just that this is almost never what your users will want to happen ...
The Decimal constructor is really well designed, so stick to it !

Best,
Wolfgang

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-19 07:30 -0800
  Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-19 13:59 -0500
    Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-19 13:30 -0800
      Re: extend methods of decimal module Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-19 15:54 -0600
      Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-19 17:10 -0500
        Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 04:07 -0800
          Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 14:42 +0000
            Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 07:42 -0800
              Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 02:57 +1100
              Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 16:24 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 15:00 -0800
                Re: extend methods of decimal module Wolfgang <xpysol@gmail.com> - 2014-02-27 15:43 -0800
                Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-27 23:50 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 18:15 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 15:26 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 21:18 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 16:26 +1100
                Re: extend methods of decimal module Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-02-28 08:54 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:23 -0800
                Re: extend methods of decimal module Steven D'Aprano <steve@pearwood.info> - 2014-02-28 03:15 +0000
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 20:41 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 16:00 +1100
                Re: extend methods of decimal module Steven D'Aprano <steve@pearwood.info> - 2014-02-28 07:34 +0000
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 19:52 +1100
                Re: extend methods of decimal module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-28 15:11 +0000
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-03-01 02:36 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:34 -0800
                Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-03-01 05:37 +1100
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 11:26 -0800
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 11:39 -0800
                Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 12:17 -0800
          Re: extend methods of decimal module Terry Reedy <tjreedy@udel.edu> - 2014-02-27 12:07 -0500
            Re: extend methods of decimal module Anssi Saari <as@sci.fi> - 2014-03-01 08:55 +0200
              Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-01 16:49 -0800
          Re: extend methods of decimal module Chris Angelico <rosuav@gmail.com> - 2014-02-28 04:48 +1100
      Re: extend methods of decimal module Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-19 16:27 -0600
      Re: extend methods of decimal module casevh@gmail.com - 2014-02-19 21:11 -0800
        Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 02:33 -0800
          Re: extend methods of decimal module casevh@gmail.com - 2014-02-28 06:23 -0800
  Re: extend methods of decimal module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-19 22:29 +0000
    Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-27 02:37 -0800

csiph-web