CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
value.py
Go to the documentation of this file.
1 # Copyright (C) 2014 Colin Bernet
2 # https://github.com/cbernet/heppy/blob/master/LICENSE
3 
4 import math
5 import copy
6 
7 class Value(object):
8  def __init__(self, val, err):
9  self.val = val
10  self.err = err
11 
12  def relerr(self):
13  return abs(self.err / self.val)
14 
15  def __iadd__(self, other):
16  self.val += other.val
17  self.err = math.sqrt( self.err*self.err + other.err*other.err)
18  return self
19 
20  def __add__(self, other):
21  new = copy.deepcopy(self)
22  new += other
23  return new
24 
25  def __isub__(self, other):
26  self.val -= other.val
27  self.err = math.sqrt( self.err*self.err + other.err*other.err)
28  return self
29 
30  def __sub__(self, other):
31  new = copy.deepcopy(self)
32  new -= other
33  return new
34 
35  def __idiv__(self, other):
36  relerr = math.sqrt( self.relerr()*self.relerr() + other.relerr()*other.relerr())
37  self.val /= other.val
38  self.err = relerr * self.val
39  return self
40 
41  def __div__(self, other):
42  new = copy.deepcopy(self)
43  new /= other
44  return new
45 
46  def __str__(self):
47  return '{val:10.3f} +- {err:8.3f} ({relerr:5.2f}%)'.format(val=self.val,
48  err=self.err,
49  relerr=self.relerr()*100)
50 
51 if __name__=='__main__':
52 
53  val1 = Value(1.,0.02)
54  val2 = Value(2.,0.02)
55  val3 = val1 / val2
56  print val1
57  print val2
58  print
59  print val1+val2
60  print val1-val2
61  print val1/val2
def __idiv__
Definition: value.py:35
def __div__
Definition: value.py:41
def __isub__
Definition: value.py:25
def __str__
Definition: value.py:46
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def __init__
Definition: value.py:8
list object
Definition: dbtoconf.py:77
def __add__
Definition: value.py:20
def relerr
Definition: value.py:12
def __sub__
Definition: value.py:30
def __iadd__
Definition: value.py:15