Garfield++ v1r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
minmax.h
Go to the documentation of this file.
1#ifndef MINMAX_H
2#define MINMAX_H
3/*
4Copyright (c) 2001 Igor Borisovich Smirnov
5
6Permission to use, copy, modify, distribute and sell this file
7and its documentation for any purpose is hereby granted without fee,
8provided that the above copyright notice, this permission notice,
9and notices about any modifications of the original text
10appear in all copies and in supporting documentation.
11This software is distributed in the hope that it will be useful,
12but the author makes no representations about the suitability
13of this software for any particular purpose.
14It is provided "as is" without express or implied warranty.
15*/
16
17//#ifndef DBL_MAX // in Solaris they are in limits.h
18//#define DBL_MAX MAXDOUBLE
19//#endif
20//#ifndef DBL_MIN //
21//#define DBL_MIN MINDOUBLE
22//#endif
23
24#ifdef VISUAL_STUDIO
25#define _USE_MATH_DEFINES
26// see comment in math.h:
27/* Define _USE_MATH_DEFINES before including math.h to expose these macro
28 * definitions for common math constants. These are placed under an #ifdef
29 * since these commonly-defined names are not part of the C/C++ standards.
30 */
31#endif
32#include <cmath>
33#include <cfloat>
36
37// Minimal and maximal power of e, whose result can be represented.
38// Similar numbers for a != e can be obtained in the user's program by
39// double dbl_max_a_exp = dbl_max_e_exp / log(a);
40
41static const double dbl_min_e_exp = (DBL_MIN_EXP - 1) * log(double(FLT_RADIX));
42static const double dbl_max_e_exp = (DBL_MAX_EXP - 1) * log(double(FLT_RADIX));
43static const double dbl_max_square = sqrt(DBL_MAX); // maximal number,
44// which square can be represented.
45static const double dbl_max_pow3 = pow(DBL_MAX, 1.0 / 3.0); // maximal number,
46// whose power 3 can be represented.
47
48static const double flt_min_e_exp = (FLT_MIN_EXP - 1) * log(double(FLT_RADIX));
49static const double flt_max_e_exp = (FLT_MAX_EXP - 1) * log(double(FLT_RADIX));
50static const double flt_max_square = sqrt(FLT_MAX); // maximal number,
51 // which square can be represented.
52static const double flt_max_pow3 =
53 pow(FLT_MAX, float(1.0 / 3.0)); // maximal number,
54 // whose power 3 can be represented.
55
56//There is a widely used macros min and max making
57//the program calculating the expresion twice.
58//It is dangerous to define them again, since they can be in include files.
59//It looks like no macro with name fmin, fmax.
60//Note that there is double function fabs and int function(or macro?) abs.
61//Note, the fabs is declared through math.h ( which need to include),
62// but with the use of other headers also.
63// It is useless to search it through grep, since
64// the name fabs is generated via complicated macros.
65
66/*
678.11.2005:
68Now fmin and fmax are also found in new version g++ 3.2 and 3.2.3
69in file /usr/include/bits/mathcalls.h
70
71I am now not sure that they are common (there is no book under hand)
72and that int and long versions are also available.
73So I will change fmin and fmax to names probably more original
74find_min and find_max.
75By the way the names fmin were similar to notation of function parameters
76in these programs.
77This is other reason to get rid of them.
78
79
80//inline double fmin( double a, double b) { return (a<b ? a : b ); }
81//inline double fmax( double a, double b) { return (a>b ? a : b ); }
82//inline float fmin( float a, float b) { return (a<b ? a : b ); }
83//inline float fmax( float a, float b) { return (a>b ? a : b ); }
84//inline long fmin( long a, long b) { return (a<b ? a : b ); }
85//inline long fmax( long a, long b) { return (a>b ? a : b ); }
86//inline int fmin( int a, int b) { return (a<b ? a : b ); }
87//inline int fmax( int a, int b) { return (a>b ? a : b ); }
88*/
89
90inline double find_min(double a, double b) { return (a < b ? a : b); }
91inline double find_max(double a, double b) { return (a > b ? a : b); }
92inline float find_min(float a, float b) { return (a < b ? a : b); }
93inline float find_max(float a, float b) { return (a > b ? a : b); }
94inline long find_min(long a, long b) { return (a < b ? a : b); }
95inline long find_max(long a, long b) { return (a > b ? a : b); }
96inline int find_min(int a, int b) { return (a < b ? a : b); }
97inline int find_max(int a, int b) { return (a > b ? a : b); }
98
99inline long left_round(double f) {
100 if (f >= 0)
101 return long(f);
102 else
103 return -long(-f) - 1;
104}
105inline int even_num(long n) {
106 long v = n / 2;
107 long n1 = v * 2;
108 if (n == n1)
109 return 1;
110 else
111 return 0;
112}
113
114inline int check_value_inside_bounds(double val, double b1, double b2) {
115 return ((val > b1 && val < b2) ? 1 : 0);
116}
117inline int check_value_inside_bounds(float val, float b1, float b2) {
118 return ((val > b1 && val < b2) ? 1 : 0);
119}
120inline int check_value_inside_bounds(long val, long b1, long b2) {
121 return ((val > b1 && val < b2) ? 1 : 0);
122}
123inline int check_value_inside_bounds(int val, int b1, int b2) {
124 return ((val > b1 && val < b2) ? 1 : 0);
125}
126
127inline int check_value_in_bounds(double val, double b1, double b2) {
128 return ((val >= b1 && val <= b2) ? 1 : 0);
129}
130inline int check_value_in_bounds(float val, float b1, float b2) {
131 return ((val >= b1 && val <= b2) ? 1 : 0);
132}
133inline int check_value_in_bounds(long val, long b1, long b2) {
134 return ((val >= b1 && val <= b2) ? 1 : 0);
135}
136inline int check_value_in_bounds(int val, int b1, int b2) {
137 return ((val >= b1 && val <= b2) ? 1 : 0);
138}
139
140inline int check_value_inside_bounds(double val, double b[2]) {
141 return ((val > b[0] && val < b[1]) ? 1 : 0);
142}
143inline int check_value_inside_bounds(float val, float b[2]) {
144 return ((val > b[0] && val < b[1]) ? 1 : 0);
145}
146inline int check_value_inside_bounds(long val, long b[2]) {
147 return ((val > b[0] && val < b[1]) ? 1 : 0);
148}
149inline int check_value_inside_bounds(int val, int b[2]) {
150 return ((val > b[0] && val < b[1]) ? 1 : 0);
151}
152
153inline int check_value_in_bounds(double val, double b[2]) {
154 return ((val >= b[0] && val <= b[1]) ? 1 : 0);
155}
156inline int check_value_in_bounds(float val, float b[2]) {
157 return ((val >= b[0] && val <= b[1]) ? 1 : 0);
158}
159inline int check_value_in_bounds(long val, long b[2]) {
160 return ((val >= b[0] && val <= b[1]) ? 1 : 0);
161}
162inline int check_value_in_bounds(int val, int b[2]) {
163 return ((val >= b[0] && val <= b[1]) ? 1 : 0);
164}
165
166//inline lmin( long a, long b) { return (a<b ? a : b ); }
167//inline lmax( long a, long b) { return (a>b ? a : b ); }
168/*
169template<class T*>
170inline void fcopy(T ar_source, T ar_dest, long q)
171{
172 long n=0;
173 for( n=0; n<q; n++) ar_dest[n] = ar_source[n];
174}
175*/
176template <class T> inline void fcopy(T ar_source, T ar_dest, long q) {
177 for (long n = 0; n < q; ++n)
178 ar_dest[n] = ar_source[n];
179}
180
181template <class T>
182inline void arr_copy(const T* ar_source, T* ar_dest, long q) {
183 for (long n = 0; n < q; ++n)
184 ar_dest[n] = ar_source[n];
185}
186
187template <class T> inline void arr_assign(const T source, T* ar_dest, long q) {
188 for (long n = 0; n < q; ++n)
189 ar_dest[n] = source;
190}
191
192template <class T> inline long fmax_ar(T ar, long q) {
193 if (q <= 0) {
194 mcerr << "inline long fmax_ar(T ar, long q): q<=0\n";
195 spexit(mcerr);
196 }
197 long nmval = 0;
198 for (long n = 1; n < q; ++n) {
199 if (ar[n] > ar[nmval]) nmval = n;
200 }
201 return nmval;
202}
203
204template <class T> inline T tabs(const T& x) { return x >= 0 ? x : -x; }
205
206template <class T> int apeq_mant(const T& x1, const T& x2, T prec) {
207 if (x1 == x2) return 1;
208 if (prec == 0) return 0;
209 if (x1 == 0 && x2 == 0) return 1;
210 if ((x1 < 0 && x2 > 0) || (x1 > 0 && x2 < 0)) return 0;
211 if (tabs((x1 - x2) / (x1 + x2)) <= prec) return 1;
212 return 0;
213}
214
215#endif
DoubleAc pow(const DoubleAc &f, double p)
Definition: DoubleAc.cpp:336
DoubleAc sqrt(const DoubleAc &f)
Definition: DoubleAc.cpp:313
#define spexit(stream)
Definition: FunNameStack.h:536
int even_num(long n)
Definition: minmax.h:105
double find_min(double a, double b)
Definition: minmax.h:90
long left_round(double f)
Definition: minmax.h:99
int check_value_in_bounds(double val, double b1, double b2)
Definition: minmax.h:127
int apeq_mant(const T &x1, const T &x2, T prec)
Definition: minmax.h:206
int check_value_inside_bounds(double val, double b1, double b2)
Definition: minmax.h:114
long fmax_ar(T ar, long q)
Definition: minmax.h:192
void arr_copy(const T *ar_source, T *ar_dest, long q)
Definition: minmax.h:182
T tabs(const T &x)
Definition: minmax.h:204
double find_max(double a, double b)
Definition: minmax.h:91
void arr_assign(const T source, T *ar_dest, long q)
Definition: minmax.h:187
void fcopy(T ar_source, T ar_dest, long q)
Definition: minmax.h:176
#define mcerr
Definition: prstream.h:135