Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include "RtypesCore.h"
30
31#include <cstdlib>
32#include <cmath>
33#include <algorithm>
34
35namespace TMath {
36
37 // Abs
38 inline Short_t Abs(Short_t d);
39 inline Int_t Abs(Int_t d);
40 inline Long_t Abs(Long_t d);
41 inline Long64_t Abs(Long64_t d);
42 inline Float_t Abs(Float_t d);
43 inline Double_t Abs(Double_t d);
45
46 // Even/Odd
47 inline Bool_t Even(Long_t a);
48 inline Bool_t Odd(Long_t a);
49
50 // SignBit
51 template<typename Integer>
52 inline Bool_t SignBit(Integer a);
53 inline Bool_t SignBit(Float_t a);
54 inline Bool_t SignBit(Double_t a);
56
57 // Sign
58 template<typename T1, typename T2>
59 inline T1 Sign( T1 a, T2 b);
60 inline Float_t Sign(Float_t a, Float_t b);
63
64 // Min, Max of two scalars
65 inline Short_t Min(Short_t a, Short_t b);
67 inline Int_t Min(Int_t a, Int_t b);
68 inline UInt_t Min(UInt_t a, UInt_t b);
69 inline Long_t Min(Long_t a, Long_t b);
70 inline ULong_t Min(ULong_t a, ULong_t b);
73 inline Float_t Min(Float_t a, Float_t b);
75
76 inline Short_t Max(Short_t a, Short_t b);
78 inline Int_t Max(Int_t a, Int_t b);
79 inline UInt_t Max(UInt_t a, UInt_t b);
80 inline Long_t Max(Long_t a, Long_t b);
81 inline ULong_t Max(ULong_t a, ULong_t b);
84 inline Float_t Max(Float_t a, Float_t b);
86
87 // Range
89 inline Int_t Range(Int_t lb, Int_t ub, Int_t x);
93
94 //NextPrime is used by the Core classes.
95 Long_t NextPrime(Long_t x); // Least prime number greater than x
96
97 // Binary search
98 template <typename T> Long64_t BinarySearch(Long64_t n, const T *array, T value);
99 template <typename T> Long64_t BinarySearch(Long64_t n, const T **array, T value);
100
101 // Sorting
102 template <typename Element, typename Index>
103 void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
104 template <typename Iterator, typename IndexIterator>
105 void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
106}
107
108
109//---- Even/odd ----------------------------------------------------------------
110
111/// Returns `true` if `a` is even.
113 { return ! (a & 1); }
114
115/// Returns `true` if `a` is odd.
117 { return (a & 1); }
118
119//---- Abs ---------------------------------------------------------------------
120
121/// Returns the absolute value of parameter `Short_t d`.
123{ return (d >= 0) ? d : Short_t(-d); }
124
125/// Returns the absolute value of parameter `Int_t d`.
127{ return std::abs(d); }
128
129/// Returns the absolute value of parameter `Long_t d`.
131{ return std::labs(d); }
132
133/// Returns the absolute value of parameter `Long64_t d`.
135{ return std::llabs(d); }
136
137/// Returns the absolute value of parameter `Float_t d`.
139{ return std::abs(d); }
140
141/// Returns the absolute value of parameter `Double_t d`.
143{ return std::abs(d); }
144
145/// Returns the absolute value of parameter `LongDouble_t d`.
147{ return std::abs(d); }
148
149
150//---- Sign Bit--------------------------------------------------------------------
151
152/// Returns whether the sign of `Integer a` is negative.
153template<typename Integer>
154inline Bool_t TMath::SignBit( Integer a)
155 { return (a < 0); }
156
157/// Returns whether the sign of `Float_t a` is negative.
159 { return std::signbit(a); }
160
161/// Returns whether the sign of `Double_t a` is negative.
163 { return std::signbit(a); }
164
165/// Returns whether the sign of `LongDouble_t a` is negative.
167 { return std::signbit(a); }
168
169
170//---- Sign --------------------------------------------------------------------
171
172/// Returns a value with the magnitude of `a` and the sign of `b`.
173template<typename T1, typename T2>
175 { return (SignBit(b)) ? - Abs(a) : Abs(a); }
176
177/// Returns a value with the magnitude of `a` and the sign of `b`.
178/// `a`and `b` are `Short_t`.
180 { return std::copysign(a,b); }
181
182/// Returns a value with the magnitude of `a` and the sign of `b`.
183/// `a`and `b` are `Double_t`.
185 { return std::copysign(a,b); }
186
187/// Returns a value with the magnitude of `a` and the sign of `b`.
188/// `a`and `b` are `LongDouble_t`.
190 { return std::copysign(a,b); }
191
192
193//---- Min ---------------------------------------------------------------------
194
195/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
196/// `a`and `b` are `Short_t`.
198 { return a <= b ? a : b; }
199
200/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
201/// `a`and `b` are `UShort_t`.
203 { return a <= b ? a : b; }
204
205/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
206/// `a`and `b` are `Int_t`.
208 { return a <= b ? a : b; }
209
210/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
211/// `a`and `b` are `Short_t`.
213 { return a <= b ? a : b; }
214
215/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
216/// `a`and `b` are `Long_t`.
218 { return a <= b ? a : b; }
219
220/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
221/// `a`and `b` are `ULong_t`.
223 { return a <= b ? a : b; }
224
225/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
226/// `a`and `b` are `Long64_t`.
228 { return a <= b ? a : b; }
229
230/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
231/// `a`and `b` are `ULong64_t`.
233 { return a <= b ? a : b; }
234
235/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
236/// `a`and `b` are `Float_t`.
238 { return a <= b ? a : b; }
239
240/// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
241/// `a`and `b` are `Double_t`.
243 { return a <= b ? a : b; }
244
245//---- Max ---------------------------------------------------------------------
246
247/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
248/// `a`and `b` are `Short_t`.
250 { return a >= b ? a : b; }
251
252/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
253/// `a`and `b` are `UShort_t`.
255 { return a >= b ? a : b; }
256
257/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
258/// `a`and `b` are `Int_t`.
260 { return a >= b ? a : b; }
261
262/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
263/// `a`and `b` are `UInt_t`.
265 { return a >= b ? a : b; }
266
267/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
268/// `a`and `b` are `Long_t`.
270 { return a >= b ? a : b; }
271
272/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
273/// `a`and `b` are `ULong_t`.
275 { return a >= b ? a : b; }
276
277/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
278/// `a`and `b` are `Long64_t`.
280 { return a >= b ? a : b; }
281
282/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
283/// `a`and `b` are `ULong64_t`.
285 { return a >= b ? a : b; }
286
287/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
288/// `a`and `b` are `Float_t`.
290 { return a >= b ? a : b; }
291
292/// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
293/// `a`and `b` are `Double_t`.
295 { return a >= b ? a : b; }
296
297//---- Range -------------------------------------------------------------------
298
299/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
300/// `lb`, `ub` and `x` are `Short_t`.
302 { return x < lb ? lb : (x > ub ? ub : x); }
303
304/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
305/// `lb`, `ub` and `x` are `Int_t`.
307 { return x < lb ? lb : (x > ub ? ub : x); }
308
309/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
310/// `lb`, `ub` and `x` are `Long_t`.
312 { return x < lb ? lb : (x > ub ? ub : x); }
313
314/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
315/// `lb`, `ub` and `x` are `ULong_t`.
317 { return x < lb ? lb : (x > ub ? ub : x); }
318
319/// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
320/// `lb`, `ub` and `x` are `Double_t`.
322 { return x < lb ? lb : (x > ub ? ub : x); }
323
324/// Binary search in an array of n values to locate value.
325///
326/// Array is supposed to be sorted prior to this call.
327/// If match is found, function returns position of element.
328/// If no match found, function gives nearest element smaller than value.
329template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T *array, T value)
330{
331 const T* pind;
332 pind = std::lower_bound(array, array + n, value);
333 if ( (pind != array + n) && (*pind == value) )
334 return (pind - array);
335 else
336 return ( pind - array - 1);
337}
338
339/// Binary search in an array of n values to locate value.
340///
341/// Array is supposed to be sorted prior to this call.
342/// If match is found, function returns position of element.
343/// If no match found, function gives nearest element smaller than value.
344template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
345{
346 const T* pind;
347 pind = std::lower_bound(*array, *array + n, value);
348 if ( (pind != *array + n) && (*pind == value) )
349 return (pind - *array);
350 else
351 return ( pind - *array - 1);
352}
353
354template<typename T>
356
358
359 template<typename Index>
360 bool operator()(Index i1, Index i2) {
361 return *(fData + i1) > *(fData + i2);
362 }
363
365};
366
367template<typename T>
369
371
372 template<typename Index>
373 bool operator()(Index i1, Index i2) {
374 return *(fData + i1) < *(fData + i2);
375 }
376
378};
379
380/// Sort the n1 elements of the Short_t array defined by its
381/// iterators. In output the array index contains the indices of
382/// the sorted array. If down is false sort in increasing order
383/// (default is decreasing order).
384///
385/// NOTE that the array index must be created with a length bigger
386/// or equal than the main array before calling this function.
387template <typename Iterator, typename IndexIterator>
388void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
389{
390 int i = 0;
391
393 for ( Iterator cfirst = first; cfirst != last; ++cfirst )
394 {
395 *cindex = i++;
396 ++cindex;
397 }
398
399 if ( down )
400 std::sort(index, cindex, CompareDesc<Iterator>(first) );
401 else
402 std::sort(index, cindex, CompareAsc<Iterator>(first) );
403}
404
405/// Sort the n elements of the array a of generic templated type Element.
406/// In output the array index of type Index contains the indices of the sorted array.
407/// If down is false sort in increasing order (default is decreasing order).
408///
409/// NOTE that the array index must be created with a length >= n
410/// before calling this function.
411/// NOTE also that the size type for n must be the same type used for the index array
412/// (templated type Index)
413template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
414{
415 for(Index i = 0; i < n; i++) { index[i] = i; }
416 if ( down )
417 std::sort(index, index + n, CompareDesc<const Element*>(a) );
418 else
419 std::sort(index, index + n, CompareAsc<const Element*>(a) );
420}
421
422#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
Basic types used by ROOT and required by TInterpreter.
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short)
Definition RtypesCore.h:54
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long double LongDouble_t
Long Double (not portable)
Definition RtypesCore.h:75
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t cindex
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
#define T2
Definition md5.inl:147
#define T1
Definition md5.inl:146
TMath.
Definition TMathBase.h:35
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE)
Sort the n1 elements of the Short_t array defined by its iterators.
Definition TMathBase.h:388
Long_t NextPrime(Long_t x)
T1 Sign(T1 a, T2 b)
Returns a value with the magnitude of a and the sign of b.
Definition TMathBase.h:174
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Returns x if lb < x < up, lb if x < lb and ub if x > ub.
Definition TMathBase.h:301
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197
Bool_t Odd(Long_t a)
Returns true if a is odd.
Definition TMathBase.h:116
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:413
Bool_t SignBit(Integer a)
Returns whether the sign of Integer a is negative.
Definition TMathBase.h:154
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:329
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122
Bool_t Even(Long_t a)
Returns true if a is even.
Definition TMathBase.h:112
bool operator()(Index i1, Index i2)
Definition TMathBase.h:373
CompareAsc(T d)
Definition TMathBase.h:370
bool operator()(Index i1, Index i2)
Definition TMathBase.h:360
CompareDesc(T d)
Definition TMathBase.h:357