All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_FUNCTIONS_H_
36 #define _BLAZE_MATH_FUNCTIONS_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
49 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // MATHEMATICAL UTILITY FUNCTIONS
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
65 template< typename T >
66 inline int sign( T a );
67 
68 template< typename T >
69 inline size_t digits( T a );
70 
71 template< typename T1, typename T2 >
72 inline const typename MathTrait<T1,T2>::HighType
73  min( const T1& a, const T2& b );
74 
75 template< typename T1, typename T2, typename T3 >
76 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
77  min( const T1& a, const T2& b, const T3& c );
78 
79 template< typename T1, typename T2 >
80 inline const typename MathTrait<T1,T2>::HighType
81  max( const T1& a, const T2& b );
82 
83 template< typename T1, typename T2, typename T3 >
84 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
85  max( const T1& a, const T2& b, const T3& c );
86 
87 template< typename T >
88 inline T round( T a );
89 
90 template< typename T1, typename T2 >
91 inline bool lessThan( T1 a, T2 b );
93 //*************************************************************************************************
94 
95 
96 //*************************************************************************************************
106 template< typename T >
107 inline int sign( T a )
108 {
110 
112  return ( T(0) < a ) - ( a < T(0) );
113  else
114  return ( T(0) < a );
115 }
116 //*************************************************************************************************
117 
118 
119 //*************************************************************************************************
137 template< typename T >
138 inline size_t digits( T a )
139 {
141 
142  size_t count( 0 );
143 
144  while( a != 0 ) {
145  a /= 10;
146  ++count;
147  }
148 
149  return count;
150 }
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
166 template< typename T1, typename T2 >
167 inline const typename MathTrait<T1,T2>::HighType min( const T1& a, const T2& b )
168 {
169  // The return type of the function is only a copy of the one of the arguments for two reasons:
170  // - in case the data types T1 and T2 are equal, a reference return type could result in a
171  // bug if combined with literals
172  // - in case the two data types are unequal, the result of the comparison could be converted
173  // to the more significant data type, which results in a local temporary value
174  // The copy operation might cause a performance decrease for class types, which is probably
175  // avoided if the function is inlined.
176  return ( a < b )?( a ):( b );
177 }
178 //*************************************************************************************************
179 
180 
181 //*************************************************************************************************
194 template< typename T1, typename T2, typename T3 >
195 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
196  min( const T1& a, const T2& b, const T3& c )
197 {
198  // The return type of the function is only a copy of the one of the arguments for two reasons:
199  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
200  // a bug if combined with literals
201  // - in case the three data types are unequal, the result of the comparison could be converted
202  // to the more significant data type, which results in a local temporary value
203  // The copy operation might cause a performance decrease for class types, which is probably
204  // avoided if the function is inlined.
205  return ( a < b )?( ( a < c )?( a ):( c ) ):( ( b < c )?( b ):( c ) );
206 }
207 //*************************************************************************************************
208 
209 
210 //*************************************************************************************************
222 template< typename T1, typename T2 >
223 inline const typename MathTrait<T1,T2>::HighType max( const T1& a, const T2& b )
224 {
225  // The return type of the function is only a copy of the one of the arguments for two reasons:
226  // - in case the data types T1 and T2 are equal, a reference return type could result in a
227  // bug if combined with literals
228  // - in case the two data types are unequal, the result of the comparison could be converted
229  // to the more significant data type, which results in a local temporary value
230  // The copy operation might cause a performance decrease for class types, which is probably
231  // avoided if the function is inlined.
232  return ( a < b )?( b ):( a );
233 }
234 //*************************************************************************************************
235 
236 
237 //*************************************************************************************************
250 template< typename T1, typename T2, typename T3 >
251 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
252  max( const T1& a, const T2& b, const T3& c )
253 {
254  // The return type of the function is only a copy of the one of the arguments for two reasons:
255  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
256  // a bug if combined with literals
257  // - in case the three data types are unequal, the result of the comparison could be converted
258  // to the more significant data type, which results in a local temporary value
259  // The copy operation might cause a performance decrease for class types, which is probably
260  // avoided if the function is inlined.
261  return ( a < b )?( ( b < c )?( c ):( b ) ):( ( a < c )?( c ):( a ) );
262 }
263 //*************************************************************************************************
264 
265 
266 //*************************************************************************************************
278 template< typename T >
279 inline T round( T a )
280 {
282  return a;
283 }
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
299 template<>
300 inline float round( float a )
301 {
302  return std::floor( a + 0.5F );
303 }
305 //*************************************************************************************************
306 
307 
308 //*************************************************************************************************
320 template<>
321 inline double round<double>( double a )
322 {
323  return std::floor( a + 0.5 );
324 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
341 template<>
342 inline long double round<long double>( long double a )
343 {
344  return std::floor( a + 0.5L );
345 }
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
361 template< typename T >
362 inline bool lessThan_backend( T a, T b )
363 {
364  return a < b;
365 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
384 template<>
385 inline bool lessThan_backend<float>( float a, float b )
386 {
387  return ( b - a ) > 1E-8F;
388 }
390 //*************************************************************************************************
391 
392 
393 //*************************************************************************************************
407 template<>
408 inline bool lessThan_backend<double>( double a, double b )
409 {
410  return ( b - a ) > 1E-8;
411 }
413 //*************************************************************************************************
414 
415 
416 //*************************************************************************************************
430 template<>
431 inline bool lessThan_backend<long double>( long double a, long double b )
432 {
433  return ( b - a ) > 1E-10;
434 }
436 //*************************************************************************************************
437 
438 
439 //*************************************************************************************************
451 template< typename T1, typename T2 >
452 inline bool lessThan( T1 a, T2 b )
453 {
454  typedef typename MathTrait<T1,T2>::HighType High;
455  return lessThan_backend<High>( a, b );
456 }
457 //*************************************************************************************************
458 
459 } // namespace blaze
460 
461 #endif
Base template for the MathTrait class.
Definition: MathTrait.h:127
Compile time check for signed data types.This type trait tests whether or not the given template para...
Definition: IsSigned.h:96
int sign(T a)
Sign function.
Definition: Functions.h:107
#define BLAZE_CONSTRAINT_MUST_BE_INTEGRAL_TYPE(T)
Constraint on the data type.In case the given data type T is not an integral data type...
Definition: Integral.h:78
Compile time assertion.
Header file for the IsFloatingPoint type trait.
T round(T a)
Rounds the given input value.
Definition: Functions.h:279
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:94
Constraint on the data type.
Constraint on the data type.
bool lessThan(T1 a, T2 b)
Generic less-than comparison.
Definition: Functions.h:452
Header file for the IsSigned type trait.
Header file for the mathematical trait.
#define BLAZE_CONSTRAINT_MUST_BE_BUILTIN_TYPE(T)
Constraint on the data type.In case the given data type T is not a built-in data type, a compilation error is created.
Definition: Builtin.h:78
Header file for basic type definitions.
size_t digits(T a)
Returns the number of valid digits of an integral value.
Definition: Functions.h:138
Constraint on the data type.