Equal.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SHIMS_EQUAL_H_
36 #define _BLAZE_MATH_SHIMS_EQUAL_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <boost/math/special_functions/next.hpp>
45 #include <blaze/math/Accuracy.h>
46 #include <blaze/util/Complex.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // EQUAL SHIM
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
71 template< typename T1 // Type of the left-hand side value/object
72  , typename T2 > // Type of the right-hand side value/object
73 inline bool equal( const T1& a, const T2& b )
74 {
75  return a == b;
76 }
77 //*************************************************************************************************
78 
79 
80 //*************************************************************************************************
98 inline bool equal( float a, float b )
99 {
100  using boost::math::float_advance;
101 
102  const int distance( 6 );
103 
104  return ( std::fabs( a - b ) <= 1E-6 ) ||
105  ( a < b && b <= float_advance( a, distance ) ) ||
106  ( b < a && a <= float_advance( b, distance ) );
107 }
109 //*************************************************************************************************
110 
111 
112 //*************************************************************************************************
130 inline bool equal( float a, double b )
131 {
132  return equal( a, static_cast<float>( b ) );
133 }
135 //*************************************************************************************************
136 
137 
138 //*************************************************************************************************
156 inline bool equal( float a, long double b )
157 {
158  return equal( a, static_cast<float>( b ) );
159 }
161 //*************************************************************************************************
162 
163 
164 //*************************************************************************************************
178 inline bool equal( double a, float b )
179 {
180  return equal( static_cast<float>( a ), b );
181 }
183 //*************************************************************************************************
184 
185 
186 //*************************************************************************************************
204 inline bool equal( double a, double b )
205 {
206  using boost::math::float_advance;
207 
208  const int distance( 4 );
209 
210  return ( std::fabs( a - b ) <= accuracy ) ||
211  ( a < b && b <= float_advance( a, distance ) ) ||
212  ( b < a && a <= float_advance( b, distance ) );
213 }
215 //*************************************************************************************************
216 
217 
218 //*************************************************************************************************
236 inline bool equal( double a, long double b )
237 {
238  return std::fabs( a - b ) <= ( 1E-8L * std::fabs( b ) );
239 }
241 //*************************************************************************************************
242 
243 
244 //*************************************************************************************************
262 inline bool equal( long double a, float b )
263 {
264  return equal( static_cast<float>( a ), b );
265 }
267 //*************************************************************************************************
268 
269 
270 //*************************************************************************************************
288 inline bool equal( long double a, double b )
289 {
290  return equal( static_cast<double>( a ), b );
291 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
314 inline bool equal( long double a, long double b )
315 {
316  using boost::math::float_advance;
317 
318  const int distance( 4 );
319 
320  return ( std::fabs( a - b ) <= accuracy ) ||
321  ( a < b && b <= float_advance( a, distance ) ) ||
322  ( b < a && a <= float_advance( b, distance ) );
323 }
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
341 template< typename T1 // Type of the left-hand side complex value
342  , typename T2 > // Type of the right-hand side scalar value
343 inline bool equal( complex<T1> a, T2 b )
344 {
345  return equal( real( a ), b ) && equal( imag( a ), T1() );
346 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
364 template< typename T1 // Type of the left-hand side scalar value
365  , typename T2 > // Type of the right-hand side complex value
366 inline bool equal( T1 a, complex<T2> b )
367 {
368  return equal( a, real( b ) ) && equal( imag( b ), T2() );
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
387 template< typename T1 // Type of the left-hand side complex value
388  , typename T2 > // Type of the right-hand side complex value
389 inline bool equal( complex<T1> a, complex<T2> b )
390 {
391  return equal( real( a ), real( b ) ) && equal( imag( a ), imag( b ) );
392 }
394 //*************************************************************************************************
395 
396 } // namespace blaze
397 
398 #endif
Computation accuracy for floating point data types.
const ImagExprTrait< MT >::Type imag(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the imaginary part of each single element of dm.
Definition: DMatImagExpr.h:920
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const RealExprTrait< MT >::Type real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatRealExpr.h:920
bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:73
const Accuracy accuracy
Global Accuracy instance.The blaze::accuracy instance can be used wherever a floating point data type...
Definition: Accuracy.h:901
Header file for the complex data type.