Overture  Version 25
mathutil.h
Go to the documentation of this file.
1 // Math utilities
2 #ifndef MATHUTIL_H
3 #define MATHUTIL_H "Mathutil.h"
4 
5 #define SQR(x) ((x)*(x))
6 //#define SQRT(x) pow((x),.5) // *wdh* 061013
7 #define SQRT(x) sqrt((x))
8 
9 #ifndef NO_APP
10 #include "OvertureTypes.h" // define GUITypes::real to be float or double
11 #endif
12 
13 inline int max(int x1,int x2 ){ return x1>x2 ? x1 : x2; }
14 inline int max(int x1, unsigned int x2) { return (unsigned int)x1>x2 ? x1 : x2; }
15 inline int max(unsigned int x1, int x2) { return x1>(unsigned int)x2 ? x1 : x2; }
16 
17 // these next are needed on the dec machines
18 inline int max(unsigned long x1, int x2) { return x1>x2 ? x1 : x2; }
19 inline int max(int x1, unsigned long x2) { return x1>x2 ? x1 : x2; }
20 
21 inline double max(int x1,double x2 ){ return x1>x2 ? x1 : x2; }
22 inline double max(double x1,int x2 ){ return x1>x2 ? x1 : x2; }
23 inline float max(int x1,float x2 ){ return x1>x2 ? x1 : x2; }
24 inline float max(float x1,int x2 ){ return x1>x2 ? x1 : x2; }
25 
26 inline float max(float x1,float x2 ){ return x1>x2 ? x1 : x2; }
27 
28 inline double max(double x1,double x2 ){ return x1>x2 ? x1 : x2; }
29 
30 inline double max(float x1,double x2 ){ return x1>x2 ? double(x1) : x2; }
31 
32 inline double max(double x1,float x2 ){ return x1>x2 ? x1 : double(x2); }
33 
34 // ifndef USE_PPP *no longer needed* 010117
35 inline int min(int x1,int x2 ){ return x1<x2 ? x1 : x2; }
36 // endif
37 
38 inline float min(float x1,float x2 ){ return x1<x2 ? x1 : x2; }
39 
40 inline double min(double x1,double x2 ){ return x1<x2 ? x1 : x2; }
41 
42 inline double min(float x1,double x2 ){ return x1<x2 ? double(x1) : x2; }
43 
44 inline double min(double x1,float x2 ){ return x1<x2 ? x1 : double(x2); }
45 
46 // ************************************************
47 // **** here are max/min for multiple arguments ***
48 // ************************************************
49 inline int max(int x1,int x2,int x3 ){ return max(x1,max(x2,x3)); }
50 inline int max(int x1,int x2,int x3,int x4 ){ return max(x1,max(x2,x3,x4));}
51 inline int max(int x1,int x2,int x3,int x4,int x5 )
52  { return max(x1,max(x2,x3,x4,x5));}
53 
54 inline float max(float x1,float x2,float x3 ){ return max(x1,max(x2,x3)); }
55 inline float max(float x1,float x2,float x3,float x4 )
56  { return max(x1,max(x2,x3,x4));}
57 inline float max(float x1,float x2,float x3,float x4,float x5 )
58  { return max(x1,max(x2,x3,x4,x5));}
59 
60 inline double max(double x1,double x2,double x3 ){ return max(x1,max(x2,x3)); }
61 inline double max(double x1,double x2,double x3,double x4 )
62  { return max(x1,max(x2,x3,x4));}
63 inline double max(double x1,double x2,double x3,double x4,double x5 )
64  { return max(x1,max(x2,x3,x4,x5));}
65 
66 inline int min(int x1,int x2,int x3 ){ return min(x1,min(x2,x3)); }
67 inline int min(int x1,int x2,int x3,int x4 ){ return min(x1,min(x2,x3,x4));}
68 inline int min(int x1,int x2,int x3,int x4,int x5 )
69  { return min(x1,min(x2,x3,x4,x5));}
70 
71 inline float min(float x1,float x2,float x3 ){ return min(x1,min(x2,x3)); }
72 inline float min(float x1,float x2,float x3,float x4 )
73  { return min(x1,min(x2,x3,x4));}
74 inline float min(float x1,float x2,float x3,float x4,float x5 )
75  { return min(x1,min(x2,x3,x4,x5));}
76 
77 inline double min(double x1,double x2,double x3 ){ return min(x1,min(x2,x3)); }
78 inline double min(double x1,double x2,double x3,double x4 )
79  { return min(x1,min(x2,x3,x4));}
80 inline double min(double x1,double x2,double x3,double x4,double x5 )
81  { return min(x1,min(x2,x3,x4,x5));}
82 
83 
84 // new gcc also defines round
85 inline int rounder(double x ){ return x>0 ? int(x+.5) : int(x-.5); } // round to nearest integer
86 
87 // these are needed by the dec compiler
88 
89 #if 0
90 
91 #ifdef __GNUC__
92 inline float pow(float x1,int x2 ){ return x2==0 ? 1 : pow(double(x1),double(x2)); }
93 inline double pow(double x1,int x2 ){ return x2==0 ? 1 : pow(x1,double(x2)); }
94 #endif
95 
96 #ifndef __GNUC__
97 
98 #ifdef OV_USE_DOUBLE
99 inline float pow(float &x1,int x2 ){ return x2==0 ? 1 : pow(double(x1),double(x2)); }
100 #else
101 inline double pow(double &x1,int x2 ){ return x2==0 ? 1 : pow(x1,double(x2)); }
102 #endif
103 
104 inline double pow(real &x1,int x2) { return x2==0 ? 1 : pow(x1,real(x2)); }
105 
106 inline float pow(const float &x1,int x2 ){ return x2==0 ? 1 : pow(double(x1),double(x2)); }
107 inline double pow(const double &x1,int x2 ){ return x2==0 ? 1 : pow(x1,double(x2)); }
108 #ifndef __hpux
109 #endif
110 inline double pow(float x1,double x2 ){ return pow(double(x1),x2); }
111 inline double pow(double x1,float x2 ){ return pow(x1,double(x2)); }
112 inline float pow(float x1,float x2 ){ return pow(double(x1),double(x2)); }
113 inline double pow(double x1,int x2 ){ return x2==0 ? 1 : pow(x1,double(x2)); }
114 
115 inline double pow(int x1,int x2 ){ return x2>0 ? rounder(pow(double(x1),double(x2))) : pow(double(x1),double(x2)); }
116 #else
117 inline double pow(int x1,int x2 ){ return x2>0 ? rounder(pow(double(x1),double(x2))) : pow(double(x1),double(x2)); }
118 #endif
119 #endif
120 
121 #include <cmath>
122 using std::pow;
123 
124 inline double pow(int x1,int x2 ){ return x2>0 ? rounder(pow(double(x1),double(x2))) : pow(double(x1),double(x2)); }
125 
126 #ifdef __GNUC__
127 inline double sqrt(int x) { return sqrt(double(x)); }
128 #endif
129 
130 #ifndef __KCC
131 inline float atan2( float x1, float x2 ){ return atan2(double(x1),double(x2)); }
132 #endif
133 inline float atan2(double x1, float x2 ){ return atan2(double(x1),double(x2)); }
134 inline float atan2( float x1,double x2 ){ return atan2(double(x1),double(x2)); }
135 inline float fmod( float x1, float x2 ){ return fmod(double(x1),double(x2)); }
136 inline float fmod(double x1, float x2 ){ return fmod(double(x1),double(x2)); }
137 inline float fmod( float x1,double x2 ){ return fmod(double(x1),double(x2)); }
138 
139 inline int sign(int x ) { return x>0 ? 1 : ( x<0 ? -1 : 0);}
140 inline int sign(float x ) { return x>0 ? 1 : ( x<0 ? -1 : 0);}
141 inline int sign(double x ) { return x>0 ? 1 : ( x<0 ? -1 : 0);}
142 
143 // 020410 : wdh these don't seem to be needed with g++
144 #ifndef __GNUC__
145 inline double log10( int x ){ return log10((double)x); }
146 inline double log( int x ){ return log((double)x); }
147 #endif
148 
149 inline double fabs( int x ){ return fabs((double)x); }
150 
151 int inline
152 floorDiv(int numer, int denom )
153 // return the floor( "numer/denom" ) (ie. always chop to the left).
154 // Assumes denom>0
155 //
156 // floorDiv( 3,2) = 1 same as 3/2
157 // floorDiv( -3,2 ) =-2 **note** not the same as (-3)/2 = -1
158 {
159  if( numer>0 )
160  return numer/denom;
161  else
162  return (numer-denom+1)/denom;
163 }
164 
165 #ifndef NO_APP
166 #define ARRAY_GET_DATA_POINTER(type,aType) \
167 inline type* getDataPointer(const aType ## Array & u) \
168 { \
169  return u.Array_Descriptor.Array_View_Pointer3+ \
170  u.getBase(0)+u.getRawDataSize(0)*( \
171  u.getBase(1)+u.getRawDataSize(1)*( \
172  u.getBase(2)+u.getRawDataSize(2)*(u.getBase(3)))); \
173 }
174 
175 // return the data pointer to an array for apasing to Fortran (works for views too)
176 // Only works for up to 4D arrays.
177 ARRAY_GET_DATA_POINTER(int,int)
178 ARRAY_GET_DATA_POINTER(float,float)
179 ARRAY_GET_DATA_POINTER(double,double)
180 
181 #ifdef USE_PPP
182 ARRAY_GET_DATA_POINTER(int,intSerial)
183 ARRAY_GET_DATA_POINTER(float,floatSerial)
184 ARRAY_GET_DATA_POINTER(double,doubleSerial)
185 #endif
186 
187 #undef ARRAY_GET_DATA_POINTER
188 #endif // ifndef NO_APP
189 
190 #endif // MATHUTIL_H