itom
Loading...
Searching...
No Matches
numeric.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom and its software development toolkit (SDK).
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 In addition, as a special exception, the Institut für Technische
15 Optik (ITO) gives you certain additional rights.
16 These rights are described in the ITO LGPL Exception version 1.0,
17 which can be found in the file LGPL_EXCEPTION.txt in this package.
18
19 itom is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22 General Public Licence for more details.
23
24 You should have received a copy of the GNU Library General Public License
25 along with itom. If not, see <http://www.gnu.org/licenses/>.
26*********************************************************************** */
27
28#ifndef NUMERIC_H
29#define NUMERIC_H
30
31#include "typeDefs.h"
32#include "color.h"
33#include <limits>
34
35namespace ito
36{
38
47 template<typename _Tp> inline bool isNotZero(_Tp value)
48 {
49 return !(value == 0);
50 }
51
53 template<> inline bool isNotZero<float32>(float32 value)
54 {
55 if (fabs(value) < std::numeric_limits<float32>::epsilon())
56 return false;
57 else
58 return true;
59 }
60
62 template<> inline bool isNotZero<float64>(float64 value)
63 {
64 if (fabs(value) < std::numeric_limits<float64>::epsilon())
65 return false;
66 else
67 return true;
68 }
69
71 template<> inline bool isNotZero<complex64>(complex64 value)
72 {
73 if (fabs(value.real()) < std::numeric_limits<float32>::epsilon() &&
74 fabs(value.imag()) < std::numeric_limits<float32>::epsilon())
75 return false;
76 else
77 return true;
78 }
79
81 template<> inline bool isNotZero<complex128>(complex128 value)
82 {
83 if (fabs(value.real()) < std::numeric_limits<float64>::epsilon() &&
84 fabs(value.imag()) < std::numeric_limits<float64>::epsilon())
85 return false;
86 else
87 return true;
88 }
89
90
92
101 template<typename _Tp> inline bool isFinite(_Tp /*value*/)
102 {
103 return true;
104 }
105
107 template<> inline bool isFinite<float32>(float32 value)
108 {
109 unsigned char *ch = (unsigned char *)&value;
110 return (ch[3] & 0x7f) != 0x7f || (ch[2] & 0x80) != 0x80;
111 }
112
114 template<> inline bool isFinite<float64>(float64 value)
115 {
116 unsigned char *ch = (unsigned char *)&value;
117 return (ch[7] & 0x7f) != 0x7f || (ch[6] & 0xf0) != 0xf0;
118 }
119
121 template<> inline bool isFinite<complex64>(complex64 value)
122 {
123 float32 realVal = value.real();
124 float32 imagVal = value.imag();
125 unsigned char *chreal = (unsigned char *)&realVal;
126 unsigned char *chimag = (unsigned char *)&imagVal;
127 return ((chreal[3] & 0x7f) != 0x7f || (chreal[2] & 0x80) != 0x80) && ((chimag[3] & 0x7f) != 0x7f || (chimag[2] & 0x80) != 0x80);
128 }
129
131 template<> inline bool isFinite<complex128>(complex128 value)
132 {
133 float64 realVal = value.real();
134 float64 imagVal = value.imag();
135 unsigned char *chreal = (unsigned char *)&realVal;
136 unsigned char *chimag = (unsigned char *)&imagVal;
137 return ((chreal[7] & 0x7f) != 0x7f || (chreal[6] & 0xf0) != 0xf0) && ((chimag[7] & 0x7f) != 0x7f || (chimag[6] & 0xf0) != 0xf0);
138 }
139
141
150 template<typename _Tp> inline bool isNaN(_Tp value)
151 {
152 return false;
153 }
154
156 template<> inline bool isNaN<float32>(float32 value)
157 {
158 unsigned char *ch = (unsigned char *)&value;
159 return (ch[3] & 0x7f) == 0x7f && ch[2] > 0x80;
160 }
162 template<> inline bool isNaN<float64>(float64 value)
163 {
164 unsigned char *ch = (unsigned char *)&value;
165 return (ch[7] & 0x7f) == 0x7f && ch[6] > 0xf0;
166 }
167
169 template<> inline bool isNaN<complex64>(complex64 value)
170 {
171 float32 realVal = value.real();
172 float32 imagVal = value.imag();
173 unsigned char *chreal = (unsigned char *)&realVal;
174 unsigned char *chimag = (unsigned char *)&imagVal;
175 return ((chreal[3] & 0x7f) == 0x7f && chreal[2] > 0x80) || ((chimag[3] & 0x7f) == 0x7f && chimag[2] > 0x80);
176 }
177
179 template<> inline bool isNaN<complex128>(complex128 value)
180 {
181 float64 realVal = value.real();
182 float64 imagVal = value.imag();
183 unsigned char *chreal = (unsigned char *)&realVal;
184 unsigned char *chimag = (unsigned char *)&imagVal;
185 return ((chreal[7] & 0x7f) == 0x7f && chreal[6] > 0xf0) || ((chimag[7] & 0x7f) == 0x7f && chimag[6] > 0xf0);
186 }
187
189
198 template<typename _Tp> inline bool isInf(_Tp /*value*/)
199 {
200 return false;
201 }
202
204 template<> inline bool isInf<float32>(float32 value)
205 {
206 unsigned char *ch = (unsigned char *)&value;
207 return (ch[3] & 0x7f) == 0x7f && ch[2] == 0x80;
208 }
209
211 template<> inline bool isInf<float64>(float64 value)
212 {
213 unsigned char *ch = (unsigned char *)&value;
214 return (ch[7] & 0x7f) == 0x7f && ch[6] == 0xf0;
215 }
216
218 template<> inline bool isInf<complex64>(complex64 value)
219 {
220 float32 realVal = value.real();
221 float32 imagVal = value.imag();
222 unsigned char *chreal = (unsigned char *)&realVal;
223 unsigned char *chimag = (unsigned char *)&imagVal;
224 return ((chreal[3] & 0x7f) == 0x7f && chreal[2] == 0x80) || ((chimag[3] & 0x7f) == 0x7f && chimag[2] == 0x80);
225 }
226
228 template<> inline bool isInf<complex128>(complex128 value)
229 {
230 float64 realVal = value.real();
231 float64 imagVal = value.imag();
232 unsigned char *chreal = (unsigned char *)&realVal;
233 unsigned char *chimag = (unsigned char *)&imagVal;
234 return ((chreal[7] & 0x7f) == 0x7f && chreal[6] == 0xf0) || ((chimag[7] & 0x7f) == 0x7f && chimag[6] == 0xf0);
235 }
236
238
248 template<typename _Tp> inline bool isZeroValue(_Tp v, _Tp /*epsilon*/)
249 {
250 return v == 0;
251 }
252
253 template<> inline bool isZeroValue(Rgba32 v, Rgba32 /*epsilon*/)
254 {
255 return v == Rgba32::zeros();
256 }
257
258 template<> inline bool isZeroValue(float32 v, float32 epsilon)
259 {
260 return v >= epsilon ? false : (v <= -epsilon ? false : true);
261 }
262
263 template<> inline bool isZeroValue(float64 v, float64 epsilon)
264 {
265 return v >= epsilon ? false : (v <= -epsilon ? false : true);
266 }
267
268 template<> inline bool isZeroValue(std::complex<ito::float32> v, std::complex<ito::float32> epsilon)
269 {
270 return isZeroValue<ito::float32>(v.real(), epsilon.real()) && isZeroValue<ito::float32>(v.imag(), epsilon.real());
271 }
272
273 template<> inline bool isZeroValue(std::complex<ito::float64> v, std::complex<ito::float64> epsilon)
274 {
275 return isZeroValue<ito::float64>(v.real(), epsilon.real()) && isZeroValue<ito::float64>(v.imag(), epsilon.real());
276 }
277
278
280
283 template<typename _Tp> inline bool areEqual(_Tp a, _Tp b)
284 {
285 //for fixed-point and Rgba32 data types
286 return a == b;
287 }
288
289 template<> inline bool areEqual(float32 a, float32 b)
290 {
291 return fabs(a - b) < std::numeric_limits<float32>::epsilon();
292 }
293
294 template<> inline bool areEqual(float64 a, float64 b)
295 {
296 return fabs(a - b) < std::numeric_limits<float64>::epsilon();
297 }
298
299 template<> inline bool areEqual(complex64 a, complex64 b)
300 {
301 return areEqual<ito::float32>(a.real(), b.real()) && areEqual<ito::float32>(a.imag(), b.imag());
302 }
303
304 template<> inline bool areEqual(complex128 a, complex128 b)
305 {
306 return areEqual<ito::float64>(a.real(), b.real()) && areEqual<ito::float64>(a.imag(), b.imag());
307 }
308
309
310} // namespace ito
311
312#endif // NUMERIC_H
Definition apiFunctionsGraph.cpp:40
bool isNotZero(_Tp value)
method returns whether a given variable is not equal to zero.
Definition numeric.h:47
bool isInf< float32 >(float32 value)
Check if a value is infinite float32 values.
Definition numeric.h:204
bool isFinite< float32 >(float32 value)
Check if a value is finite float32 values.
Definition numeric.h:107
bool areEqual(_Tp a, _Tp b)
method returns whether two given numbers of the same type are equal.
Definition numeric.h:283
bool isNotZero< complex64 >(complex64 value)
Check if a value is equal to zero for complex64.
Definition numeric.h:71
bool isFinite(_Tp)
method returns whether a given variable is finite.
Definition numeric.h:101
bool isNaN< complex128 >(complex128 value)
Check if one of the components of complex128 values are not a number.
Definition numeric.h:179
bool isNaN< float64 >(float64 value)
Check if a value is isNaN float64 values.
Definition numeric.h:162
bool isNotZero< float64 >(float64 value)
Check if a value is equal to zero for float64.
Definition numeric.h:62
bool isInf< complex128 >(complex128 value)
Check if one of the components of complex128 values are infinite.
Definition numeric.h:228
bool isNaN< complex64 >(complex64 value)
Check if one of the components of complex64 values are not a number.
Definition numeric.h:169
bool isFinite< complex64 >(complex64 value)
Check if both components of complex64 value are finite.
Definition numeric.h:121
bool isNotZero< complex128 >(complex128 value)
Check if a value is equal to zero for complex128.
Definition numeric.h:81
bool isInf(_Tp)
method returns whether a given variable is Inf / not may be NaN.
Definition numeric.h:198
bool isFinite< float64 >(float64 value)
Check if a value is finite float64 values.
Definition numeric.h:114
bool isNaN< float32 >(float32 value)
Check if a value is isNaN float32 values.
Definition numeric.h:156
bool isZeroValue(_Tp v, _Tp)
method returns whether a given variable is equal to zero.
Definition numeric.h:248
bool isInf< float64 >(float64 value)
Check if a value is infinite float64 values.
Definition numeric.h:211
bool isNaN(_Tp value)
method returns whether a given variable is NaN / not a Number but maybe Inf.
Definition numeric.h:150
bool isNotZero< float32 >(float32 value)
Check if a value is equal to zero for float32.
Definition numeric.h:53
bool isFinite< complex128 >(complex128 value)
Check if both components of complex128 value are finite.
Definition numeric.h:131
bool isInf< complex64 >(complex64 value)
Check if one of the components of complex64 values are infinite.
Definition numeric.h:218