format a measurement result and its error in "scientific" way
Ulrich Eckhardt
ulrich.eckhardt at dominolaser.com
Fri Feb 17 12:16:38 CET 2012
Am 16.02.2012 01:18, schrieb Daniel Fetchinson:
> 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)
Just so that I understand you, the value of the last "digit" is
somewhere between 9-9 and 9+9, right? So the value itself is rounded so
that its last digit has the same value as the only digit to which the
error is rounded.
> 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.
First step is to format the values as decimal string ('{0:f}'.format).
Then, pad both values to the same number of digits before and after the
radix separator. Then, iterate over the strings, those digits where the
error digits are zero are those that are taken verbatim for the value.
The following digit is rounded and then added to the result. The
according digit in the error is also rounded and added in brackets.
Note that you can not compute these "values", since the definition of
the rounding depends on a decimal representation. If you have decimal
floating point numbers, where 0.1 is representable without loss, you
could actually do that. In order to allow common treatment as decimal as
required for this algorithm, the first step above was the conversion to
a decimal string.
Write tests that make sure this works, e.g. I ignored any sign and you
also can't use str() to format the value because that could give you
"123.45e+5" as a result.
Good luck!
Uli
More information about the Python-list
mailing list