ROOT  6.06/09
Reference Guide
TMathBase.h
Go to the documentation of this file.
1 // @(#)root/base:
2 // Authors: Rene Brun, Fons Rademakers 29/07/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TMathBase
13 #define ROOT_TMathBase
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TMath Base functions //
19 // //
20 // Define the functions Min, Max, Abs, Sign, Range for all types. //
21 // NB: These functions are unfortunately not available in a portable //
22 // way in std::. //
23 // //
24 // More functions are defined in TMath.h. TMathBase.h is designed to be //
25 // a stable file and used in place of TMath.h in the ROOT miniCore. //
26 // //
27 //////////////////////////////////////////////////////////////////////////
28 
29 #ifndef ROOT_Rtypes
30 #include "Rtypes.h"
31 #endif
32 
33 #include <cstdlib>
34 #include <cmath>
35 
36 namespace TMath {
37 
38  // Abs
39  inline Short_t Abs(Short_t d);
40  inline Int_t Abs(Int_t d);
41  inline Long_t Abs(Long_t d);
42  inline Long64_t Abs(Long64_t d);
43  inline Float_t Abs(Float_t d);
44  inline Double_t Abs(Double_t d);
45  inline LongDouble_t Abs(LongDouble_t d);
46 
47  // Even/Odd
48  inline Bool_t Even(Long_t a);
49  inline Bool_t Odd(Long_t a);
50 
51  // SignBit
52  template<typename Integer>
53  inline Bool_t SignBit(Integer a);
54  inline Bool_t SignBit(Float_t a);
55  inline Bool_t SignBit(Double_t a);
56  inline Bool_t SignBit(LongDouble_t a);
57 
58  // Sign
59  template<typename T1, typename T2>
60  inline T1 Sign( T1 a, T2 b);
61  inline Float_t Sign(Float_t a, Float_t b);
62  inline Double_t Sign(Double_t a, Double_t b);
64 
65  // Min, Max of two scalars
66  inline Short_t Min(Short_t a, Short_t b);
67  inline UShort_t Min(UShort_t a, UShort_t b);
68  inline Int_t Min(Int_t a, Int_t b);
69  inline UInt_t Min(UInt_t a, UInt_t b);
70  inline Long_t Min(Long_t a, Long_t b);
71  inline ULong_t Min(ULong_t a, ULong_t b);
72  inline Long64_t Min(Long64_t a, Long64_t b);
73  inline ULong64_t Min(ULong64_t a, ULong64_t b);
74  inline Float_t Min(Float_t a, Float_t b);
75  inline Double_t Min(Double_t a, Double_t b);
76 
77  inline Short_t Max(Short_t a, Short_t b);
78  inline UShort_t Max(UShort_t a, UShort_t b);
79  inline Int_t Max(Int_t a, Int_t b);
80  inline UInt_t Max(UInt_t a, UInt_t b);
81  inline Long_t Max(Long_t a, Long_t b);
82  inline ULong_t Max(ULong_t a, ULong_t b);
83  inline Long64_t Max(Long64_t a, Long64_t b);
84  inline ULong64_t Max(ULong64_t a, ULong64_t b);
85  inline Float_t Max(Float_t a, Float_t b);
86  inline Double_t Max(Double_t a, Double_t b);
87 
88  // Range
89  inline Short_t Range(Short_t lb, Short_t ub, Short_t x);
90  inline Int_t Range(Int_t lb, Int_t ub, Int_t x);
91  inline Long_t Range(Long_t lb, Long_t ub, Long_t x);
92  inline ULong_t Range(ULong_t lb, ULong_t ub, ULong_t x);
93  inline Double_t Range(Double_t lb, Double_t ub, Double_t x);
94 
95  //NextPrime is used by the Core classes.
96  Long_t NextPrime(Long_t x); // Least prime number greater than x
97 }
98 
99 
100 //---- Even/odd ----------------------------------------------------------------
101 
103  { return ! (a & 1); }
104 
106  { return (a & 1); }
107 
108 //---- Abs ---------------------------------------------------------------------
109 
111 { return (d >= 0) ? d : Short_t(-d); }
112 
114 { return std::abs(d); }
115 
117 { return std::labs(d); }
118 
120 #if __cplusplus >= 201103
121 { return std::llabs(d); }
122 #else
123 { return (d >= 0) ? d : -d; }
124 #endif
125 
127 { return std::abs(d); }
128 
130 { return std::abs(d); }
131 
133 { return std::abs(d); }
134 
135 
136 //---- Sign Bit--------------------------------------------------------------------
137 
138 template<typename Integer>
139 inline Bool_t TMath::SignBit( Integer a)
140  { return (a < 0); }
141 
143  { return std::signbit(a); }
144 
146  { return std::signbit(a); }
147 
149  { return std::signbit(a); }
150 
151 
152 //---- Sign --------------------------------------------------------------------
153 
154 template<typename T1, typename T2>
155 inline T1 TMath::Sign( T1 a, T2 b)
156  { return (SignBit(b)) ? - Abs(a) : Abs(a); }
157 
159  { return std::copysign(a,b); }
160 
162  { return std::copysign(a,b); }
163 
165  { return std::copysign(a,b); }
166 
167 
168 //---- Min ---------------------------------------------------------------------
169 
171  { return a <= b ? a : b; }
172 
174  { return a <= b ? a : b; }
175 
177  { return a <= b ? a : b; }
178 
180  { return a <= b ? a : b; }
181 
183  { return a <= b ? a : b; }
184 
186  { return a <= b ? a : b; }
187 
189  { return a <= b ? a : b; }
190 
192  { return a <= b ? a : b; }
193 
195  { return a <= b ? a : b; }
196 
198  { return a <= b ? a : b; }
199 
200 //---- Max ---------------------------------------------------------------------
201 
203  { return a >= b ? a : b; }
204 
206  { return a >= b ? a : b; }
207 
209  { return a >= b ? a : b; }
210 
212  { return a >= b ? a : b; }
213 
215  { return a >= b ? a : b; }
216 
218  { return a >= b ? a : b; }
219 
221  { return a >= b ? a : b; }
222 
224  { return a >= b ? a : b; }
225 
227  { return a >= b ? a : b; }
228 
230  { return a >= b ? a : b; }
231 
232 //---- Range -------------------------------------------------------------------
233 
235  { return x < lb ? lb : (x > ub ? ub : x); }
236 
238  { return x < lb ? lb : (x > ub ? ub : x); }
239 
241  { return x < lb ? lb : (x > ub ? ub : x); }
242 
244  { return x < lb ? lb : (x > ub ? ub : x); }
245 
247  { return x < lb ? lb : (x > ub ? ub : x); }
248 
249 
250 #endif
long long Long64_t
Definition: RtypesCore.h:69
T1 Sign(T1 a, T2 b)
Definition: TMathBase.h:155
float Float_t
Definition: RtypesCore.h:53
unsigned short UShort_t
Definition: RtypesCore.h:36
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Definition: TMathBase.h:234
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
Bool_t Odd(Long_t a)
Definition: TMathBase.h:105
Double_t x[n]
Definition: legend1.C:17
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: vector.h:450
Bool_t Even(Long_t a)
Definition: TMathBase.h:102
unsigned int UInt_t
Definition: RtypesCore.h:42
short Short_t
Definition: RtypesCore.h:35
long double LongDouble_t
Definition: RtypesCore.h:57
long Long_t
Definition: RtypesCore.h:50
Long_t NextPrime(Long_t x)
TMath Base functions.
Definition: TMathBase.cxx:29
double Double_t
Definition: RtypesCore.h:55
unsigned long long ULong64_t
Definition: RtypesCore.h:70
unsigned long ULong_t
Definition: RtypesCore.h:51
#define T2
Definition: md5.inl:146
#define T1
Definition: md5.inl:145
Bool_t SignBit(Integer a)
Definition: TMathBase.h:139
Bool_t SignBit(LongDouble_t a)
Definition: TMathBase.h:148
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
LongDouble_t Abs(LongDouble_t d)
Definition: TMathBase.h:132