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 #include <blaze/util/EnableIf.h>
51 
52 
53 namespace blaze {
54 
55 //=================================================================================================
56 //
57 // EQUAL SHIM
58 //
59 //=================================================================================================
60 
61 //*************************************************************************************************
75 template< bool RF // Relaxation flag
76  , typename T1 // Type of the left-hand side value/object
77  , typename T2 // Type of the right-hand side value/object
78  , typename = EnableIf_t< ( IsSigned_v<T1> && IsSigned_v<T2> ) ||
79  ( IsUnsigned_v<T1> && IsUnsigned_v<T2> ) > >
80 inline constexpr bool equal( const T1& a, const T2& b )
81 {
82  return a == b;
83 }
84 //*************************************************************************************************
85 
86 
87 //*************************************************************************************************
105 template< bool RF > // Relaxation flag
106 inline bool equal( float a, float b )
107 {
108  if( RF == relaxed ) {
109  const float acc( static_cast<float>( accuracy ) );
110  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
111  }
112  else {
113  return a == b;
114  }
115 }
117 //*************************************************************************************************
118 
119 
120 //*************************************************************************************************
138 template< bool RF > // Relaxation flag
139 inline bool equal( float a, double b )
140 {
141  return equal<RF>( a, static_cast<float>( b ) );
142 }
144 //*************************************************************************************************
145 
146 
147 //*************************************************************************************************
165 template< bool RF > // Relaxation flag
166 inline bool equal( float a, long double b )
167 {
168  return equal<RF>( a, static_cast<float>( b ) );
169 }
171 //*************************************************************************************************
172 
173 
174 //*************************************************************************************************
188 template< bool RF > // Relaxation flag
189 inline bool equal( double a, float b )
190 {
191  return equal<RF>( static_cast<float>( a ), b );
192 }
194 //*************************************************************************************************
195 
196 
197 //*************************************************************************************************
215 template< bool RF > // Relaxation flag
216 inline bool equal( double a, double b )
217 {
218  if( RF == relaxed ) {
219  const double acc( static_cast<double>( accuracy ) );
220  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
221  }
222  else {
223  return a == b;
224  }
225 }
227 //*************************************************************************************************
228 
229 
230 //*************************************************************************************************
248 template< bool RF > // Relaxation flag
249 inline bool equal( double a, long double b )
250 {
251  return equal<RF>( a, static_cast<double>( b ) );
252 }
254 //*************************************************************************************************
255 
256 
257 //*************************************************************************************************
275 template< bool RF > // Relaxation flag
276 inline bool equal( long double a, float b )
277 {
278  return equal<RF>( static_cast<float>( a ), b );
279 }
281 //*************************************************************************************************
282 
283 
284 //*************************************************************************************************
302 template< bool RF > // Relaxation flag
303 inline bool equal( long double a, double b )
304 {
305  return equal<RF>( static_cast<double>( a ), b );
306 }
308 //*************************************************************************************************
309 
310 
311 //*************************************************************************************************
329 template< bool RF > // Relaxation flag
330 inline bool equal( long double a, long double b )
331 {
332  if( RF == relaxed ) {
333  const long double acc( static_cast<long double>( accuracy ) );
334  return ( std::fabs( a - b ) <= max( acc, acc * std::fabs( a ) ) );
335  }
336  else {
337  return a == b;
338  }
339 }
341 //*************************************************************************************************
342 
343 
344 //*************************************************************************************************
357 template< bool RF // Relaxation flag
358  , typename T1 // Type of the left-hand side complex value
359  , typename T2 > // Type of the right-hand side scalar value
360 inline bool equal( complex<T1> a, T2 b )
361 {
362  return equal<RF>( real( a ), b ) && equal<RF>( imag( a ), T1() );
363 }
365 //*************************************************************************************************
366 
367 
368 //*************************************************************************************************
381 template< bool RF // Relaxation flag
382  , typename T1 // Type of the left-hand side scalar value
383  , typename T2 > // Type of the right-hand side complex value
384 inline bool equal( T1 a, complex<T2> b )
385 {
386  return equal<RF>( a, real( b ) ) && equal<RF>( imag( b ), T2() );
387 }
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
405 template< bool RF // Relaxation flag
406  , typename T1 // Type of the left-hand side complex value
407  , typename T2 > // Type of the right-hand side complex value
408 inline bool equal( complex<T1> a, complex<T2> b )
409 {
410  return equal<RF>( real( a ), real( b ) ) && equal<RF>( imag( a ), imag( b ) );
411 }
413 //*************************************************************************************************
414 
415 
416 //*************************************************************************************************
430 template< typename T1 // Type of the left-hand side value/object
431  , typename T2 > // Type of the right-hand side value/object
432 inline constexpr bool equal( const T1& a, const T2& b )
433 {
434  return equal<relaxed>( a, b );
435 }
436 //*************************************************************************************************
437 
438 } // namespace blaze
439 
440 #endif
Computation accuracy for floating point data types.
decltype(auto) real(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the real part of each single element of dm.
Definition: DMatMapExpr.h:1392
Headerfile for the generic max algorithm.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1179
Header file for the EnableIf class template.
Header file for the relaxation flag types.
constexpr bool relaxed
Relaxation flag for relaxed semantics.
Definition: RelaxationFlag.h:85
Header file for the IsSigned type trait.
Header file for the IsUnsigned type trait.
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:907
decltype(auto) imag(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the imaginary part of each single element of dm.
Definition: DMatMapExpr.h:1421
bool equal(const SharedValue< T1 > &lhs, const SharedValue< T2 > &rhs)
Equality check for a two shared values.
Definition: SharedValue.h:342