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


Groups > comp.lang.python > #67155

Re: extend methods of decimal module

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'binary': 0.07; 'diff': 0.07; 'float': 0.07; 'referring': 0.07; 'string': 0.09; '0.1': 0.09; 'builtin': 0.09; 'committing': 0.09; 'constructor': 0.09; 'converts': 0.09; 'literal': 0.09; 'rounds': 0.09; 'subject:module': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'systems.': 0.12; 'archive': 0.14; 'thread': 0.14; 'benjamin': 0.16; 'cc:name:python list': 0.16; "chris'": 0.16; 'clarified': 0.16; 'constructor.': 0.16; 'digits.': 0.16; 'finite': 0.16; 'idea:': 0.16; 'inputs': 0.16; 'literals': 0.16; 'literals.': 0.16; 'module?': 0.16; 'nearest': 0.16; 'points:': 0.16; 'rounding': 0.16; 'rounding.': 0.16; 'skip:d 60': 0.16; 'them...': 0.16; 'files.': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; 'module': 0.19; 'normally': 0.19; 'work,': 0.20; '>>>': 0.22; 'code,': 0.22; 'input': 0.22; 'import': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'bytes': 0.24; 'exists': 0.24; 'mathematical': 0.24; 'string,': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'code:': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'host': 0.29; 'generally': 0.29; 'converting': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'asked': 0.31; 'code': 0.31; 'lines': 0.31; 'decimal': 0.31; 'object.': 0.31; 'seemingly': 0.31; 'anyone': 0.31; 'file': 0.32; 'supposed': 0.32; 'another': 0.32; 'text': 0.33; 'not.': 0.33; 'plain': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'common': 0.35; 'convert': 0.35; 'equal': 0.35; 'form.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'false': 0.36; 'introducing': 0.36; 'leads': 0.36; 'module.': 0.36; 'surely': 0.36; 'method': 0.36; 'thanks': 0.36; 'represent': 0.38; 'handle': 0.38; 'whatever': 0.38; 'files': 0.38; 'fact': 0.38; 'rather': 0.38; 'does': 0.39; 'bad': 0.39; 'called': 0.40; 'space': 0.40; 'how': 0.40; 'days': 0.60; 'even': 0.60; 'skip:u 10': 0.60; 'mentioned': 0.61; 'new': 0.61; 'numbers': 0.61; 'simple': 0.61; "you're": 0.61; 'save': 0.62; 'show': 0.63; 'such': 0.63; 'to:addr:gmail.com': 0.65; 'viewed': 0.74; 'browser.': 0.78; 'alongside': 0.84; 'oscar': 0.84; 'so...': 0.84; 'space...': 0.84; 'url:master': 0.84; 'habit': 0.91; 'imagine': 0.93; 'directly.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=xXipJLX88sfVqi2N/ZUdvXqeFE7YKWJB0Svh3EZMfV0=; b=POz6Jg2cply0DjAGMzyb4j7hN5EEbcvzy4LgmxvbJO9yIl7cUI1aNzDuUxlGF/Ys/9 QJSgG79YJ0Z1tEGMYfsMiZIYO7lNfI/ujCxXPBZAy9ZVZ7X1YSQ0K46snMGhYrK1hUQy zVg4QPDnbvFfn+RBAd35OL6dCRw5aH0nJ9OS5l9AqmeVqfW23yrpZxCIa6IUiKC9mEPD 2v/YPopzoax3nbpAeIZKiMXIW5Ix7ebjj615ZtL1kh/15Bw9vau/d+uqeU2wQpaFQ+RZ za0eYcX30SPrPDxdlIhsh8ednVr9Rxb+9II//8PUQIWKWJYKt6j3ZMCDTxlmIQKMOheA wJ2g==
X-Received by 10.52.166.81 with SMTP id ze17mr1747423vdb.35.1393518284010; Thu, 27 Feb 2014 08:24:44 -0800 (PST)
MIME-Version 1.0
In-Reply-To <360e87d2-4daf-4222-8ebe-51f3e4d1fade@googlegroups.com>
References <02cdd9c7-aef7-4cc7-a813-cd1c9627ceb4@googlegroups.com> <mailman.7160.1392836388.18130.python-list@python.org> <94b1962a-0004-4c5b-b484-972a166b88b5@googlegroups.com> <CAKJDb-NaZmLysq+2KWgd_f4UPKCk7jdLZkvcCM6ECL5TSAwCRQ@mail.gmail.com> <mailman.7165.1392847869.18130.python-list@python.org> <55525f2c-fd3a-4927-b642-2dbf5eae7e9b@googlegroups.com> <mailman.7422.1393512211.18130.python-list@python.org> <360e87d2-4daf-4222-8ebe-51f3e4d1fade@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 27 Feb 2014 16:24:23 +0000
Subject Re: extend methods of decimal module
To "Mark H. Harris" <harrismh777@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7429.1393518292.18130.python-list@python.org> (permalink)
Lines 86
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393518292 news.xs4all.nl 2904 [2001:888:2000:d::a6]:52036
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67155

Show key headers only | View raw


On 27 February 2014 15:42, Mark H. Harris <harrismh777@gmail.com> wrote:
> On Thursday, February 27, 2014 8:42:55 AM UTC-6, Oscar Benjamin wrote:
>
>>
>> Some points:
>
>    Thanks so much... you have clarified some things I was struggling with...
>
>> 1) Why have you committed the code as a .tar.gz file?
>
>     um, to save space... well, I know its tiny, but its just a habit I have... 5kb instead of 25kb...

It made it awkward for me to see the code you were referring to. In a
separate thread you asked about how to share code samples. These days
it's common practice to host code somewhere it can be viewed from a
browser. This means that I can do things like link to a line in Chris'
code:
https://github.com/Rosuav/ExceptExpr/blob/master/examples.py#L169

There are many other reasons (as mentioned by Chris) why committing a
binary archive instead of just the plain old files is a generally bad
idea with standard version control systems. A relevant one is that
every change you make to your code, even if you only change 2 lines
will require an additional 5kb of space to store the new .tar.gz file
alongside the old rather than 200 bytes or whatever it is to store the
diff of the code files.

>> 2) This function is not such a good idea:
>>
>> def D(numform):
>>     return Decimal(str(numform))
>
>     The reason for this is not for strings, but for float literals.  All of my functions may take a float literal and things still work, because the D(float) function converts the float literal to a string, which is then passed to the Decimal constructor.    so... this works  sqrt(0.1)  or,  sqrt(2.01)   Without the D() function float literals may not be passed to the Decimal because it really cannot handle them...  0.1  and others cause it a fit...    is there another way to do what I'm looking for here..?

I think you're misunderstanding what's going on:

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

There is no binary floating point value that is exactly equal to the
mathematical number 0.1. So when you write 0.1 in Python the number is
rounded to the nearest binary floating point number. When you create a
Decimal from a float it will create a Decimal with the *exact* same
value that the float had. That's exactly what you're seeing above.

When you print a float normally it pretends to have the value 0.1 but
that's really a lie:
>>> 0.1  # There is no float with the true value 0.1!!!!!
0.1
>>> 0.1 == D('0.1')
False

When you convert a float to a string it does not show the exact value
of the float but rather a rounded, shortened decimal form. By
converting float->str->Decimal you are introducing unnecessary
rounding. The Decimal constructor deliberately never rounds its inputs
and every float can be represented as a Decimal with a finite number
of digits. So Decimal(float) is exact but your D function is not.

It is precisely the fact that binary floating point cannot represent
seemingly simple non-integer decimal numbers like 0.1 that leads to
the creation of the decimal module. But why would anyone pass a float
into one of your dmath functions since they're surely much slower than
the math module? The main reason that I can imagine is that they would
like a really accurate result in which case rounding all floats on
input is unacceptable.

>      This has confused me, because when I look into the module help text I don't see roun() all I see is
>     __round__(...)     how do I know when the   round()  method exists vs.  __round__(...)  ??

The round builtin function calls __round__ on any object. Dunder
methods are not supposed to be called directly.


Oscar

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