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


Groups > comp.lang.python > #20527

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'fixes': 0.05; ':-)': 0.06; 'attribute': 0.07; 'indeed,': 0.07; 'python': 0.08; '>>>>': 0.09; 'folks,': 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; "'int'": 0.16; '(say': 0.16; 'adjustment': 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; 'skip:) 10': 0.16; 'subject:result': 0.16; 'url:putitdown': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'simpler': 0.18; 'thanks,': 0.19; 'cheers,': 0.20; "haven't": 0.20; '(most': 0.21; 'tells': 0.21; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'formatting': 0.23; 'string': 0.24; 'index': 0.24; 'traceback': 0.24; 'scale': 0.25; 'function': 0.27; 'import': 0.27; 'subject:" ': 0.28; 'skip:" 30': 0.28; 'message-id:@mail.gmail.com': 0.29; "skip:' 10": 0.29; 'print': 0.29; '(and': 0.30; 'kelly': 0.30; 'precision': 0.30; 'subject:format': 0.30; "didn't": 0.30; 'error': 0.30; 'from:addr:googlemail.com': 0.31; 'version': 0.32; 'cases': 0.32; 'thu,': 0.32; "i've": 0.32; 'thanks': 0.32; 'received:209.85.216.46': 0.32; 'received:mail- qw0-f46.google.com': 0.32; 'object': 0.33; 'times.': 0.34; 'file': 0.34; 'daniel': 0.34; 'mostly': 0.34; 'done': 0.34; 'anything': 0.34; 'last):': 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; 'that.': 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; '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; '26,': 0.73; "'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=mpf/7MLi/ycjTEsaFS62cwwssjrfG0wVeZ0CKwd2tEk=; b=AEqcNeqIpQotlgnFvkBz8yZY7pwDEM6Sk1WEyUptXh+TYiXZJBFPyJcFA6LqqVKQ+Y j/NY8ZIePigXy4uH4uDZDbiEqAphy1etZGhpqD4ZqhEIBY7OkU0+XqPyZuKphzXSQkuL /vfd1GmdpwZvaI/FQs0e+X/EdwvvMES5YJcTk=
MIME-Version 1.0
In-Reply-To <CALwzidk2i40vAecq14f=_AeQM22CjQK-L_uf+44g7JHJYjxuPg@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> <CALwzidk2i40vAecq14f=_AeQM22CjQK-L_uf+44g7JHJYjxuPg@mail.gmail.com>
Date Thu, 16 Feb 2012 23:21:50 +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.5902.1329430918.27778.python-list@python.org> (permalink)
Lines 113
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1329430918 news.xs4all.nl 6843 [2001:888:2000:d::a6]:41727
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20527

Show key headers only | View raw


On 2/16/12, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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

Thanks, it's simpler indeed, but gives me an error for value=1.267, error=0.08:

Traceback (most recent call last):
  File "/home/fetchinson/bin/format_error", line 26, in <module>
    print format_error( sys.argv[1], sys.argv[2] )
  File "/home/fetchinson/bin/format_error", line 9, in format_error
    error_scale += error.scaleb( -error_scale ).to_integral(  ).adjusted(  )
  File "/usr/lib64/python2.6/decimal.py", line 3398, in scaleb
    ans = self._check_nans(other, context)
  File "/usr/lib64/python2.6/decimal.py", line 699, in _check_nans
    other_is_nan = other._isnan()
AttributeError: 'int' object has no attribute '_isnan'

Which version of python are you using?

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 23:21 +0100

csiph-web