acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
mathematics.h
1#pragma once
2
3
4
5template < typename UNIT_TYPE, prototype_number NUMBER >
6inline UNIT_TYPE linear_rate(const UNIT_TYPE & t1, const UNIT_TYPE & t2, NUMBER numerator, NUMBER denominator)
7{
8
9 if (t2 == t1)
10 {
11
12 return t1;
13
14 }
15
16 return (UNIT_TYPE)(((::f64)(t2 - t1) * (::f64)numerator / (::f64)denominator) + (::f64)t1);
17
18}
19
20
21
22namespace mathematics
23{
24
25 struct math_os_data;
26
27 class CLASS_DECL_ACME mathematics :
28 virtual public ::matter
29 {
30 private:
31
32
33 static ::f64 s_π;
34 static ::f32 s_fπ;
35 ::u32 dwMode;
36 ::u8 pbData[16];
37
38 public:
39
40
41 ::pointer < math_os_data > m_posdata;
42
43
44 mathematics();
45 virtual ~mathematics();
46
47
48 virtual void initialize(::particle * pparticle) override;
49 virtual void destroy() override;
50
51
52 ::i32 LinearMap(::i32 iMin, ::i32 iMax, ::i32 iValue, ::i32 iValueMin, ::i32 iValueMax);
53
54 ::f64 LinearMap(::f64 dMin, ::f64 dMax, ::f64 dValue, ::f64 dValueMin, ::f64 dValueMax);
55 static inline ::f64 π() { return s_π; }
56 static inline ::f32 πf() { return s_fπ; }
57
58 static bool IsPowerOfTwo(::u64 uiValue);
59 static ::u32 ReverseBits(::u32 index, ::u32 NumBits);
60 static inline void MaxClip(::f64 * pnClipValue, ::f64 nMaxValue);
61 static inline void MinClip(::f64 * pnClipValue, ::f64 nMinValue);
62
63 ::i32 RangeRate(::i32 iMin, ::i32 iMax, ::f64 dRate);
64
65
66
67
68 template < typename T1, typename T2 >
69 inline bool clip_convert(T1 & t1, const T2 & t2)
70 {
71
72 if (::comparison::gt(t2, ::numeric_info < T1 >::get_maximum_value()))
73 {
74
75 t1 = ::numeric_info < T1 >::get_maximum_value();
76
77 informationf("----> clip::assign maximum clipped");
78
79 return false;
80
81 }
82
83 if (::comparison::lt(t2, ::numeric_info < T1 >::get_minimum_value()))
84 {
85
86 t1 = ::numeric_info < T1 >::get_minimum_value();
87
88 informationf("----> clip::assign minimum clipped");
89
90 return false;
91
92 }
93
94 t1 = (T1)t2;
95
96 return true;
97
98 }
99
100
101 template < typename T1, typename T2 >
102 inline T1 clip_convert(const T2 & t2)
103 {
104
105 if (::comparison::gt(t2, ::numeric_info < T1 >::get_maximum_value()))
106 {
107
108 informationf("----> clip::convert maximum clipped");
109
110 return ::numeric_info < T1 >::get_maximum_value();
111
112 }
113
114 if (::comparison::lt(t2, ::numeric_info < T1 >::get_minimum_value()))
115 {
116
117 informationf("----> clip::convert minimum clipped");
118
119 return ::numeric_info < T1 >::get_minimum_value();
120
121 }
122
123 return (T1)t2;
124
125 }
126
127
128 template < typename T1, typename T2 >
129 inline bool clip_convert_add(T1 & t1, const T2 & t2)
130 {
131
132 auto t = t1 + t2;
133
134 if (!convert(t1, t))
135 {
136
137 informationf(" (convert_add)");
138
139 }
140
141 return true;
142
143 }
144
145
146 template < typename T1, typename T2 >
147 inline bool clip_convert_difference(T1 & t1, const T2 & t2)
148 {
149
150 auto t = t1 - t2;
151
152 return clip_convert(t1, t);
153
154 }
155
156
157 template < typename T1, typename T2 >
158 inline bool clip_convert_multiply(T1 & t1, const T2 & t2)
159 {
160
161 auto t = t2 * t1;
162
163 if (!clip_convert(t1, t))
164 {
165
166 informationf(" (convert_multiply)");
167
168 }
169
170 return true;
171
172 }
173
174
175 // division by zero protection isn't this function's current responsability and maybe never should be
176 template < typename T1, typename T2 >
177 inline bool clip_convert_divide(T1 & t1, const T2 & t2)
178 {
179
180 auto t = t1 / t2;
181
182 if (!clip_convert(t1, t))
183 {
184
185 informationf(" (convert_divide)");
186
187 }
188
189 return true;
190
191 }
192
193
194
195 //CLASS_DECL_ACME void generate_random_bytes(void* p, memsize s);
196 void transform_alphanumeric(void * p, memsize s);
197 //void generate_random_alphanumeric(void * p, memsize s);
198
199 //template < typename TYPE >
200 //inline TYPE * __random_bytes(TYPE * p, memsize s);
201
202 //template < typename TYPE >
203 //TYPE & random(TYPE & t);
204
205 //template < typename TYPE >
206 //TYPE random();
207
208
209 //::f32 random(::f32 f1, ::f32 f2);
210 //::f64 random(::f64 d1, ::f64 d2);
211 //::i64 random(::i64 i1, ::i64 i2);
212 //::i32 random(::i32 i1, ::i32 i2);
213 //template < typename ::i32 >
214 //::i32 __random_int(::i32 i1, ::i32 i2);
215
216
217 //#include "acme/prototype/prototype/matter.h"
218 void random(const block & block);
219 //::u64 gen_rand();
220//void random_bytes(void * buf, memsize dwLen);
221//void RandomBytes(void * buf, memsize dwLen);
222//void gen_rand(void * buf, memsize dwLen);
223
224 void random_alphanumeric(char_pointer buf, memsize dwLen);
225 //string gen_rand_alnum(character_count s);
226 string random_alphanumeric(character_count s);
227
228 ::i8 random_char();
229 ::u8 random_u8();
230 ::u32 random_ui();
231 ::u64 random_u64();
232
233 ::i32 rand_max();
234
235
236 //template < typename TYPE >
237 //inline TYPE * __random_bytes(TYPE * p, memsize s)
238 //{
239 // generate_random_bytes(p, s);
240 // return p;
241 //}
242
243
244 //template < typename TYPE >
245 //inline TYPE & random(TYPE & t)
246 //{
247
248 // return *__random_bytes(&t, sizeof(TYPE));
249
250 //}
251
252
253 template < typename TYPE >
254 inline TYPE random()
255 {
256
257 TYPE t;
258
259 random({ e_as_block, t });
260
261 return t;
262
263 }
264
265
266
267 template < typename UNIT_TYPE >
268 inline UNIT_TYPE minimum_maximum_rand(const UNIT_TYPE & t1, const UNIT_TYPE & t2)
269 {
270
271 return linear_rate(t1, t2, ::rand(), RAND_MAX);
272
273 }
274
275
276
277
278 template < prototype_floating FLOATING1, prototype_floating FLOATING2 >
279 inline ::largest_type < FLOATING1, FLOATING2 > random(const FLOATING1 & f1, const FLOATING2 & f2)
280 {
281
282 if (f1 == f2) return f1;
283
284 ::largest_type < FLOATING1, FLOATING2 > fMin = minimum(f1, f2);
285
286 ::largest_type < FLOATING1, FLOATING2 > fMax = maximum(f1, f2);
287
288 ::largest_type < FLOATING1, FLOATING2 > fRange = fMax - fMin;
289
290 ::u64 u = 0;
291
292 random(u);
293
294 return (::largest_type < FLOATING1, FLOATING2 >) (((u * fRange) / (::largest_type < FLOATING1, FLOATING2 >)U64_MAXIMUM) + fMin);
295
296 }
297
298
299
300
301 template < prototype_integral INTEGRAL1, prototype_integral INTEGRAL2 >
302 inline ::largest_type < INTEGRAL1, INTEGRAL2 > random(const INTEGRAL1 & i1, const INTEGRAL2 & i2)
303 {
304
305 if (i1 == i2) return i1;
306
307 ::largest_type < INTEGRAL1, INTEGRAL2 > iMin = minimum(i1, i2);
308
309 ::largest_type < INTEGRAL1, INTEGRAL2 > iMax = maximum(i1, i2);
310
311 ::largest_type < INTEGRAL1, INTEGRAL2 > iRange = iMax - iMin;
312
313 auto u = random_u64();
314
315 return(::largest_type < INTEGRAL1, INTEGRAL2 >) ((u % (iRange + 1)) + iMin);
316
317 }
318
319
320
321
322
323 };
324
325
326 inline void mathematics::MaxClip(::f64 * pnClipValue, ::f64 nMaxValue)
327 {
328
329 if (*pnClipValue > nMaxValue)
330 {
331
332 *pnClipValue = nMaxValue;
333
334 }
335
336 }
337
338
339 inline void mathematics::MinClip(::f64 * pnClipValue, ::f64 nMinValue)
340 {
341
342 if (*pnClipValue < nMinValue)
343 {
344
345 *pnClipValue = nMinValue;
346
347 }
348
349 }
350
351
352 //CLASS_DECL_ACME class mathematics * mathematics();
353
354
355} // namespace mathematics
356
357
Definition block.h:46
Definition aaa_department.cpp:19