1 |
#include <iostream>
|
2 |
#include <TMath.h>
|
3 |
#define ValueClassLoaded
|
4 |
|
5 |
using namespace std;
|
6 |
|
7 |
|
8 |
/*
|
9 |
*
|
10 |
* This file contains the class "Unit", the version is 1.0
|
11 |
* This class stores the value and error of a given parameter and is able to perform error propagation for addition, subtraction, multiplication, division and even powers. Please add any bugs you find to the change log below.
|
12 |
*
|
13 |
* V1.0
|
14 |
* - First implementation with operators +,-,*,/ and pow; also implemented a custom "<<" operator (i.e. overloaded it)
|
15 |
*
|
16 |
*/
|
17 |
|
18 |
class Value
|
19 |
{
|
20 |
float value;
|
21 |
float error;
|
22 |
|
23 |
public:
|
24 |
Value(float value, float error);
|
25 |
float getValue();
|
26 |
float getError();
|
27 |
Value operator+(const Value& c);
|
28 |
Value operator-(const Value& c);
|
29 |
Value operator/(const Value& c);
|
30 |
Value operator*(const Value& c);
|
31 |
Value Pow(Value a, Value b);
|
32 |
};
|
33 |
|
34 |
Value::Value(float newvalue = 0, float newerror = -999) : value(newvalue) {
|
35 |
if(newerror!=-999) {
|
36 |
error=newerror;
|
37 |
} else {
|
38 |
error = TMath::Sqrt(newvalue);
|
39 |
}
|
40 |
}
|
41 |
|
42 |
float Value::getValue()
|
43 |
{
|
44 |
return value;
|
45 |
}
|
46 |
float Value::getError()
|
47 |
{
|
48 |
return error;
|
49 |
}
|
50 |
|
51 |
Value Value::operator+(const Value& c)
|
52 |
{//verified
|
53 |
Value result;
|
54 |
result.value = (this->value+c.value);
|
55 |
result.error = TMath::Sqrt((this->error)*(this->error)+(c.error)*(c.error));
|
56 |
return result;
|
57 |
}
|
58 |
|
59 |
Value Value::operator-(const Value& c)
|
60 |
{//verified
|
61 |
Value result;
|
62 |
result.value = (this->value-c.value);
|
63 |
result.error = TMath::Sqrt((this->error)*(this->error)+(c.error)*(c.error));
|
64 |
return result;
|
65 |
}
|
66 |
|
67 |
Value Value::operator*(const Value& c)
|
68 |
{//verified
|
69 |
Value result;
|
70 |
result.value = (this->value*c.value);
|
71 |
result.error = TMath::Sqrt(c.value*c.value*(this->error)*(this->error)+(this->value)*(this->value)*(c.error)*(c.error));
|
72 |
return result;
|
73 |
}
|
74 |
|
75 |
Value Value::operator/(const Value& c)
|
76 |
{//verified
|
77 |
Value result;
|
78 |
result.value = (this->value/c.value);
|
79 |
result.error = TMath::Sqrt((1/(c.value*c.value))*(this->error)*(this->error) + ((this->value/(c.value*c.value))*(this->value/(c.value*c.value)))*(c.error*c.error));
|
80 |
return result;
|
81 |
}
|
82 |
|
83 |
Value Value::Pow(Value a, Value b)
|
84 |
{
|
85 |
//this calculates e.g. sqrt(a), where b=(0.5,0)
|
86 |
Value result;
|
87 |
result.value = TMath::Power(a.value,b.value);
|
88 |
result.error=TMath::Power(a.value,b.value)*TMath::Sqrt(TMath::Power(a.value,-2)*(b.value-1)*(b.value-1)*(a.error*a.error)+(TMath::Log(a.value)*TMath::Log(a.value))*(b.error*b.error));
|
89 |
return result;
|
90 |
}
|
91 |
|
92 |
std::ostream &operator<<(std::ostream &ostr, Value v)
|
93 |
{//this leads to an output like 2.000 +/- 1.000
|
94 |
return ostr << v.getValue() << " +/- " << v.getError();
|
95 |
}
|
96 |
|
97 |
/*
|
98 |
|
99 |
int main()
|
100 |
{
|
101 |
cout << "This is my value class" << endl;
|
102 |
Value a(2,2);
|
103 |
Value b(16,7);
|
104 |
Value sq(0.5);
|
105 |
Value c = a/b;
|
106 |
Value d = d.Pow(b,sq);
|
107 |
cout << c << endl;
|
108 |
cout << "log 11 " << TMath::Log(11) << endl;
|
109 |
cout << "Power: " << d << endl;
|
110 |
return 0;
|
111 |
}
|
112 |
*/
|