Added AliL3Stopwatch.
[u/mrichter/AliRoot.git] / HLT / misc / AliL3FFloat.cxx
CommitLineData
62e60107 1//$Id$
2
3// Author: Constantin Loizides <mailto:loizides@fi.uib.no>
4//*-- Copyright & copy CL
5
6#include <stream.h>
7#include <math.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include "AliL3Logging.h"
12#include "AliL3Logger.h"
13#include "AliL3FFloat.h"
14
15/** \class AliL3FFloat
16//<pre>
17//----------------------------------------------------
18// AliL3FFloat
19//
20// Fixed Floating Point class for debugging purposes.
21//
22// The class behaves like a normal Double_t class but
23// calculates everything in respect to fDigits (eg.
24// fDigits=100 -> 2 digits behind the comma). Further-
25// more it keeps the exact value in floating precision
26// and gathers some statistical information about
27// its usage.
28//</pre>
29 */
30
62e60107 31ClassImp(AliL3FFloat)
32
27835353 33Int_t AliL3FFloat::fDigits = DEFDIG;
34Int_t AliL3FFloat::fMax = DEFMAX;
35Int_t AliL3FFloat::fMin = DEFMIN;
36Char_t AliL3FFloat::fQuery[10] = "%.2f";
37
38#ifdef CALCSTATS
39Int_t AliL3FFloat::fN = 0;
40Int_t AliL3FFloat::fNRounded = 0;
41Int_t AliL3FFloat::fNOpAdds = 0;
42Int_t AliL3FFloat::fNOpMults = 0;
43Int_t AliL3FFloat::fNOpDivs = 0;
44Int_t AliL3FFloat::fNOpSubs = 0;
45Int_t AliL3FFloat::fNOverFlow = 0;
62e60107 46Int_t AliL3FFloat::fNUnderFlow = 0;
27835353 47Double_t AliL3FFloat::fNDiff = 0;
48#endif
49
50
51AliL3FFloat::~AliL3FFloat()
52{
53#ifdef CALCSTATS
54 fNDiff+=fabs(fVal-fExactVal);
55#endif
56}
62e60107 57
58ostream& operator<<(ostream &os, const AliL3FFloat &f)
59{
60 // os << (Double_t)f << endl;
27835353 61 os << (Double_t)f << "(" << f.fExactVal << ")";
62e60107 62 return os;
63}
64
65AliL3FFloat operator + (const AliL3FFloat &f1,const AliL3FFloat &f2)
66{
67 AliL3FFloat r(f1);
68 r+=f2;
69 return r;
70}
71
72AliL3FFloat operator + (const AliL3FFloat &f1,const Double_t f2)
73{
74 AliL3FFloat r(f1);
75 r+=f2;
76 return r;
77}
78
79AliL3FFloat operator + (const Double_t f1,const AliL3FFloat &f2)
80{
81 AliL3FFloat r(f1);
82 r+=f2;
83 return r;
84}
85
86AliL3FFloat operator + (const AliL3FFloat &f)
87{
88 AliL3FFloat r(f);
89 return r;
90}
91
92
93AliL3FFloat operator - (const AliL3FFloat &f1,const AliL3FFloat &f2)
94{
95 AliL3FFloat r(f1);
27835353 96 r-=f2;
62e60107 97 return r;
98}
99
100AliL3FFloat operator - (const AliL3FFloat &f1,const Double_t f2)
101{
102 AliL3FFloat r(f1);
103 r-=f2;
104 return r;
105}
106
107AliL3FFloat operator - (const Double_t f1,const AliL3FFloat &f2)
108{
109 AliL3FFloat r(f1);
110 r-=f2;
111 return r;
112}
113
114AliL3FFloat operator - (const AliL3FFloat &f)
115{
116 AliL3FFloat r((-(Double_t)f));
117 return r;
118}
119
120AliL3FFloat operator * (const AliL3FFloat &f1,const AliL3FFloat &f2)
121{
122 AliL3FFloat r(f1);
123 r*=f2;
124 return r;
125}
126
27835353 127AliL3FFloat operator * (const AliL3FFloat &f1,const Double_t f2)
128{
129 AliL3FFloat r(f1);
130 r*=f2;
131 return r;
132}
133
134AliL3FFloat operator * (const Double_t f1,const AliL3FFloat &f2)
135{
136 AliL3FFloat r(f1);
137 r*=f2;
138 return r;
139}
140
62e60107 141AliL3FFloat operator / (const AliL3FFloat &f1,const AliL3FFloat &f2)
142{
143 AliL3FFloat r(f1);
144 r/=f2;
145 return r;
146}
147
27835353 148AliL3FFloat operator / (const AliL3FFloat &f1,const Double_t f2)
149{
150 AliL3FFloat r(f1);
151 r/=f2;
152 return r;
153}
154
155AliL3FFloat operator / (const Double_t f1,const AliL3FFloat &f2)
156{
157 AliL3FFloat r(f1);
158 r/=f2;
159 return r;
160}
161
62e60107 162AliL3FFloat& AliL3FFloat::operator += (const AliL3FFloat &f)
163{
164 Double_t ev=fExactVal+f.GetExactVal();
165 Set(fVal+(Double_t)f);
166 fExactVal=ev;
27835353 167#ifdef CALCSTATS
62e60107 168 fNOpAdds++;
27835353 169#endif
62e60107 170 return *this;
171}
172
173AliL3FFloat& AliL3FFloat::operator += (const Double_t f)
174{
175 Double_t ev=fExactVal+f;
176 Set(fVal+Round(f));
177 fExactVal=ev;
27835353 178#ifdef CALCSTATS
62e60107 179 fNOpAdds++;
27835353 180#endif
62e60107 181 return *this;
182}
183
184AliL3FFloat& AliL3FFloat::operator -= (const AliL3FFloat &f)
185{
186 Double_t ev=fExactVal-f.GetExactVal();
187 Set(fVal-(Double_t)f);
188 fExactVal=ev;
27835353 189#ifdef CALCSTATS
62e60107 190 fNOpSubs++;
27835353 191#endif
62e60107 192 return *this;
193}
194
195AliL3FFloat& AliL3FFloat::operator -= (const Double_t f)
196{
197 Double_t ev=fExactVal-f;
198 Set(fVal-Round(f));
199 fExactVal=ev;
27835353 200#ifdef CALCSTATS
62e60107 201 fNOpSubs++;
27835353 202#endif
62e60107 203 return *this;
204}
205
206AliL3FFloat& AliL3FFloat::operator *= (const AliL3FFloat &f)
207{
208 Double_t ev=fExactVal*f.GetExactVal();
209 Set(fVal*(Double_t)f);
210 fExactVal=ev;
27835353 211#ifdef CALCSTATS
62e60107 212 fNOpMults++;
27835353 213#endif
62e60107 214 return *this;
215}
216
217AliL3FFloat& AliL3FFloat::operator *= (const Double_t f)
218{
219 Double_t ev=fExactVal*f;
220 Set(fVal*Round(f));
221 fExactVal=ev;
27835353 222#ifdef CALCSTATS
62e60107 223 fNOpMults++;
27835353 224#endif
62e60107 225 return *this;
226}
227
228AliL3FFloat& AliL3FFloat::operator /= (const AliL3FFloat &f)
229{
230 Double_t ev=fExactVal/f.GetExactVal();
231 Set(fVal/(Double_t)f);
232 fExactVal=ev;
27835353 233#ifdef CALCSTATS
62e60107 234 fNOpDivs++;
27835353 235#endif
62e60107 236 return *this;
237}
238
239AliL3FFloat& AliL3FFloat::operator /= (const Double_t f)
240{
241 Double_t ev=fExactVal/f;
242 Set(fVal/Round(f));
243 fExactVal=ev;
27835353 244#ifdef CALCSTATS
62e60107 245 fNOpDivs++;
27835353 246#endif
62e60107 247 return *this;
248}
249
27835353 250inline void AliL3FFloat::Set(Double_t val)
62e60107 251{
252 fVal=Round(val);
253 fExactVal=val;
254 CheckBounds();
27835353 255#ifdef CALCSTATS
62e60107 256 fN++;
27835353 257#endif
62e60107 258}
259
27835353 260inline void AliL3FFloat::Set(AliL3FFloat &f)
62e60107 261{
262 fVal=(Double_t)f;
263 fExactVal=f.GetExactVal();
264 CheckBounds();
27835353 265#ifdef CALCSTATS
62e60107 266 fN++;
27835353 267#endif
62e60107 268}
269
27835353 270#ifdef FASTWITHROUNDINDERROS
271inline Double_t AliL3FFloat::Round(Double_t val)
62e60107 272{
273 Int_t dummy=Int_t(fDigits*val);
274 Double_t ret=(Double_t)(dummy)/fDigits;
27835353 275#ifdef CALCSTATS
62e60107 276 if(ret!=val) fNRounded++;
27835353 277#endif
62e60107 278 return ret;
279}
27835353 280#else
281inline Double_t AliL3FFloat::Round(Double_t val)
282{
283 static Char_t strnum[100];
284 sprintf(strnum,fQuery,val);
285 Double_t ret=atof(strnum);
286#ifdef CALCSTATS
287 if(ret!=val) fNRounded++;
288#endif
289 return ret;
290}
291#endif
62e60107 292
27835353 293inline Bool_t AliL3FFloat::CheckUpperBound()
62e60107 294{
295 if(fVal>fMax){
296 fVal=fMax;
27835353 297#ifdef CALCSTATS
62e60107 298 fNOverFlow++;
27835353 299#endif
62e60107 300 return kFALSE;
301 }
302
303 return kTRUE;
304}
305
27835353 306inline Bool_t AliL3FFloat::CheckLowerBound()
62e60107 307{
308 if(fVal<fMin){
309 fVal=fMin;
27835353 310#ifdef CALCSTATS
62e60107 311 fNUnderFlow++;
27835353 312#endif
62e60107 313 return kFALSE;
314 }
315
316 return kTRUE;
317}
318
319void AliL3FFloat::SetParams(Int_t dig,Int_t min,Int_t max)
320{
321 fDigits=dig;
27835353 322 Int_t prec=0;
323 if(fDigits>0) prec=(Int_t)log10(fDigits);
324 sprintf(fQuery,"%%.%df",prec);
62e60107 325 fMin=min;
326 fMax=max;
327}
328
329void AliL3FFloat::PrintStat(){
27835353 330#ifdef CALCSTATS
62e60107 331 cout << "fN: " << fN << endl;
332 cout << "fNRounded: " << fNRounded << endl;
333 cout << "fNOpAdds: " << fNOpAdds << endl;
334 cout << "fNOpSubs: " << fNOpSubs << endl;
335 cout << "fNOpMults: " << fNOpMults << endl;
336 cout << "fNOpDivs: " << fNOpDivs << endl;
337 cout << "fNOpOverFlow: " << fNOverFlow << endl;
338 cout << "fNOpUnderFlow: " << fNUnderFlow << endl;
27835353 339 if(fN) cout << "fNDiff: " << fNDiff/fN << endl;
340#else
341 cerr << "Not compiled with #define CALCSTATS!" << endl;
342#endif
343
62e60107 344}