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


Groups > comp.lang.python > #20505

Re: format a measurement result and its error in "scientific" way

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <fetchinson@googlemail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; ':-)': 0.06; '>>>>': 0.09; 'folks,': 0.09; 'received:209.85.216.53': 0.09; 'received:mail-qw0-f53.google.com': 0.09; 'rounded': 0.09; 'rounding': 0.09; 'subject:error': 0.09; 'def': 0.13; 'binary': 0.13; 'float': 0.13; 'skip:v 30': 0.15; '(say': 0.16; 'from:addr:fetchinson': 0.16; 'from:name:daniel fetchinson': 0.16; 'function?': 0.16; 'parentheses': 0.16; 'psss,': 0.16; 'simplest': 0.16; 'subject:result': 0.16; 'url:putitdown': 0.16; '>>>': 0.18; 'arguments': 0.18; 'cheers,': 0.20; "haven't": 0.20; 'tells': 0.21; 'header:In-Reply-To:1': 0.22; 'string': 0.24; 'index': 0.24; 'function': 0.27; 'subject:" ': 0.28; 'message- id:@mail.gmail.com': 0.29; "skip:' 10": 0.29; '(and': 0.30; 'precision': 0.30; 'subject:format': 0.30; "didn't": 0.30; 'error': 0.30; 'from:addr:googlemail.com': 0.31; 'cases': 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; 'to:addr:python-list': 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; "i'd": 0.39; 'except': 0.39; 'received:209': 0.39; 'point': 0.40; 'put': 0.40; 'to:addr:python.org': 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=googlemail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=aBlq0RYQ+3hsOV5fQWuMXAw+bA7HgsVZc2oqeMvF6ro=; b=IKtJuWxLE5a9IozvVeD2OIktr/Lvx/SHdinxw0uMRPU64EQ7WVjji3sxeqTnw6LWJx bvlCDh9NhWEzmABYOqQk5mOWnFnhyoxUHR2Bdo/zHsRd64muMJkHsVlk7U09BGf8fr4g degL8JIvIFVtuzWvaSFIzoYTBYxClU+XW2S4s=
MIME-Version 1.0
In-Reply-To <CALwzidk=KX42EwZBe8jCJ9YQT_FNHG8GGU_ogNhtv7myBptSjQ@mail.gmail.com>
References <CADjSo4QhqgpXnd5wn=_cu_i=O15wTR8i-gAK=EbMsoggsZ+NQQ@mail.gmail.com> <CALwzidk=KX42EwZBe8jCJ9YQT_FNHG8GGU_ogNhtv7myBptSjQ@mail.gmail.com>
Date Thu, 16 Feb 2012 09:36:20 +0100
Subject Re: format a measurement result and its error in "scientific" way
From Daniel Fetchinson <fetchinson@googlemail.com>
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5878.1329381383.27778.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1329381383 news.xs4all.nl 6846 [2001:888:2000:d::a6]:45561
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20505

Show key headers only | View raw


>> 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):
> ...     precision = int(math.floor(math.log(error, 10)))
> ...     format = "%%.%df(%%d)" % max(-precision, 0)
> ...     return format % (round(value, -precision),
> ...                      int(round(error / 10 ** precision)))
> ...
>>>> 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):
>     value = decimal.Decimal(value)
>     error = decimal.Decimal(error)
>     value_scale = value.log10().to_integral(decimal.ROUND_FLOOR)
>     error_scale = error.log10().to_integral(decimal.ROUND_FLOOR)
>     precision = value_scale - error_scale
>     if error_scale > 0:
>         format = "%%.%dE" % max(precision, 0)
>     else:
>         format = "%%.%dG" % (max(precision, 0) + 1)
>     value_str = format % value.quantize(decimal.Decimal("10") **
> error_scale)
>     error_str = '(%d)' % error.scaleb(-error_scale).to_integral()
>     if 'E' in value_str:
>         index = value_str.index('E')
>         return value_str[:index] + error_str + value_str[index:]
>     else:
>         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)

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown

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


Thread

Re: format a measurement result and its error in "scientific" way Daniel Fetchinson <fetchinson@googlemail.com> - 2012-02-16 09:36 +0100

csiph-web