Main MRPT website > C++ reference
MRPT logo
round.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/utils/SSE_types.h> // needed by SSE intrinsics used in some inline functions below.
13 #define _USE_MATH_DEFINES // (For VS to define M_PI, etc. in cmath)
14 #include <cmath> // pow()
15 
16 namespace mrpt
17 {
18  namespace utils
19  {
20  /** Returns the closer integer (int) to x */
21  template <typename T>
22  inline int round(const T value)
23  {
24  #if MRPT_HAS_SSE2
25  __m128d t = _mm_set_sd( value );
26  return _mm_cvtsd_si32(t);
27  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
28  int t;
29  __asm
30  {
31  fld value;
32  fistp t;
33  }
34  return t;
35  #elif defined HAVE_LRINT || defined __GNUC__
36  return static_cast<int>(lrint(value));
37  #else
38  return static_cast<int>(value + 0.5);
39  #endif
40  }
41 
42  /** Returns the closer integer (long) to x */
43  template <typename T>
44  inline long round_long(const T value)
45  {
46  #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
47  __m128d t = _mm_set_sd( value );
48  return _mm_cvtsd_si64(t);
49  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
50  long t;
51  __asm
52  {
53  fld value;
54  fistp t;
55  }
56  return t;
57  #elif defined HAVE_LRINT || defined __GNUC__
58  return lrint(value);
59  #else
60  return static_cast<long>(value + 0.5);
61  #endif
62  }
63 
64  /** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and also fractions)
65  * power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2 -> 0.01, ...
66  */
67  template <class T>
68  T round_10power(T val, int power10)
69  {
70  long double F = ::pow((long double)10.0,-(long double)power10);
71  long int t = mrpt::utils::round_long( val * F );
72  return T(t/F);
73  }
74 
75  } // End of namespace
76 } // end of namespace
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:22
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
T round_10power(T val, int power10)
Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and also fractions) power10 m...
Definition: round.h:68
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: round.h:44
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015