All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_FUNCTIONS_H_
23 #define _BLAZE_MATH_FUNCTIONS_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cmath>
31 #include <limits>
36 #include <blaze/util/Types.h>
37 
38 
39 namespace blaze {
40 
41 //=================================================================================================
42 //
43 // MATHEMATICAL UTILITY FUNCTIONS
44 //
45 //=================================================================================================
46 
47 //*************************************************************************************************
50 template< typename T >
51 inline const T sign( T a );
52 
53 template< typename T >
54 inline size_t digits( T a );
55 
56 template< typename T1, typename T2 >
57 inline const typename MathTrait<T1,T2>::HighType
58  min( const T1& a, const T2& b );
59 
60 template< typename T1, typename T2, typename T3 >
61 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
62  min( const T1& a, const T2& b, const T3& c );
63 
64 template< typename T1, typename T2 >
65 inline const typename MathTrait<T1,T2>::HighType
66  max( const T1& a, const T2& b );
67 
68 template< typename T1, typename T2, typename T3 >
69 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
70  max( const T1& a, const T2& b, const T3& c );
71 
72 template< typename T >
73 inline T round( T a );
74 
75 template< typename T1, typename T2 >
76 inline bool lessThan( T1 a, T2 b );
78 //*************************************************************************************************
79 
80 
81 //*************************************************************************************************
91 template< typename T >
92 inline const T sign( T a )
93 {
94  BLAZE_STATIC_ASSERT( std::numeric_limits<T>::is_signed );
95  return ( a < T(0) )?( T(-1) ):( T(1) );
96 }
97 //*************************************************************************************************
98 
99 
100 //*************************************************************************************************
118 template< typename T >
119 inline size_t digits( T a )
120 {
122 
123  size_t count( 0 );
124 
125  while( a != 0 ) {
126  a /= 10;
127  ++count;
128  }
129 
130  return count;
131 }
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
147 template< typename T1, typename T2 >
148 inline const typename MathTrait<T1,T2>::HighType min( const T1& a, const T2& b )
149 {
150  // The return type of the function is only a copy of the one of the arguments for two reasons:
151  // - in case the data types T1 and T2 are equal, a reference return type could result in a
152  // bug if combined with literals
153  // - in case the two data types are unequal, the result of the comparison could be converted
154  // to the more significant data type, which results in a local temporary value
155  // The copy operation might cause a performance decrease for class types, which is probably
156  // avoided if the function is inlined.
157  return ( a < b )?( a ):( b );
158 }
159 //*************************************************************************************************
160 
161 
162 //*************************************************************************************************
175 template< typename T1, typename T2, typename T3 >
176 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
177  min( const T1& a, const T2& b, const T3& c )
178 {
179  // The return type of the function is only a copy of the one of the arguments for two reasons:
180  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
181  // a bug if combined with literals
182  // - in case the three data types are unequal, the result of the comparison could be converted
183  // to the more significant data type, which results in a local temporary value
184  // The copy operation might cause a performance decrease for class types, which is probably
185  // avoided if the function is inlined.
186  return ( a < b )?( ( a < c )?( a ):( c ) ):( ( b < c )?( b ):( c ) );
187 }
188 //*************************************************************************************************
189 
190 
191 //*************************************************************************************************
203 template< typename T1, typename T2 >
204 inline const typename MathTrait<T1,T2>::HighType max( const T1& a, const T2& b )
205 {
206  // The return type of the function is only a copy of the one of the arguments for two reasons:
207  // - in case the data types T1 and T2 are equal, a reference return type could result in a
208  // bug if combined with literals
209  // - in case the two data types are unequal, the result of the comparison could be converted
210  // to the more significant data type, which results in a local temporary value
211  // The copy operation might cause a performance decrease for class types, which is probably
212  // avoided if the function is inlined.
213  return ( a < b )?( b ):( a );
214 }
215 //*************************************************************************************************
216 
217 
218 //*************************************************************************************************
231 template< typename T1, typename T2, typename T3 >
232 inline const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
233  max( const T1& a, const T2& b, const T3& c )
234 {
235  // The return type of the function is only a copy of the one of the arguments for two reasons:
236  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
237  // a bug if combined with literals
238  // - in case the three data types are unequal, the result of the comparison could be converted
239  // to the more significant data type, which results in a local temporary value
240  // The copy operation might cause a performance decrease for class types, which is probably
241  // avoided if the function is inlined.
242  return ( a < b )?( ( b < c )?( c ):( b ) ):( ( a < c )?( c ):( a ) );
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
259 template< typename T >
260 inline T round( T a )
261 {
263  return a;
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
280 template<>
281 inline float round( float a )
282 {
283  return std::floor( a + 0.5F );
284 }
286 //*************************************************************************************************
287 
288 
289 //*************************************************************************************************
301 template<>
302 inline double round<double>( double a )
303 {
304  return std::floor( a + 0.5 );
305 }
307 //*************************************************************************************************
308 
309 
310 //*************************************************************************************************
322 template<>
323 inline long double round<long double>( long double a )
324 {
325  return std::floor( a + 0.5L );
326 }
328 //*************************************************************************************************
329 
330 
331 //*************************************************************************************************
342 template< typename T >
343 inline bool lessThan_backend( T a, T b )
344 {
345  return a < b;
346 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
365 template<>
366 inline bool lessThan_backend<float>( float a, float b )
367 {
368  return ( b - a ) > 1E-8F;
369 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
388 template<>
389 inline bool lessThan_backend<double>( double a, double b )
390 {
391  return ( b - a ) > 1E-8;
392 }
394 //*************************************************************************************************
395 
396 
397 //*************************************************************************************************
411 template<>
412 inline bool lessThan_backend<long double>( long double a, long double b )
413 {
414  return ( b - a ) > 1E-10;
415 }
417 //*************************************************************************************************
418 
419 
420 //*************************************************************************************************
432 template< typename T1, typename T2 >
433 inline bool lessThan( T1 a, T2 b )
434 {
435  typedef typename MathTrait<T1,T2>::HighType High;
436  return lessThan_backend<High>( a, b );
437 }
438 //*************************************************************************************************
439 
440 } // namespace blaze
441 
442 #endif