Library of Assembled Shared Sources
floating_point_consistency.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2022 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_FLOATING_POINT_CONSISTENCY_H
45#define LASS_GUARDIAN_OF_INCLUSION_NUM_FLOATING_POINT_CONSISTENCY_H
46
47#include "num_common.h"
48#include "../util/call_traits.h"
49#include <complex>
50
51namespace lass
52{
53namespace num
54{
55
56template <typename T>
57class Consistent
58{
59public:
60 typedef Consistent<T> TSelf;
61 typedef typename util::CallTraits<T>::TValue TValue;
62 typedef typename util::CallTraits<T>::TParam TParam;
63 typedef typename util::CallTraits<T>::TReference TReference;
64 typedef typename util::CallTraits<T>::TConstReference TConstReference;
65 typedef num::NumTraits<T> TNumTraits;
66
67 Consistent() : t_()
68 {
69 }
70
71 Consistent(TParam t):
72 t_(t)
73 {
74 }
75
76 Consistent(const TSelf& other):
77 t_(other.t_)
78 {
79 }
80
81 TSelf& operator=(const TSelf& other)
82 {
83 t_ = other.t_;
84 return *this;
85 }
86
87 TParam value() const
88 {
89 return t_;
90 }
91
92 TSelf operator-() const
93 {
94 return TSelf(-t_);
95 }
96
97 TSelf operator+() const
98 {
99 return *this;
100 }
101
102 TSelf& operator+=(const TSelf& other)
103 {
104 t_ = t_ + other.t_;
105 return *this;
106 }
107 TSelf& operator-=(const TSelf& other)
108 {
109 t_ = t_ - other.t_;
110 return *this;
111 }
112 TSelf& operator*=(const TSelf& other)
113 {
114 t_ = t_ * other.t_;
115 return *this;
116 }
117 TSelf& operator/=(const TSelf& other)
118 {
119 t_ = t_ / other.t_;
120 return *this;
121 }
122
123 void swap(TSelf& other)
124 {
125 std::swap(t_, other.t_);
126 }
127
128private:
129 volatile TValue t_;
130};
131
132
133
134template <typename T> inline
135Consistent<T> operator+(const Consistent<T>& a, const Consistent<T>& b)
136{
137 return Consistent<T>(a.value() + b.value());
138}
139
140template <typename T> inline
141Consistent<T> operator-(const Consistent<T>& a, const Consistent<T>& b)
142{
143 return Consistent<T>(a.value() - b.value());
144}
145
146template <typename T> inline
147Consistent<T> operator*(const Consistent<T>& a, const Consistent<T>& b)
148{
149 return Consistent<T>(a.value() * b.value());
150}
151
152template <typename T> inline
153Consistent<T> operator/(const Consistent<T>& a, const Consistent<T>& b)
154{
155 return Consistent<T>(a.value() / b.value());
156}
157
158template <typename T> inline
159bool operator==(const Consistent<T>& a, const Consistent<T>& b)
160{
161 return a.value() == b.value();
162}
163
164template <typename T> inline
165bool operator!=(const Consistent<T>& a, const Consistent<T>& b)
166{
167 return !(a == b);
168}
169
170template <typename T> inline
171bool operator<(const Consistent<T>& a, const Consistent<T>& b)
172{
173 return a.value() < b.value();
174}
175
176template <typename T> inline
177bool operator>(const Consistent<T>& a, const Consistent<T>& b)
178{
179 return b < a;
180}
181
182template <typename T> inline
183bool operator<=(const Consistent<T>& a, const Consistent<T>& b)
184{
185 return !(b < a);
186}
187
188template <typename T> inline
189bool operator>=(const Consistent<T>& a, const Consistent<T>& b)
190{
191 return !(a < b);
192}
193
194
195
196
197template <typename T> inline
198Consistent<T> operator+(const Consistent<T>& a, const T& b)
199{
200 return Consistent<T>(a.value() + b);
201}
202
203template <typename T> inline
204Consistent<T> operator-(const Consistent<T>& a, const T& b)
205{
206 return Consistent<T>(a.value() - b);
207}
208
209template <typename T> inline
210Consistent<T> operator*(const Consistent<T>& a, const T& b)
211{
212 return Consistent<T>(a.value() * b);
213}
214
215template <typename T> inline
216Consistent<T> operator/(const Consistent<T>& a, const T& b)
217{
218 return Consistent<T>(a.value() / b);
219}
220
221template <typename T> inline
222bool operator==(const Consistent<T>& a, const T& b)
223{
224 return a == Consistent<T>(b);
225}
226
227template <typename T> inline
228bool operator!=(const Consistent<T>& a, const T& b)
229{
230 return !(a == b);
231}
232
233template <typename T> inline
234bool operator<(const Consistent<T>& a, const T& b)
235{
236 return a < Consistent<T>(b);
237}
238
239template <typename T> inline
240bool operator>(const Consistent<T>& a, const T& b)
241{
242 return b < a;
243}
244
245template <typename T> inline
246bool operator<=(const Consistent<T>& a, const T& b)
247{
248 return !(b < a);
249}
250
251template <typename T> inline
252bool operator>=(const Consistent<T>& a, const T& b)
253{
254 return !(a < b);
255}
256
257
258
259
260template <typename T> inline
261Consistent<T> operator+(const T& a, const Consistent<T>& b)
262{
263 return Consistent<T>(a + b.value());
264}
265
266template <typename T> inline
267Consistent<T> operator-(const T& a, const Consistent<T>& b)
268{
269 return Consistent<T>(a - b.value());
270}
271
272template <typename T> inline
273Consistent<T> operator*(const T& a, const Consistent<T>& b)
274{
275 return Consistent<T>(a * b.value());
276}
277
278template <typename T> inline
279Consistent<T> operator/(const T& a, const Consistent<T>& b)
280{
281 return Consistent<T>(a / b.value());
282}
283
284template <typename T> inline
285bool operator==(const T& a, const Consistent<T>& b)
286{
287 return Consistent<T>(a) == b;
288}
289
290template <typename T> inline
291bool operator!=(const T& a, const Consistent<T>& b)
292{
293 return !(a == b);
294}
295
296template <typename T> inline
297bool operator<(const T& a, const Consistent<T>& b)
298{
299 return Consistent<T>(a) < b;
300}
301
302template <typename T> inline
303bool operator>(const T& a, const Consistent<T>& b)
304{
305 return b < a;
306}
307
308template <typename T> inline
309bool operator<=(const T& a, const Consistent<T>& b)
310{
311 return !(b < a);
312}
313
314template <typename T> inline
315bool operator>=(const T& a, const Consistent<T>& b)
316{
317 return !(a < b);
318}
319
320
321
322
323
324template <typename T> inline
325Consistent<T> abs(const Consistent<T>& v)
326{
327 return num::abs(v.value());
328}
329
330template <typename T> inline
331Consistent<T> inv(const Consistent<T>& v)
332{
333 return num::inv(v.value());
334}
335/*
336template <typename T> inline
337Consistent<T> sqrt(const Consistent<T>& v)
338{
339 return num::sqrt(v.value());
340}
341*/
342template <typename T> inline
343Consistent<T> sqrt(Consistent<T> v)
344{
345 return num::sqrt(v.value());
346}
347
348template <typename T> inline
349Consistent<T> pow(const Consistent<T>& v, const Consistent<T>& p)
350{
351 return num::pow(v.value(), p.value());
352}
353
354template <typename T> inline
355Consistent<T> exp(const Consistent<T>& v)
356{
357 return num::exp(v.value());
358}
359
360template <typename T> inline
361Consistent<T> log(const Consistent<T>& v)
362{
363 return num::log(v.value());
364}
365
366template <typename T> inline
367Consistent<T> cos(const Consistent<T>& v)
368{
369 return num::cos(v.value());
370}
371
372template <typename T> inline
373Consistent<T> sin(const Consistent<T>& v)
374{
375 return num::sin(v.value());
376}
377
378template <typename T> inline
379Consistent<T> tan(const Consistent<T>& v)
380{
381 return num::tan(v.value());
382}
383
384template <typename T> inline
385Consistent<T> acos(const Consistent<T>& v)
386{
387 return num::acos(v.value());
388}
389
390template <typename T> inline
391Consistent<T> asin(const Consistent<T>& v)
392{
393 return num::asin(v.value());
394}
395
396template <typename T> inline
397Consistent<T> atan(const Consistent<T>& v)
398{
399 return num::atan(v.value());
400}
401
402template <typename T> inline
403Consistent<T> atan2(const Consistent<T>& y, const Consistent<T>& x)
404{
405 return num::atan2(y.value(), x.value());
406}
407
408template <typename T> inline
409Consistent<T> floor(const Consistent<T>& v)
410{
411 return num::floor(v.value());
412}
413
414template <typename T> inline
415Consistent<T> ceil(const Consistent<T>& v)
416{
417 return num::ceil(v.value());
418}
419
420template <typename T> inline
421Consistent<T> round(const Consistent<T>& v)
422{
423 return num::round(v.value());
424}
425
426template <typename T> inline
427Consistent<T> fractional(const Consistent<T>& v)
428{
429 return num::fractional(v.value());
430}
431
432template <typename T> inline
433Consistent<T> div(const Consistent<T>& v, const Consistent<T>& m)
434{
435 return num::div(v.value(), m.value());
436}
437
438template <typename T> inline
439Consistent<T> mod(const Consistent<T>& v, const Consistent<T>& m)
440{
441 return num::mod(v.value(), m.value());
442}
443
444
445#define LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(type)\
446 template <>\
447 struct LASS_DLL NumTraits< Consistent< type > >\
448 {\
449 private:\
450 typedef NumTraits< type > TBaseTraits;\
451 public:\
452 typedef Consistent< type > selfType;\
453 typedef type baseType;\
454 typedef type intervalType;\
455 enum\
456 {\
457 isDistribution = TBaseTraits::isDistribution,\
458 isIntegral = TBaseTraits::isIntegral,\
459 isNative = TBaseTraits::isNative,\
460 isSigned = TBaseTraits::isSigned,\
461 hasInfinity = TBaseTraits::hasInfinity,\
462 hasNaN = TBaseTraits::hasNaN\
463 };\
464 static const int memorySize;\
465 static const std::string name() { return "Consistent<" + TBaseTraits::name() + ">" ; }\
466 static const selfType one;\
467 static const selfType zero;\
468 static const selfType sNaN;\
469 static const selfType qNaN;\
470 static const selfType epsilon;\
471 static const selfType infinity;\
472 static const selfType min;\
473 static const selfType max;\
474 static const selfType minStrictPositive;\
475 static const selfType pi;\
476 static const selfType e;\
477 static const selfType sqrt2;\
478 static const selfType sqrtPi;\
479 };
480
481LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(float)
482LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(double)
483LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(long double)
484
485template<class T> inline
486bool isNaN( const Consistent<T>& v )
487{
488 return num::isNaN(v.value());
489}
490
491template <typename T, typename Char, typename Traits>
492std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& s, const Consistent<T>& v)
493{
494 s << v.value();
495 return s;
496}
497
498template <typename T, typename Char, typename Traits>
499std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& s, Consistent<T>& v)
500{
501 T temp;
502 s >> temp;
503 v = Consistent<T>(temp);
504 return s;
505}
506
507}
508
509}
510
511namespace std
512{
513
514template <typename T>
515void swap(::lass::num::Consistent<T>& a, ::lass::num::Consistent<T>& b)
516{
517 a.swap(b);
518}
519
520}
521
522#endif
T inv(const T &x)
return x ^ -1
Definition basic_ops.h:178
T pow(const T &x, const T &p)
return exp(p * log(x));
Definition basic_ops.h:187
T abs(const T &x)
if x < 0 return -x, else return x.
Definition basic_ops.h:145
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53