Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20497
| 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!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; ':-)': 0.06; 'folks,': 0.09; 'received:mail-lpp01m010-f46.google.com': 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; 'function?': 0.16; 'parentheses': 0.16; 'simplest': 0.16; 'subject:result': 0.16; 'cc:addr:python-list': 0.16; 'wed,': 0.17; '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; 'string': 0.24; 'index': 0.24; 'cc:2**0': 0.26; 'function': 0.27; 'subject:" ': 0.28; 'message-id:@mail.gmail.com': 0.29; "skip:' 10": 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; '(and': 0.30; 'precision': 0.30; 'subject:format': 0.30; "didn't": 0.30; 'error': 0.30; 'received:209.85.215.46': 0.32; "i've": 0.32; 'times.': 0.34; 'daniel': 0.34; 'done': 0.34; 'anything': 0.34; 'round': 0.34; 'probably': 0.35; 'something': 0.35; 'things': 0.35; 'skip:" 20': 0.35; '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; 'received:209.85.215': 0.39; 'received:209': 0.39; 'point': 0.40; "you'll": 0.61; 'simple': 0.61; 'your': 0.61; 'vary': 0.64; 'due': 0.66; 'strange': 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; bh=o3X1mAKTceGTyBG7lQYG3/QMwUziTw8I24Ffs6hxvuI=; b=e3bB5QdiQNYO+R5prP6IXk2qBUhgQyoxFuYcpvo/KDBjzfUTGnElUvktzoZ26+WWhC UTTyctVsm9D8yx/Xdenab30ZNL6wTSqHQoVwE+5VrWrH13XTPZCbE5z61HeJrOHRhoTR cT238x88f9bdToOYXpJQim5sUSx7EWacdf7po= |
| MIME-Version | 1.0 |
| In-Reply-To | <CADjSo4QhqgpXnd5wn=_cu_i=O15wTR8i-gAK=EbMsoggsZ+NQQ@mail.gmail.com> |
| References | <CADjSo4QhqgpXnd5wn=_cu_i=O15wTR8i-gAK=EbMsoggsZ+NQQ@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 15 Feb 2012 18:56:30 -0700 |
| Subject | Re: format a measurement result and its error in "scientific" way |
| To | Daniel Fetchinson <fetchinson@googlemail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | Python <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 <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.5871.1329357423.27778.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1329357423 news.xs4all.nl 6946 [2001:888:2000:d::a6]:35992 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20497 |
Show key headers only | View raw
On Wed, Feb 15, 2012 at 5:18 PM, Daniel Fetchinson
<fetchinson@googlemail.com> 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):
... 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. :-)
Cheers,
Ian
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: format a measurement result and its error in "scientific" way Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-15 18:56 -0700
csiph-web