All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Equal.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_SHIMS_EQUAL_H_
23 #define _BLAZE_MATH_SHIMS_EQUAL_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
32 #include <blaze/util/Complex.h>
33 
34 
35 namespace blaze {
36 
37 //=================================================================================================
38 //
39 // EQUAL SHIM
40 //
41 //=================================================================================================
42 
43 //*************************************************************************************************
57 inline bool equal( float a, float b )
58 {
59  // Computing the absolute error
60  if( std::fabs( a - b ) <= 1E-6F )
61  return true;
62 
63  // Computing the relative error
64  float relativeError;
65  if( std::fabs(b) > std::fabs(a) )
66  relativeError = std::fabs( ( a - b ) / b );
67  else
68  relativeError = std::fabs( ( a - b ) / a );
69 
70  if( relativeError <= 5E-4F )
71  return true;
72  return false;
73 }
75 //*************************************************************************************************
76 
77 
78 //*************************************************************************************************
92 inline bool equal( float a, double b )
93 {
94  return equal( a, static_cast<float>( b ) );
95 }
97 //*************************************************************************************************
98 
99 
100 //*************************************************************************************************
114 inline bool equal( float a, long double b )
115 {
116  return equal( a, static_cast<float>( b ) );
117 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
136 inline bool equal( double a, float b )
137 {
138  return equal( static_cast<float>( a ), b );
139 }
141 //*************************************************************************************************
142 
143 
144 //*************************************************************************************************
158 inline bool equal( double a, double b )
159 {
160  return std::fabs( a - b ) <= ( 1E-8 * std::fabs( a ) );
161 }
163 //*************************************************************************************************
164 
165 
166 //*************************************************************************************************
180 inline bool equal( double a, long double b )
181 {
182  return std::fabs( a - b ) <= ( 1E-8L * std::fabs( b ) );
183 }
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
202 inline bool equal( long double a, float b )
203 {
204  return equal( static_cast<float>( a ), b );
205 }
207 //*************************************************************************************************
208 
209 
210 //*************************************************************************************************
224 inline bool equal( long double a, double b )
225 {
226  return std::fabs( a - b ) <= ( 1E-8L * std::fabs( a ) );
227 }
229 //*************************************************************************************************
230 
231 
232 //*************************************************************************************************
246 inline bool equal( long double a, long double b )
247 {
248  return std::fabs( a - b ) <= ( 1E-8L * std::fabs( a ) );
249 }
251 //*************************************************************************************************
252 
253 
254 //*************************************************************************************************
267 template< typename T1 // Type of the left-hand side complex value
268  , typename T2 > // Type of the right-hand side scalar value
269 inline bool equal( complex<T1> a, T2 b )
270 {
271  return equal( a.real(), b ) && isDefault( a.imag() );
272 }
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
290 template< typename T1 // Type of the left-hand side scalar value
291  , typename T2 > // Type of the right-hand side complex value
292 inline bool equal( T1 a, complex<T2> b )
293 {
294  return equal( a, b.real() ) && isDefault( b.imag() );
295 }
297 //*************************************************************************************************
298 
299 
300 //*************************************************************************************************
313 template< typename T1 // Type of the left-hand side complex value
314  , typename T2 > // Type of the right-hand side complex value
315 inline bool equal( complex<T1> a, complex<T2> b )
316 {
317  return equal( a.real(), b.real() ) && equal( a.imag(), b.imag() );
318 }
320 //*************************************************************************************************
321 
322 
323 //*************************************************************************************************
337 template< typename T1 // Type of the left-hand side value/object
338  , typename T2 > // Type of the right-hand side value/object
339 inline bool equal( const T1& a, const T2& b )
340 {
341  return a == b;
342 }
343 //*************************************************************************************************
344 
345 } // namespace blaze
346 
347 #endif