Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'fixes': 0.05; ':-)': 0.06; '>>>>': 0.09; 'folks,': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'rounded': 0.09; 'rounding': 0.09; 'subject:error': 0.09; 'am,': 0.12; 'def': 0.13; 'binary': 0.13; 'float': 0.13; '16,': 0.15; 'skip:v 30': 0.15; '(say': 0.16; 'adjustment': 0.16; 'function?': 0.16; 'parentheses': 0.16; 'simplest': 0.16; 'subject:result': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'cheers,': 0.20; "haven't": 0.20; 'tells': 0.21; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'formatting': 0.23; 'string': 0.24; 'index': 0.24; 'scale': 0.25; 'cc:2**0': 0.26; 'function': 0.27; 'import': 0.27; 'subject:" ': 0.28; 'message-id:@mail.gmail.com': 0.29; "skip:' 10": 0.29; 'cc:addr:python.org': 0.29; '(and': 0.30; 'precision': 0.30; 'subject:format': 0.30; "didn't": 0.30; 'error': 0.30; 'cases': 0.32; 'received:209.85.215.46': 0.32; 'thu,': 0.32; "i've": 0.32; 'thanks': 0.32; 'times.': 0.34; 'daniel': 0.34; 'mostly': 0.34; 'done': 0.34; 'anything': 0.34; 'round': 0.34; 'probably': 0.35; '...': 0.35; 'something': 0.35; 'things': 0.35; 'skip:" 20': 0.35; 'two': 0.36; 'digit': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'skip:" 10': 0.37; 'received:209.85': 0.38; 'could': 0.38; 'some': 0.38; 'format': 0.38; 'becomes': 0.39; 'skip:\xa0 10': 0.39; 'that.': 0.39; "i'd": 0.39; 'except': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'point': 0.40; 'skip:s 40': 0.40; "you'll": 0.61; 'simple': 0.61; 'your': 0.61; 'vary': 0.64; 'due': 0.66; 'strange': 0.68; 'works,': 0.68; "'e'": 0.84; 'risk.': 0.84; 'so:': 0.84; 'subject:its': 0.84; 'expresses': 0.91; 'skip:v 40': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=9baRQzuBGJR5tJMjGiQvEmt5FKPIWIPBW0fWbhwFNTo=; b=B33StyBiR8yc9gRNZ/2zPr46m4sUXl9qnU7uuBt/WtFgqKj4aFAEHTeleaNN+VLEfK 0mbjQyestnwwb+jOVppUyEndqE7//sLK5oKBTUThrI/ErmT44OFoppnPSymR2P5XbyF7 a3qjlanzwF4sPXldkJQ8eoSvIrhN4O4pkb8Fc= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 16 Feb 2012 10:34:18 -0700 Subject: Re: format a measurement result and its error in "scientific" way To: Daniel Fetchinson Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329413691 news.xs4all.nl 6958 [2001:888:2000:d::a6]:36742 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20520 On Thu, Feb 16, 2012 at 1:36 AM, Daniel Fetchinson wrote: >>> Hi folks, often times in science one expresses a value (say >>> 1.03789291) and its error (say 0.00089) in a short way by parentheses >>> like so: 1.0379(9) >>> >>> One can vary things a bit, but let's take the simplest case when we >>> only keep 1 digit of the error (and round it of course) and round the >>> value correspondingly. I've been searching around for a simple >>> function that would take 2 float arguments and would return a string >>> but didn't find anything although something tells me it's been done a >>> gazillion times. >>> >>> What would be the simplest such function? >> >> Well, this basically works: >> >>>>> def format_error(value, error): >> ... =A0 =A0 precision =3D int(math.floor(math.log(error, 10))) >> ... =A0 =A0 format =3D "%%.%df(%%d)" % max(-precision, 0) >> ... =A0 =A0 return format % (round(value, -precision), >> ... =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0int(round(error / 10 ** p= recision))) >> ... >>>>> format_error(1.03789291, 0.00089) >> '1.0379(9)' >> >> Note that "math.floor(math.log(error, 10))" may return the wrong >> decimal precision due to binary floating point rounding error, which >> could produce some strange results: >> >>>>> format_error(10378929, 1000) >> '10378900(10)' >> >> So you'll probably want to use decimals instead: >> >> def format_error(value, error): >> =A0 =A0 value =3D decimal.Decimal(value) >> =A0 =A0 error =3D decimal.Decimal(error) >> =A0 =A0 value_scale =3D value.log10().to_integral(decimal.ROUND_FLOOR) >> =A0 =A0 error_scale =3D error.log10().to_integral(decimal.ROUND_FLOOR) >> =A0 =A0 precision =3D value_scale - error_scale >> =A0 =A0 if error_scale > 0: >> =A0 =A0 =A0 =A0 format =3D "%%.%dE" % max(precision, 0) >> =A0 =A0 else: >> =A0 =A0 =A0 =A0 format =3D "%%.%dG" % (max(precision, 0) + 1) >> =A0 =A0 value_str =3D format % value.quantize(decimal.Decimal("10") ** >> error_scale) >> =A0 =A0 error_str =3D '(%d)' % error.scaleb(-error_scale).to_integral() >> =A0 =A0 if 'E' in value_str: >> =A0 =A0 =A0 =A0 index =3D value_str.index('E') >> =A0 =A0 =A0 =A0 return value_str[:index] + error_str + value_str[index:] >> =A0 =A0 else: >> =A0 =A0 =A0 =A0 return value_str + error_str >> >>>>> format_error(1.03789291, 0.00089) >> '1.0379(9)' >>>>> format_error(103789291, 1000) >> '1.03789(1)E+08' >> >> I haven't tested this thoroughly, so use at your own risk. :-) > > Thanks a lot, this indeed mostly works, except for cases when the > error needs to be rounded up and becomes two digits: > >>>> format_error( '1.34883', '0.0098' ) > '1.349(10)' > > But in this case I'd like to see 1.35(1) A small adjustment to the scale fixes that. Also tidied up the string formatting part: import decimal def format_error(value, error): value =3D decimal.Decimal(value) error =3D decimal.Decimal(error) error_scale =3D error.adjusted() error_scale +=3D error.scaleb(-error_scale).to_integral().adjusted() value_str =3D str(value.quantize(decimal.Decimal("1E%d" % error_scale))= ) error_str =3D '(%d)' % error.scaleb(-error_scale).to_integral() if 'E' in value_str: index =3D value_str.index('E') return value_str[:index] + error_str + value_str[index:] else: return value_str + error_str Cheers, Ian