Main MRPT website > C++ reference for MRPT 1.5.6
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-2017, 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  /** \addtogroup mrpt_round Round functions (in #include <mrpt/utils/round.h>)
21  * \ingroup mrpt_base_grp
22  * @{ */
23 
24  /** Returns the closer integer (int) to x */
25  template <typename T>
26  inline int round(const T value)
27  {
28  #if MRPT_HAS_SSE2
29  __m128d t = _mm_set_sd( value );
30  return _mm_cvtsd_si32(t);
31  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
32  int t;
33  __asm
34  {
35  fld value;
36  fistp t;
37  }
38  return t;
39  #elif defined HAVE_LRINT || defined __GNUC__
40  return static_cast<int>(lrint(value));
41  #else
42  return static_cast<int>(value + 0.5);
43  #endif
44  }
45 
46  /** Returns the closer integer (long) to x */
47  template <typename T>
48  inline long round_long(const T value)
49  {
50  #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
51  __m128d t = _mm_set_sd( value );
52  return _mm_cvtsd_si64(t);
53  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
54  long t;
55  __asm
56  {
57  fld value;
58  fistp t;
59  }
60  return t;
61  #elif defined HAVE_LRINT || defined __GNUC__
62  return lrint(value);
63  #else
64  return static_cast<long>(value + 0.5);
65  #endif
66  }
67 
68  /** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and also fractions)
69  * power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2 -> 0.01, ...
70  */
71  template <class T>
72  T round_10power(T val, int power10)
73  {
74  long double F = ::pow((long double)10.0,-(long double)power10);
75  long int t = mrpt::utils::round_long( val * F );
76  return T(t/F);
77  }
78 
79  /** @} */
80  } // End of namespace
81 } // end of namespace
GLdouble GLdouble t
Definition: glext.h:3610
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: round.h:48
int val
Definition: mrpt_jpeglib.h:953
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
T round_10power(T val, int power10)
Round a decimal number up to the given 10&#39;th power (eg, to 1000,100,10, and also fractions) power10 m...
Definition: round.h:72
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
GLsizei const GLfloat * value
Definition: glext.h:3929



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019