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-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 #include <cmath> // pow(), lrint()
14 
15 namespace mrpt
16 {
17 namespace utils
18 {
19 /** \addtogroup mrpt_round Round functions (in #include <mrpt/utils/round.h>)
20  * \ingroup mrpt_base_grp
21  * @{ */
22 
23 /** Returns the closer integer (int) to x */
24 template <typename T>
25 inline int round(const T value)
26 {
27 #if MRPT_HAS_SSE2
28  __m128d t = _mm_set_sd(value);
29  return _mm_cvtsd_si32(t);
30 #else
31  return static_cast<int>(lrint(value));
32 #endif
33 }
34 
35 /** Returns the closer integer (long) to x */
36 template <typename T>
37 inline long round_long(const T value)
38 {
39 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE == 64
40  __m128d t = _mm_set_sd(value);
41  return _mm_cvtsd_si64(t);
42 #else
43  return lrint(value);
44 #endif
45 }
46 
47 /** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and
48  * also fractions)
49  * power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2
50  * -> 0.01, ...
51  */
52 template <class T>
53 T round_10power(T val, int power10)
54 {
55  long double F = ::pow((long double)10.0, -(long double)power10);
56  long int t = mrpt::utils::round_long(val * F);
57  return T(t / F);
58 }
59 
60 /** @} */
61 } // End of namespace
62 } // end of namespace
GLdouble GLdouble t
Definition: glext.h:3689
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: round.h:37
int val
Definition: mrpt_jpeglib.h:955
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:53
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLsizei const GLfloat * value
Definition: glext.h:4117



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019