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


Groups > comp.lang.python > #20520

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

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 <ian.g.kelly@gmail.com>
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 <CADjSo4RpDwohyNFvudqTCX8vdOFd-sHMUZE0adkg6P=sU8jNwQ@mail.gmail.com>
References <CADjSo4QhqgpXnd5wn=_cu_i=O15wTR8i-gAK=EbMsoggsZ+NQQ@mail.gmail.com> <CALwzidk=KX42EwZBe8jCJ9YQT_FNHG8GGU_ogNhtv7myBptSjQ@mail.gmail.com> <CADjSo4RpDwohyNFvudqTCX8vdOFd-sHMUZE0adkg6P=sU8jNwQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 16 Feb 2012 10:34:18 -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
Content-Transfer-Encoding quoted-printable
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.5894.1329413691.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, Feb 16, 2012 at 1:36 AM, 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. :-)
>
> 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 = decimal.Decimal(value)
    error = decimal.Decimal(error)
    error_scale = error.adjusted()
    error_scale += error.scaleb(-error_scale).to_integral().adjusted()
    value_str = str(value.quantize(decimal.Decimal("1E%d" % 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

Cheers,
Ian

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


Thread

Re: format a measurement result and its error in "scientific" way Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-16 10:34 -0700

csiph-web