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 <blaze/math/Accuracy.h>
47 #include <blaze/util/Complex.h>
48 
49 
50 namespace blaze {
51 
52 //=================================================================================================
53 //
54 // EQUAL SHIM
55 //
56 //=================================================================================================
57 
58 //*************************************************************************************************
73 template< bool RF // Relaxation flag
74  , typename T1 // Type of the left-hand side value/object
75  , typename T2 > // Type of the right-hand side value/object
76 inline constexpr bool equal( const T1& a, const T2& b )
77 {
78  return a == b;
79 }
80 //*************************************************************************************************
81 
82 
83 //*************************************************************************************************
101 template< bool RF > // Relaxation flag
102 inline bool equal( float a, float b )
103 {
104  if( RF == relaxed ) {
105  const float acc( static_cast<float>( accuracy ) );
106  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
107  }
108  else {
109  return a == b;
110  }
111 }
113 //*************************************************************************************************
114 
115 
116 //*************************************************************************************************
134 template< bool RF > // Relaxation flag
135 inline bool equal( float a, double b )
136 {
137  return equal<RF>( a, static_cast<float>( b ) );
138 }
140 //*************************************************************************************************
141 
142 
143 //*************************************************************************************************
161 template< bool RF > // Relaxation flag
162 inline bool equal( float a, long double b )
163 {
164  return equal<RF>( a, static_cast<float>( b ) );
165 }
167 //*************************************************************************************************
168 
169 
170 //*************************************************************************************************
184 template< bool RF > // Relaxation flag
185 inline bool equal( double a, float b )
186 {
187  return equal<RF>( static_cast<float>( a ), b );
188 }
190 //*************************************************************************************************
191 
192 
193 //*************************************************************************************************
211 template< bool RF > // Relaxation flag
212 inline bool equal( double a, double b )
213 {
214  if( RF == relaxed ) {
215  const double acc( static_cast<double>( accuracy ) );
216  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
217  }
218  else {
219  return a == b;
220  }
221 }
223 //*************************************************************************************************
224 
225 
226 //*************************************************************************************************
244 template< bool RF > // Relaxation flag
245 inline bool equal( double a, long double b )
246 {
247  return equal<RF>( a, static_cast<double>( b ) );
248 }
250 //*************************************************************************************************
251 
252 
253 //*************************************************************************************************
271 template< bool RF > // Relaxation flag
272 inline bool equal( long double a, float b )
273 {
274  return equal<RF>( static_cast<float>( a ), b );
275 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
298 template< bool RF > // Relaxation flag
299 inline bool equal( long double a, double b )
300 {
301  return equal<RF>( static_cast<double>( a ), b );
302 }
304 //*************************************************************************************************
305 
306 
307 //*************************************************************************************************
325 template< bool RF > // Relaxation flag
326 inline bool equal( long double a, long double b )
327 {
328  if( RF == relaxed ) {
329  const long double acc( static_cast<long double>( accuracy ) );
330  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
331  }
332  else {
333  return a == b;
334  }
335 }
337 //*************************************************************************************************
338 
339 
340 //*************************************************************************************************
353 template< bool RF // Relaxation flag
354  , typename T1 // Type of the left-hand side complex value
355  , typename T2 > // Type of the right-hand side scalar value
356 inline bool equal( complex<T1> a, T2 b )
357 {
358  return equal<RF>( real( a ), b ) && equal<RF>( imag( a ), T1() );
359 }
361 //*************************************************************************************************
362 
363 
364 //*************************************************************************************************
377 template< bool RF // Relaxation flag
378  , typename T1 // Type of the left-hand side scalar value
379  , typename T2 > // Type of the right-hand side complex value
380 inline bool equal( T1 a, complex<T2> b )
381 {
382  return equal<RF>( a, real( b ) ) && equal<RF>( imag( b ), T2() );
383 }
385 //*************************************************************************************************
386 
387 
388 //*************************************************************************************************
401 template< bool RF // Relaxation flag
402  , typename T1 // Type of the left-hand side complex value
403  , typename T2 > // Type of the right-hand side complex value
404 inline bool equal( complex<T1> a, complex<T2> b )
405 {
406  return equal<RF>( real( a ), real( b ) ) && equal<RF>( imag( a ), imag( b ) );
407 }
409 //*************************************************************************************************
410 
411 
412 //*************************************************************************************************
426 template< typename T1 // Type of the left-hand side value/object
427  , typename T2 > // Type of the right-hand side value/object
428 inline constexpr bool equal( const T1& a, const T2& b )
429 {
430  return equal<relaxed>( a, b );
431 }
432 //*************************************************************************************************
433 
434 } // namespace blaze
435 
436 #endif
Computation accuracy for floating point data types.
constexpr bool equal(const T1 &a, const T2 &b)
Generic equality check.
Definition: Equal.h:76
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1387
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
Headerfile for the generic max algorithm.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the relaxation flag types.
constexpr bool relaxed
Relaxation flag for relaxed semantics.
Definition: RelaxationFlag.h:85
Header file for the complex data type.
constexpr Accuracy accuracy
Global Accuracy instance.The blaze::accuracy instance can be used wherever a floating point data type...
Definition: Accuracy.h:902
decltype(auto) imag(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the imaginary part of each single element of dm.
Definition: DMatMapExpr.h:1416