Main MRPT website > C++ reference for MRPT 1.9.9
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-2018, 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/core/SSE_types.h> // needed by SSE intrinsics used in some inline functions below.
13 #include <cmath> // pow(), lrint()
14 
15 namespace mrpt
16 {
17 /** \addtogroup mrpt_round Round functions (in #include <mrpt/core/round.h>)
18  * \ingroup mrpt_core_grp
19  * @{ */
20 
21 /** Returns the closer integer (int) to x */
22 template <typename T>
23 inline int round(const T value)
24 {
25 #if MRPT_HAS_SSE2
26  __m128d t = _mm_set_sd(value);
27  return _mm_cvtsd_si32(t);
28 #else
29  return static_cast<int>(lrint(value));
30 #endif
31 }
32 
33 /** Returns the closer integer (long) to x */
34 template <typename T>
35 inline long round_long(const T value)
36 {
37 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE == 64
38  __m128d t = _mm_set_sd(value);
39  return _mm_cvtsd_si64(t);
40 #else
41  return lrint(value);
42 #endif
43 }
44 
45 /** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and
46  * also fractions)
47  * power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2
48  * -> 0.01, ...
49  */
50 template <class T>
51 T round_10power(T val, int power10)
52 {
53  long double F = ::pow((long double)10.0, -(long double)power10);
54  long int t = round_long(val * F);
55  return T(t / F);
56 }
57 
58 /** @} */
59 } // namespace mrpt
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::round_long
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: round.h:35
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
SSE_types.h
val
int val
Definition: mrpt_jpeglib.h:955
mrpt::round_10power
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:51
mrpt::round
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23
value
GLsizei const GLfloat * value
Definition: glext.h:4117



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST