Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'snippet': 0.07; '1200,': 0.09; '>>>>': 0.09; 'curious.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; '25,': 0.12; 'wrote:': 0.15; '@property': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; '>>>': 0.16; 'def': 0.16; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'skip:l 30': 0.25; 'changed': 0.25; "i'm": 0.27; 'cc:addr:python.org': 0.30; 'class': 0.31; 'chris': 0.32; 'actually': 0.33; 'header:User-Agent:1': 0.34; 'there': 0.34; 'but': 0.37; '1200': 0.38; 'subject:: ': 0.38; 'else': 0.38; 'two': 0.38; 'needed.': 0.40; 'your': 0.61; 'received:websitewelcome.com': 0.65; 'jun': 0.67; 'received:184': 0.67; 'received:69.93': 0.67; 'digits?': 0.84 Date: Mon, 27 Jun 2011 13:53:17 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Harold Subject: Re: Significant figures calculation References: <10b8388e-76fc-4a93-aeff-676cdb3d1d12@5g2000yqb.googlegroups.com> <4e04f793$0$29975$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:4885 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1309207197 news.xs4all.nl 4366 [::ffff:82.94.164.166]:33794 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8514 Harold wrote: > On Jun 25, 9:04 pm, Chris Torek wrote: >>> I'm curious. Is there a way to get the number of significant digits >>> for a particular Decimal instance? >> Yes: >> >> def sigdig(x): >> "return the number of significant digits in x" >> return len(x.as_tuple()[1]) > > Great, Chris, this is (almost) exactly what I needed. > To make it work for numbers like 1200, that have four digits but only > two of them being significant, I changed your snippet to the > following: > > class Empirical(Decimal) : > @property > def significance(self) : > t = self.as_tuple() > if t[2] < 0 : > return len(t[1]) > else : > return len(''.join(map(str,t[1])).rstrip('0')) > > >>>> Empirical('1200.').significance > 2 >>>> Empirical('1200.0').significance > 5 What about when 1200 is actually 4 significant digits? Or 3? ~Ethan~