A more current and elaborate description of the class Measurement can be found in the Python Cookbook, 2nd edition, p.677, edited by Alex Martinelli, Anna Martinelli Ravenscroft & David Ascher, O'Reilly, March 2005, ISBN 0-596-00797-3

Mario Hilgemeier

Measurement Arithmetics

# An arithmetics class for measurement values of known percentual error
# This class uses the error propagation formulae of physicists
# --------------------------
# arithmetics uses number tupels consisting of
# measurement value val and error perc (in percent)
# uses operator overloading of '+','-','*','/'
# use '/' only if val != 0 (division by 0 problem)
# --------------------------
# version 2001-12-19 by Mario Hilgemeier
# I'd like to hear of modifications or extensions, e.g.
# addition of other error types (absolute, last digit +-1,
# last digit +-0.5 i.e. digit after last digit +-5)

--------------------------------------

exemplary use:

execfile("measurement.py")

m1 = Measurement(100.0, 5.5) # i.e. a measured value of 100.0 with 5.5% error
m2 = Measurement(50, 2)
print "m1 = ", m1
print "m2 = ", m2
print "m1 + m2 = ", m1 + m2
print "m1 - m2 = ", m1 - m2
print "m1 * m2 = ", m1 * m2
print "m1 / m2 = ", m1 / m2
print "(m1+m2) * (m1-m2) = ", (m1+m2) * (m1-m2)
print "(m1-m2) / (m1+m2) = ", (m1-m2) / (m1+m2)

# results:
# m1 =  (100,5.5)
# m2 =  (50,2)
# m1 + m2 =  (150,3.72678)
# m1 - m2 =  (50,11.1803)
# m1 * m2 =  (5000,5.85235)
# m1 / m2 =  (2,5.85235)
# (m1+m2) * (m1-m2) =  (7500,11.7851)
# (m1-m2) / (m1+m2) =  (0.333333,11.7851)

--------------------------------------

Discussion:

Good for automatic error computation for large formulae in physics or engineering.

Do not divide by zero.

I'd like to hear of modifications or extensions, e.g.
addition of other error types (absolute, last digit +-1,
last digit +-0.5 i.e. digit after last digit +-5),
fuzzy logic, and similar error propagating methods.

--------------------------------------

Download Python code measurement.py


Algorithms and Program Codes


© Copyright 2001-2005 Mario Hilgemeier, email: contact
homepage stylized apple blossom